1
0

scripting.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. Python/Lua Scripting
  2. ====================
  3. Scripting (21.0+) adds support for Python 3 and Luajit 2 (which is
  4. roughly equivalent to Lua 5.2), allowing the ability to quickly extend,
  5. add features, or automate the program without needing to build a native
  6. plugin module.
  7. Scripting can be accessed in OBS Studio via the Tools menu -> Scripts
  8. option, which will bring up the scripting dialog. Scripts can be added,
  9. removed, and reloaded in real time while the program is running.
  10. **NOTE:** To use Python on Windows or macOS, you must download and install a
  11. Python version that matches your OBS architecture. Then, in the scripting
  12. dialog, you must set the path to the Python install in the "Python Settings" tab.
  13. All API bindings are provided through the **obspython** module in
  14. Python, and the **obslua** module in Lua.
  15. Certain functions have been changed/replaced in order to provide script
  16. callbacks, see :ref:`other_script_differences` for more information.
  17. **WARNING:** Because bindings to the entire API are provided, it is
  18. possible to leak memory or crash the program with an improperly-written
  19. script. Please exercise caution when making scripts and check the
  20. memory leak counter in your log file to make sure scripts you write
  21. aren't leaking memory. **Please treat the API bindings as though you
  22. were writing a C program: read the documentation for functions you use,
  23. and release/destroy objects you reference or create via the API.**
  24. Script Function Exports
  25. -----------------------
  26. There are a number of global functions that scripts can optionally
  27. provide:
  28. .. py:function:: script_description()
  29. Called to retrieve a description string to be displayed to the user
  30. in the Scripts window.
  31. .. py:function:: script_load(settings)
  32. Called on script startup with specific settings associated with the
  33. script. The *settings* parameter provided is not typically used for
  34. settings that are set by the user; instead the parameter is used for
  35. any extra internal settings data that may be used in the script.
  36. :param settings: Settings associated with the script.
  37. .. py:function:: script_unload()
  38. Called when the script is being unloaded.
  39. .. py:function:: script_save(settings)
  40. Called when the script is being saved. This is not necessary for
  41. settings that are set by the user; instead this is used for any
  42. extra internal settings data that may be used in the script.
  43. :param settings: Settings associated with the script.
  44. .. py:function:: script_defaults(settings)
  45. Called to set default settings (if any) associated with the script.
  46. You would typically call :ref:`obs_data_default_funcs` for the
  47. on the settings in order to set its default values.
  48. :param settings: Settings associated with the script.
  49. .. py:function:: script_update(settings)
  50. Called when the script's settings (if any) have been changed by the
  51. user.
  52. :param settings: Settings associated with the script.
  53. .. py:function:: script_properties()
  54. Called to define user properties associated with the script. These
  55. properties are used to define how to show settings properties to a
  56. user.
  57. :return: obs_properties_t object created via
  58. :c:func:`obs_properties_create()`.
  59. .. py:function:: script_tick(seconds)
  60. Called every frame in case per-frame processing is needed. If a
  61. timer is needed, please use :ref:`scripting_timers` instead, as
  62. timers are more efficient if all that's needed is basic timer
  63. functionality. Using this function in Python is not recommended due
  64. to the global interpreter lock of Python.
  65. :param seconds: Seconds passed since previous frame.
  66. Getting the Current Script's Path
  67. ---------------------------------
  68. There is a function you can use to get the current script's path. This
  69. function is automatically implemented in to each script before the
  70. script is loaded, and is part of the script's namespace, not
  71. obslua/obspython:
  72. .. py:function:: script_path()
  73. :return: The path to the script.
  74. .. _scripting_timers:
  75. Script Timers
  76. -------------
  77. Script timers provide an efficient means of providing timer callbacks
  78. without necessarily having to lock scripts/interpreters every frame.
  79. (These functions are part of the obspython/obslua modules/namespaces).
  80. .. py:function:: timer_add(callback, milliseconds)
  81. Adds an timer callback which triggers every *millseconds*.
  82. .. py:function:: timer_remove(callback)
  83. Removes a timer callback. (Note: You can also use
  84. :py:func:`remove_current_callback()` to terminate the timer from the
  85. timer callback)
  86. Script Sources (Lua Only)
  87. -------------------------
  88. It is possible to register sources in Lua. To do so, create a table,
  89. and define its keys the same way you would define an
  90. :c:type:`obs_source_info` structure:
  91. .. code:: lua
  92. local info = {}
  93. info.id = "my_source_id"
  94. info.type = obslua.OBS_SOURCE_TYPE_INPUT
  95. info.output_flags = obslua.OBS_SOURCE_VIDEO
  96. info.get_name = function()
  97. return "My Source"
  98. end
  99. info.create = function(settings, source)
  100. -- typically source data would be stored as a table
  101. local my_source_data = {}
  102. [...]
  103. return my_source_data
  104. end
  105. info.video_render = function(my_source_data, effect)
  106. [...]
  107. end
  108. info.get_width = function(my_source_data)
  109. [...]
  110. -- assuming the source data contains a 'width' key
  111. return my_source_data.width
  112. end
  113. info.get_height = function(my_source_data)
  114. [...]
  115. -- assuming the source data contains a 'height' key
  116. return my_source_data.height
  117. end
  118. -- register the source
  119. obs_register_source(info)
  120. .. _other_script_differences:
  121. Other Differences From the C API
  122. --------------------------------
  123. Certain functions are implemented differently from the C API due to how
  124. callbacks work. (These functions are part of the obspython/obslua
  125. modules/namespaces).
  126. .. py:function:: obs_enum_sources()
  127. Enumerates all sources.
  128. :return: An array of reference-incremented sources. Release with
  129. :py:func:`source_list_release()`.
  130. .. py:function:: obs_scene_enum_items(scene)
  131. Enumerates scene items within a scene.
  132. :param scene: obs_scene_t object to enumerate items from.
  133. :return: List of scene items. Release with
  134. :py:func:`sceneitem_list_release()`.
  135. .. py:function:: obs_sceneitem_group_enum_items(group)
  136. Enumerates scene items within a group.
  137. :param group: obs_sceneitem_t object to enumerate items from.
  138. :return: List of scene items. Release with
  139. :py:func:`sceneitem_list_release()`.
  140. .. py:function:: obs_add_main_render_callback(callback)
  141. **Lua only:** Adds a primary output render callback. This callback
  142. has no parameters.
  143. :param callback: Render callback. Use
  144. :py:func:`obs_remove_main_render_callback()` or
  145. :py:func:`remove_current_callback()` to remove the
  146. callback.
  147. .. py:function:: obs_remove_main_render_callback(callback)
  148. **Lua only:** Removes a primary output render callback.
  149. :param callback: Render callback.
  150. .. py:function:: signal_handler_connect(handler, signal, callback)
  151. Adds a callback to a specific signal on a signal handler. This
  152. callback has one parameter: the calldata_t object.
  153. :param handler: A signal_handler_t object.
  154. :param signal: The signal on the signal handler (string)
  155. :param callback: The callback to connect to the signal. Use
  156. :py:func:`signal_handler_disconnect()` or
  157. :py:func:`remove_current_callback()` to remove the
  158. callback.
  159. .. py:function:: signal_handler_disconnect(handler, signal, callback)
  160. Removes a callback from a specific signal of a signal handler.
  161. :param handler: A signal_handler_t object.
  162. :param signal: The signal on the signal handler (string)
  163. :param callback: The callback to disconnect from the signal.
  164. .. py:function:: signal_handler_connect_global(handler, callback)
  165. Adds a global callback to a signal handler. This callback has two
  166. parameters: the first parameter is the signal string, and the second
  167. parameter is the calldata_t object.
  168. :param handler: A signal_handler_t object.
  169. :param callback: The callback to connect. Use
  170. :py:func:`signal_handler_disconnect_global()` or
  171. :py:func:`remove_current_callback()` to remove the
  172. callback.
  173. .. py:function:: signal_handler_disconnect_global(handler, callback)
  174. Removes a global callback from a signal handler.
  175. :param handler: A signal_handler_t object.
  176. :param callback: The callback to disconnect.
  177. .. py:function:: obs_hotkey_register_frontend(name, description, callback)
  178. Adds a frontend hotkey. The callback takes one parameter: a boolean
  179. 'pressed' parameter.
  180. :param name: Unique name identifier string of the hotkey.
  181. :param description: Hotkey description shown to the user.
  182. :param callback: Callback for the hotkey. Use
  183. :py:func:`obs_hotkey_unregister()` or
  184. :py:func:`remove_current_callback()` to remove
  185. the callback.
  186. .. py:function:: obs_hotkey_unregister(callback)
  187. Unregisters the hotkey associated with the specified callback.
  188. :param callback: Callback of the hotkey to unregister.
  189. .. py:function:: obs_properties_add_button(properties, setting_name, text, callback)
  190. Adds a button property to an obs_properties_t object. The callback
  191. takes two parameters: the first parameter is the obs_properties_t
  192. object, and the second parameter is the obs_property_t for the
  193. button.
  194. :param properties: An obs_properties_t object.
  195. :param setting_name: A setting identifier string.
  196. :param text: Button text.
  197. :param callback: Button callback. This callback is automatically
  198. cleaned up.
  199. .. py:function:: remove_current_callback()
  200. Removes the current callback being executed. Does nothing if not
  201. within a callback.
  202. .. py:function:: source_list_release(source_list)
  203. Releases the references of a source list.
  204. :param source_list: Array of sources to release.
  205. .. py:function:: sceneitem_list_release(item_list)
  206. Releases the references of a scene item list.
  207. :param item_list: Array of scene items to release.
  208. .. py:function:: calldata_source(calldata, name)
  209. Casts a pointer parameter of a calldata_t object to an obs_source_t
  210. object.
  211. :param calldata: A calldata_t object.
  212. :param name: Name of the parameter.
  213. :return: A borrowed reference to an obs_source_t object.
  214. .. py:function:: calldata_sceneitem(calldata, name)
  215. Casts a pointer parameter of a calldata_t object to an
  216. obs_sceneitem_t object.
  217. :param calldata: A calldata_t object.
  218. :param name: Name of the parameter.
  219. :return: A borrowed reference to an obs_sceneitem_t object.