Hotkey Persistance issue with Python Script

Scratch

New Member
I've been having trouble with this for several hours.
I've been looking at other's people's code, as the samples multiple times
The data is being stored in the basic.ini file, however, whenever the script is loaded, it is not being applied.

My code looks exactly like this
Code:
UNIQUE_CODE = "brownpants"
hotkey_id = obs.OBS_INVALID_HOTKEY_ID

...

def script_load(settings):
    hotkey_id = obs.obs_hotkey_register_frontend("randomVideo_{}".format(UNIQUE_CODE), "Play Random Video ({})".format(UNIQUE_CODE), hotkey_pressed)
    hotkey_save_array = obs.obs_data_get_array(settings, "randomVideo_{}".format(UNIQUE_CODE))
    obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)

def script_save(settings):
    hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
    obs.obs_data_set_array(settings, "randomVideo_{}".format(UNIQUE_CODE), hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
 

Scratch

New Member
Given this a few days, Some more info:
I did mention that the settings are being saved, so I've been looking at the loading function specifically
I've toyed around with the variables just for testing
My basic.ini has this in hit hotkeys block
Code:
randomVideo={\n    "bindings": [\n        {\n            "key": "OBS_KEY_NUM9"\n        }\n    ]\n}
But when I use these two lines of code:
Code:
    hotkey_save_array = obs.obs_data_get_array(settings, "randomVideo")
    obs.script_log(obs.LOG_DEBUG, "hksa: " + hotkey_save_array)
The Log spits out
Code:
[randomvideo.py] Traceback (most recent call last):
[randomvideo.py]   File "D:/Scratch/Documents/Git/py-obsVideo\randomvideo.py", line 70, in script_load
[randomvideo.py]     obs.script_log(obs.LOG_DEBUG, "hksa: " + hotkey_save_array)
[randomvideo.py] TypeError: must be str, not NoneType

The NoneType suggests that either OBS is returning some bad data, or there's something on how I'm storing/retrieving the data
I'd print out what I could fetch from settings, but it (a swigpyobject) doesn't have a proper dict. Can't print em, can't pickle em.
 

Scratch

New Member
I've been jebaited
18-06-11_20-07-54-CUsersScratchAppDataRoamingobs-studiobasic.png


Extra content is saved with the scene data, not the profile
 

Scratch

New Member
Fixed the issue.
Noticing it was actually not saving anything, means that the entire thing was kaput.
And doing a lot of work today on the file, I noticed how important the global keyword was.
Therefore, since the hotkey_id would have not been persistent regarding any changes.

No Scopped
 

upgradeQ

Member
Had similar issue with hotkeys, here is full example code with global
Python:
import obspython as obs
from itertools import cycle

datacycle = cycle([1, 2, 3, 4, 5])
HOTKEY_ID = obs.OBS_INVALID_HOTKEY_ID


class Example:
    def __init__(self, source_name=None):
        self.source_name = source_name

    def update_text(self):
        source = obs.obs_get_source_by_name(self.source_name)
        if source is not None:
            data = str(next(datacycle))
            settings = obs.obs_data_create()
            obs.obs_data_set_string(settings, "text", data)
            obs.obs_source_update(source, settings)
            obs.obs_data_release(settings)
            obs.obs_source_release(source)


eg = Example()  # class created ,obs part starts


def script_description():
    return "Hotkey example"


def script_save(settings):
    global HOTKEY_ID
    hotkey_save_array_htk = obs.obs_hotkey_save(HOTKEY_ID)
    obs.obs_data_set_array(settings, "htk_hotkey", hotkey_save_array_htk)
    obs.obs_data_array_release(hotkey_save_array_htk)


def script_load(settings):
    global HOTKEY_ID
    def callback(pressed):
        if pressed:
            return eg.update_text()

    HOTKEY_ID = obs.obs_hotkey_register_frontend(
        "htk_id", "Example hotkey", callback
    )
    hotkey_save_array_htk = obs.obs_data_get_array(settings, "htk_hotkey")
    obs.obs_hotkey_load(HOTKEY_ID, hotkey_save_array_htk)
    obs.obs_data_array_release(hotkey_save_array_htk)


def script_update(settings):
    eg.source_name = obs.obs_data_get_string(settings, "source")


def script_properties():  # ui
    props = obs.obs_properties_create()
    p = obs.obs_properties_add_list(
        props,
        "source",
        "Text Source",
        obs.OBS_COMBO_TYPE_EDITABLE,
        obs.OBS_COMBO_FORMAT_STRING,
    )
    sources = obs.obs_enum_sources()
    if sources is not None:
        for source in sources:
            source_id = obs.obs_source_get_unversioned_id(source)
            if source_id == "text_gdiplus" or source_id == "text_ft2_source":
                name = obs.obs_source_get_name(source)
                obs.obs_property_list_add_string(p, name, name)

        obs.source_list_release(sources)
    return props
github repo
 
Top