I have a Problem with my Code...

maxhewe

New Member
Since yesterday, I'm trying to figure out how to program my own python scripts for OBS. I found this code example (https://github.com/nibalizer/obs-scripts-examples/blob/master/helloworld.py) which was really useful because I could train with it and modify it so I could use it in my final program. I want this script to output two different Hello World massages in two different text fields. This is what I have done:
Python:
import obspython as obs

source_name = ""
source2_name = ""
i = 1
# ------------------------------------------------------------


def refresh_pressed(props, prop):
    """
    Called when the 'refresh' button defined below is pressed
    """
    print("Refresh Pressed")
    update_all()

def update_all():
    update_text()
    update_text2()



def update_text2():
    global source2_name

    source2 = obs.obs_get_source_by_name(source2_name)
    text2 = "Hello World2"
    print(text2)
    if i == 1:
        print("Check1")
        settings2 = obs.obs_data_create()
        obs.obs_data_set_string(settings2, "text2", text2)
        obs.obs_source_update(source2, settings2)
        obs.obs_data_release(settings2)
        obs.obs_source_release(source2)
        print("check2")

def update_text():
    global source_name

    source = obs.obs_get_source_by_name(source_name)
    text = "Hello World11"
    print(text)
    if i == 1:
        print("Check11")
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text", text)
        obs.obs_source_update(source, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source)
        print("CHeck22")
# ------------------------------------------------------------


def script_properties():
    """
    Called to define user properties associated with the script. These
    properties are used to define how to show settings properties to a user.
    """
    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_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)
    p2 = obs.obs_properties_add_list(props, "source2", "Text2 Source",
                                     obs.OBS_COMBO_TYPE_EDITABLE,
                                     obs.OBS_COMBO_FORMAT_STRING)
    sources2 = obs.obs_enum_sources()
    if sources2 is not None:
        for source2 in sources2:
            source2_id = obs.obs_source_get_id(source2)
            if source2_id == "text2_gdiplus" or source2_id == "text2_ft2_source":
                name2 = obs.obs_source_get_name(source2)
                obs.obs_property_list_add_string(p2, name2, name2)

        obs.source_list_release(sources2)

    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
    return props



def script_update(settings):
    """
    Called when the script’s settings (if any) have been changed by the user.
    """
    global source_name

    source_name = obs.obs_data_get_string(settings, "source")

def script2_update(settings2):
    """
    Called when the script’s settings (if any) have been changed by the user.
    """
    global source2_name

    source2_name = obs.obs_data_get_string(settings2, "source2")

(I know it isn't beautiful, but I hope it gets the job done)

I really don't know why it isn't working. Could someone help me out with that?

I hope this is the right forum...

 

maxhewe

New Member
I suggest you to read https://github.com/obsproject/obs-studio/wiki/Getting-Started-With-OBS-Scripting
You don't need script2_update, put source2_name into script_update and it should work.
I've wrote https://github.com/upgradeQ/obstextoverlay - a script which has keyboard navigation & displays different metrics in text sources you might want to check out the code.
I changed my code to this, but it still doesn't work....
Python:
import obspython as obs

source_name = ""
source2_name = ""
i = 1
# ------------------------------------------------------------


def refresh_pressed(props, prop):
    """
    Called when the 'refresh' button defined below is pressed
    """
    print("Refresh Pressed")
    update_all()

def update_all():
    update_text()
    update_text2()



def update_text2():
    global source2_name

    source2 = obs.obs_get_source_by_name(source2_name)
    text2 = "S2"
    print(text2)
    if i == 1:
        print("Check1")
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text2", text2)
        obs.obs_source_update(source2, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source2)
        print("check2")

def update_text():
    global source_name

    source = obs.obs_get_source_by_name(source_name)
    text = "S1"
    print(text)
    if i == 1:
        print("Check11")
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text", text)
        obs.obs_source_update(source, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source)
        print("CHeck22")
# ------------------------------------------------------------


def script_properties():
    """
    Called to define user properties associated with the script. These
    properties are used to define how to show settings properties to a user.
    """
    props = obs.obs_properties_create()

    b = obs.obs_properties_add_list(props, "source2", "Text2 Source",
                                    obs.OBS_COMBO_TYPE_EDITABLE,
                                    obs.OBS_COMBO_FORMAT_STRING)
    sources = obs.obs_enum_sources()
    if sources is not None:
        for source2 in sources:
            source2_id = obs.obs_source_get_id(source2)
            if source2_id == "text_gdiplus" or source2_id == "text_ft2_source":
                name2 = obs.obs_source_get_name(source2)
                obs.obs_property_list_add_string(b, name2, name2)

        obs.source_list_release(sources)

    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_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)


    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
    return props



def script_update(settings):
    """
    Called when the script’s settings (if any) have been changed by the user.
    """
    global source_name
    global source2_name

    source_name = obs.obs_data_get_string(settings, "source")
    source2_name = obs.obs_data_get_string(settings, "source2")
 

maxhewe

New Member
I changed my code to this, but it still doesn't work....
Python:
import obspython as obs

source_name = ""
source2_name = ""
i = 1
# ------------------------------------------------------------


def refresh_pressed(props, prop):
    """
    Called when the 'refresh' button defined below is pressed
    """
    print("Refresh Pressed")
    update_all()

def update_all():
    update_text()
    update_text2()



def update_text2():
    global source2_name

    source2 = obs.obs_get_source_by_name(source2_name)
    text2 = "S2"
    print(text2)
    if i == 1:
        print("Check1")
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text2", text2)
        obs.obs_source_update(source2, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source2)
        print("check2")

def update_text():
    global source_name

    source = obs.obs_get_source_by_name(source_name)
    text = "S1"
    print(text)
    if i == 1:
        print("Check11")
        settings = obs.obs_data_create()
        obs.obs_data_set_string(settings, "text", text)
        obs.obs_source_update(source, settings)
        obs.obs_data_release(settings)
        obs.obs_source_release(source)
        print("CHeck22")
# ------------------------------------------------------------


def script_properties():
    """
    Called to define user properties associated with the script. These
    properties are used to define how to show settings properties to a user.
    """
    props = obs.obs_properties_create()

    b = obs.obs_properties_add_list(props, "source2", "Text2 Source",
                                    obs.OBS_COMBO_TYPE_EDITABLE,
                                    obs.OBS_COMBO_FORMAT_STRING)
    sources = obs.obs_enum_sources()
    if sources is not None:
        for source2 in sources:
            source2_id = obs.obs_source_get_id(source2)
            if source2_id == "text_gdiplus" or source2_id == "text_ft2_source":
                name2 = obs.obs_source_get_name(source2)
                obs.obs_property_list_add_string(b, name2, name2)

        obs.source_list_release(sources)

    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_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)


    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
    return props



def script_update(settings):
    """
    Called when the script’s settings (if any) have been changed by the user.
    """
    global source_name
    global source2_name

    source_name = obs.obs_data_get_string(settings, "source")
    source2_name = obs.obs_data_get_string(settings, "source2")
Never mind I solved all my problems with writing a new script that has no user interface. Only Problem it always crashes when I load the script into OBS... I will start a new post for that.
 
Top