How to set target window in window source via lua

MaZy

New Member
Hello, I am trying to make a lua script which allows me to use a hotkey and change the target window of the window source.

Example I have window capture and I selected notepad. It will capture and show notepad window. Now on desktop I change focus chrome. If I press the hotkey so the source should change to the current window as target window. In this case it should show chrome now instead of notepad.

Code:
function OnHotkeyChangeState(pressed)
    if not pressed then
        return false
    end
  
    if not hasSource then
        return false
    end
  
    local source = obs.obs_get_source_by_name(select_window_source)
    if source ~= nil then
        if obs.obs_source_get_id(source) == "window_capture" then
            local properties = obs.obs_source_properties(source)
            local windowProp = obs.obs_properties_get(properties, "window")
          
            if windowProp ~= nil then
                print(obs.obs_property_list_item_name(windowProp, 0))
                local settings = obs.obs_source_get_settings(source)
                obs.obs_properties_apply_settings(properties, settings)
              
            end
        end
    end
    obs.obs_source_release(source)
end

OnHotkeyChangeState will get called if I use the hotkey. But after the line with "if windowProp ~= nil then" I am not what can I do. I only find out that the obs_property_list_item_name id 0 will have always the current focused window. So I am missing something like "select and apply this". I found only apply but it is not working or maybe I did something wrong there.
 

MaZy

New Member
I managed to update. It shows now in window capture the target window but but does not render. It stills renders the old window. Feels like it changes the target window but there need an internal update. Like "render this now" or "restart rendering". Example if I check or uncheck capture cursor then suddently it shows the current window in the preview.

Code:
function OnHotkeyChangeState(pressed)
    if not pressed then
        return false
    end
   
    if not hasSource then
        return false
    end
   
    local source = obs.obs_get_source_by_name(select_window_source)
    if source ~= nil then
        if obs.obs_source_get_id(source) == "window_capture" then
       
            local properties = obs.obs_source_properties(source)
            local windowProp = obs.obs_properties_get(properties, "window")
            local propName = obs.obs_property_name(windowProp)
           
            if windowProp ~= nil then
           
                local name = get_first_enabled(windowProp)
               
                if name ~= nil then
                    local settings = obs.obs_source_get_settings(source)
                    obs.obs_data_set_string(settings,propName, name)
                    print(obs.obs_data_get_string(settings,propName))
                    obs.obs_properties_apply_settings(properties, settings)
                    print(name)
                    obs.obs_data_release(settings)
                end
               
            end
        end
    end
    obs.obs_source_release(source)
end

function get_first_enabled(windowProp)
    local size = obs.obs_property_list_item_count(windowProp)
    for i = 0, size do
        if not obs.obs_property_list_item_disabled(windowProp, i) then
            return obs.obs_property_list_item_string(windowProp, i)
        end
    end
    return nil
end
 

MaZy

New Member
Ok could finally make it work.

Code:
function OnHotkeyChangeState(pressed)
    if not pressed then
        return false
    end
    
    if not hasSource then
        return false
    end
    
    local source = obs.obs_get_source_by_name(select_window_source)
    if source ~= nil then
        if obs.obs_source_get_id(source) == "window_capture" then
        
            local properties = obs.obs_source_properties(source)
            local windowProp = obs.obs_properties_get(properties, "window")
            local propName = obs.obs_property_name(windowProp)
            
            if windowProp ~= nil then
            
                local name = get_first_enabled(windowProp)
                
                if name ~= nil then
                    local settings = obs.obs_source_get_settings(source)
                    obs.obs_data_set_string(settings,propName, name)
                    obs.obs_properties_apply_settings(properties, settings)
                    obs.obs_source_update(source, settings)
                    obs.obs_data_release(settings)
                    obs.obs_transition_get_source(source, 1)
                    
                end
                
            end
            
            obs.obs_properties_destroy(properties)
            
            obs.obs_source_set_enabled(source, false)
            obs.obs_source_set_enabled(source, true)
        end
        obs.obs_source_release(source)
    end

end

function get_first_enabled(windowProp)
    local size = obs.obs_property_list_item_count(windowProp)
    for i = 0, size do
        if not obs.obs_property_list_item_disabled(windowProp, i) then
            return obs.obs_property_list_item_string(windowProp, i)
        end
    end
    return nil
end
 
Top