Functions to manipulate obs_properties_add_editable_list()

SN1974

New Member
I cannot seem to locate any functions that allow a script to manipulate the contents of the list box created with obs_properties_add_editable_list(). As an experiment I tried the obs_property_list_add_string() but that failed. Does anyone have any suggestions?

Thanks
SN1974
 

Phoenix_ZA

New Member
Hello,

So it's a PITA, as you have to make an array from scratch with the `obs_data_array_create` method, then populate it with the `obs_data_array_push_back` method, but you must use data_t objects and not the raw values, with each of those object being made from JSON by using the `obs_data_create_from_json` method, with the JSON being of a schema that looks like `{"hidden":False, "selected":False, "value":value}`

In order to use it practically, I had to make my own helper functions for the basic array operations:
Python:
import json

import obspython as S


def _from_data_t(data_t):
    j = S.obs_data_get_json(data_t)
    d = json.loads(j)
    return d["value"]

def _to_data_t(value):
    dic = {"hidden":False, "selected":False, "value":value}
    j = json.dumps(dic)
    return S.obs_data_create_from_json(j)

def _array_t_to_list(array_t):
    length = S.obs_data_array_count(array_t)
    data_t_list = [S.obs_data_array_item(array_t, i) for i in range(length)]
    return [_from_data_t(data_t) for data_t in data_t_list]

def _list_to_array_t(values):
    data_t_list = [_to_data_t(value) for value in values]
    array_t = S.obs_data_array_create()
    for data_t in data_t_list:
        S.obs_data_array_push_back(array_t, data_t)
    return array_t

And here is an example of how I set a default setting:

Python:
def script_defaults(settings):
    array_t = _list_to_array_t(["Scene 1", "Scene 2", "Scene 3"])
    S.obs_data_set_default_array(settings, "scene_setting", array_t)

And how to retrieve those settings:

Python:
def script_update(settings):
    scenes_array_t = S.obs_data_get_array(settings, "scene_setting")
    scenes = _array_t_to_list(scenes_array_t)
 
Top