# Made by DiaTech
# Discord: DiaTech#2363
# Email: mail@diatech.tv

import obspython as obs

# Function 'change_name' is the main function of this file.
def change_name(prop, props):
  global choose_scene
  global new_name_string

  # Scene = the scene chosen in the dropdown list of 'script_properties'
  # We get the source of the scene of which we are trying to change the name of
  # We also get the settings of the source to force update it
  scene = obs.obs_get_scene_by_name(choose_scene)
  scene_source = obs.obs_scene_get_source(scene)
  source_settings = obs.obs_source_get_settings(scene_source)
  
  # Setting the name of the source to the new name typed into the text box of 'script_properties'
  obs.obs_source_set_name(scene_source, new_name_string)

  # Update source
  obs.obs_source_update(scene_source, source_settings)
  
  # Print to script log for troubleshooting
  print(choose_scene)
  print(new_name_string)
  
  # Release stuff used
  obs.obs_scene_release(scene)
  obs.obs_source_release(scene_source)
  obs.obs_data_release(source_settings)

# Description of script
def script_description():
  return """
   Changes name of scene to a new specified name.
  """
  
# Base script settings
def script_update(settings):
  global choose_scene
  global new_name_string
  choose_scene = obs.obs_data_get_string(settings, "choose_scene")
  new_name_string = obs.obs_data_get_string(settings, "new_scene_name")
  
# Properties of script. Will be displayed in OBS under "Scripts" after adding it.
def script_properties():
    # Creating properties, getting all scene names
    props = obs.obs_properties_create()
    scenes = obs.obs_frontend_get_scene_names()
    
    # Show a list of current scenes and allows user to pick which scene they want to change the name of
    scene_select = obs.obs_properties_add_list(props, "choose_scene", "Change name of which scene? ", obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    for scene in scenes:
        obs.obs_property_list_add_string(scene_select, scene, scene)
        
    # Gets the new name to be used for the scene selected
    new_name = obs.obs_properties_add_text(props, "new_scene_name", "Change name to: ", obs.OBS_TEXT_DEFAULT)
    
    # Adds button that will change the name of the scene to the desired name. Calls the function 'change_name'
    obs.obs_properties_add_button(props, "button", "Rename", change_name)
    
    return props