scripting.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 *milliseconds*.
  82. Note: Using instance methods as callbacks is not supported. Always
  83. use module methods.
  84. .. py:function:: timer_remove(callback)
  85. Removes a timer callback. (Note: You can also use
  86. :py:func:`remove_current_callback()` to terminate the timer from the
  87. timer callback)
  88. Script Sources (Lua Only)
  89. -------------------------
  90. It is possible to register sources in Lua. To do so, create a table,
  91. and define its keys the same way you would define an
  92. :c:type:`obs_source_info` structure:
  93. .. code:: lua
  94. local info = {}
  95. info.id = "my_source_id"
  96. info.type = obslua.OBS_SOURCE_TYPE_INPUT
  97. info.output_flags = obslua.OBS_SOURCE_VIDEO
  98. info.get_name = function()
  99. return "My Source"
  100. end
  101. info.create = function(settings, source)
  102. -- typically source data would be stored as a table
  103. local my_source_data = {}
  104. [...]
  105. return my_source_data
  106. end
  107. info.video_render = function(my_source_data, effect)
  108. [...]
  109. end
  110. info.get_width = function(my_source_data)
  111. [...]
  112. -- assuming the source data contains a 'width' key
  113. return my_source_data.width
  114. end
  115. info.get_height = function(my_source_data)
  116. [...]
  117. -- assuming the source data contains a 'height' key
  118. return my_source_data.height
  119. end
  120. -- register the source
  121. obs_register_source(info)
  122. .. _other_script_differences:
  123. Other Differences From the C API
  124. --------------------------------
  125. Certain functions are implemented differently from the C API due to how
  126. callbacks work. (These functions are part of the obspython/obslua
  127. modules/namespaces).
  128. .. py:function:: obs_enum_sources()
  129. Enumerates all sources.
  130. :return: An array of reference-incremented sources. Release with
  131. :py:func:`source_list_release()`.
  132. .. py:function:: obs_scene_enum_items(scene)
  133. Enumerates scene items within a scene.
  134. :param scene: obs_scene_t object to enumerate items from.
  135. :return: List of scene items. Release with
  136. :py:func:`sceneitem_list_release()`.
  137. .. py:function:: obs_sceneitem_group_enum_items(group)
  138. Enumerates scene items within a group.
  139. :param group: obs_sceneitem_t object to enumerate items from.
  140. :return: List of scene items. Release with
  141. :py:func:`sceneitem_list_release()`.
  142. .. py:function:: obs_add_main_render_callback(callback)
  143. **Lua only:** Adds a primary output render callback. This callback
  144. has no parameters.
  145. :param callback: Render callback. Use
  146. :py:func:`obs_remove_main_render_callback()` or
  147. :py:func:`remove_current_callback()` to remove the
  148. callback.
  149. .. py:function:: obs_remove_main_render_callback(callback)
  150. **Lua only:** Removes a primary output render callback.
  151. :param callback: Render callback.
  152. .. py:function:: signal_handler_connect(handler, signal, callback)
  153. Adds a callback to a specific signal on a signal handler. This
  154. callback has one parameter: the calldata_t object.
  155. :param handler: A signal_handler_t object.
  156. :param signal: The signal on the signal handler (string)
  157. :param callback: The callback to connect to the signal. Use
  158. :py:func:`signal_handler_disconnect()` or
  159. :py:func:`remove_current_callback()` to remove the
  160. callback.
  161. .. py:function:: signal_handler_disconnect(handler, signal, callback)
  162. Removes a callback from a specific signal of a signal handler.
  163. :param handler: A signal_handler_t object.
  164. :param signal: The signal on the signal handler (string)
  165. :param callback: The callback to disconnect from the signal.
  166. .. py:function:: signal_handler_connect_global(handler, callback)
  167. Adds a global callback to a signal handler. This callback has two
  168. parameters: the first parameter is the signal string, and the second
  169. parameter is the calldata_t object.
  170. :param handler: A signal_handler_t object.
  171. :param callback: The callback to connect. Use
  172. :py:func:`signal_handler_disconnect_global()` or
  173. :py:func:`remove_current_callback()` to remove the
  174. callback.
  175. .. py:function:: signal_handler_disconnect_global(handler, callback)
  176. Removes a global callback from a signal handler.
  177. :param handler: A signal_handler_t object.
  178. :param callback: The callback to disconnect.
  179. .. py:function:: obs_hotkey_register_frontend(name, description, callback)
  180. Adds a frontend hotkey. The callback takes one parameter: a boolean
  181. 'pressed' parameter.
  182. :param name: Unique name identifier string of the hotkey.
  183. :param description: Hotkey description shown to the user.
  184. :param callback: Callback for the hotkey. Use
  185. :py:func:`obs_hotkey_unregister()` or
  186. :py:func:`remove_current_callback()` to remove
  187. the callback.
  188. .. py:function:: obs_hotkey_unregister(callback)
  189. Unregisters the hotkey associated with the specified callback.
  190. :param callback: Callback of the hotkey to unregister.
  191. .. py:function:: obs_properties_add_button(properties, setting_name, text, callback)
  192. Adds a button property to an obs_properties_t object. The callback
  193. takes two parameters: the first parameter is the obs_properties_t
  194. object, and the second parameter is the obs_property_t for the
  195. button.
  196. :param properties: An obs_properties_t object.
  197. :param setting_name: A setting identifier string.
  198. :param text: Button text.
  199. :param callback: Button callback. This callback is automatically
  200. cleaned up.
  201. .. py:function:: remove_current_callback()
  202. Removes the current callback being executed. Does nothing if not
  203. within a callback.
  204. .. py:function:: source_list_release(source_list)
  205. Releases the references of a source list.
  206. :param source_list: Array of sources to release.
  207. .. py:function:: sceneitem_list_release(item_list)
  208. Releases the references of a scene item list.
  209. :param item_list: Array of scene items to release.
  210. .. py:function:: calldata_source(calldata, name)
  211. Casts a pointer parameter of a calldata_t object to an obs_source_t
  212. object.
  213. :param calldata: A calldata_t object.
  214. :param name: Name of the parameter.
  215. :return: A borrowed reference to an obs_source_t object.
  216. .. py:function:: calldata_sceneitem(calldata, name)
  217. Casts a pointer parameter of a calldata_t object to an
  218. obs_sceneitem_t object.
  219. :param calldata: A calldata_t object.
  220. :param name: Name of the parameter.
  221. :return: A borrowed reference to an obs_sceneitem_t object.