scripting.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_load(settings)
  29. Called on script startup with specific settings associated with the
  30. script. The *settings* parameter provided is not typically used for
  31. settings that are set by the user; instead the parameter is used for
  32. any extra internal settings data that may be used in the script.
  33. :param settings: Settings associated with the script.
  34. .. py:function:: script_unload()
  35. Called when the script is being unloaded.
  36. .. py:function:: script_save(settings)
  37. Called when the script is being saved. This is not necessary for
  38. settings that are set by the user; instead this is used for any
  39. extra internal settings data that may be used in the script.
  40. :param settings: Settings associated with the script.
  41. .. py:function:: script_defaults(settings)
  42. Called to set default settings (if any) associated with the script.
  43. You would typically call :ref:`obs_data_default_funcs` for the
  44. on the settings in order to set its default values.
  45. :param settings: Settings associated with the script.
  46. .. py:function:: script_update(settings)
  47. Called when the script's settings (if any) have been changed by the
  48. user.
  49. :param settings: Settings associated with the script.
  50. .. py:function:: script_properties()
  51. Called to define user properties associated with the script. These
  52. properties are used to define how to show settings properties to a
  53. user.
  54. :return: obs_properties_t object created via
  55. :c:func:`obs_properties_create()`.
  56. .. py:function:: script_tick(seconds)
  57. Called every frame in case per-frame processing is needed. If a
  58. timer is needed, please use :ref:`scripting_timers` instead, as
  59. timers are more efficient if all that's needed is basic timer
  60. functionality. Using this function in Python is not recommended due
  61. to the global interpreter lock of Python.
  62. :param seconds: Seconds passed since previous frame.
  63. Getting the Current Script's Path
  64. ---------------------------------
  65. There is a function you can use to get the current script's path. This
  66. function is automatically implemented in to each script before the
  67. script is loaded, and is part of the script's namespace, not
  68. obslua/obspython:
  69. .. py:function:: script_path()
  70. :return: The path to the script.
  71. .. _scripting_timers:
  72. Script Timers
  73. -------------
  74. Script timers provide an efficient means of providing timer callbacks
  75. without necessarily having to lock scripts/interpreters every frame.
  76. (These functions are part of the obspython/obslua modules/namespaces).
  77. .. py:function:: timer_add(callback, milliseconds)
  78. Adds an timer callback which triggers every *millseconds*.
  79. .. py:function:: timer_remove(callback)
  80. Removes a timer callback. (Note: You can also use
  81. :py:func:`remove_current_callback()` to terminate the timer from the
  82. timer callback)
  83. Script Sources (Lua Only)
  84. -------------------------
  85. It is possible to register sources in Lua. To do so, create a table,
  86. and define its keys the same way you would define an
  87. :c:type:`obs_source_info` structure:
  88. .. code:: lua
  89. local info = {}
  90. info.id = "my_source_id"
  91. info.type = obslua.OBS_SOURCE_TYPE_INPUT
  92. info.output_flags = obslua.OBS_SOURCE_VIDEO
  93. info.get_name = function()
  94. return "My Source"
  95. end
  96. info.create = function(settings, source)
  97. -- typically source data would be stored as a table
  98. local my_source_data = {}
  99. [...]
  100. return my_source_data
  101. end
  102. info.video_render = function(my_source_data, effect)
  103. [...]
  104. end
  105. info.get_width = function(my_source_data)
  106. [...]
  107. -- assuming the source data contains a 'width' key
  108. return my_source_data.width
  109. end
  110. info.get_height = function(my_source_data)
  111. [...]
  112. -- assuming the source data contains a 'height' key
  113. return my_source_data.height
  114. end
  115. -- register the source
  116. obs_register_source(info)
  117. .. _other_script_differences:
  118. Other Differences From the C API
  119. --------------------------------
  120. Certain functions are implemented differently from the C API due to how
  121. callbacks work. (These functions are part of the obspython/obslua
  122. modules/namespaces).
  123. .. py:function:: obs_enum_sources()
  124. Enumerates all sources.
  125. :return: An array of reference-incremented sources. Release with
  126. :py:func:`source_list_release()`.
  127. .. py:function:: obs_scene_enum_items(scene)
  128. Enumerates scene items within a scene.
  129. :param scene: obs_scene_t object to enumerate items from.
  130. :return: List of scene items. Release with
  131. :py:func:`sceneitem_list_release()`.
  132. .. py:function:: obs_sceneitem_group_enum_items(group)
  133. Enumerates scene items within a group.
  134. :param group: obs_sceneitem_t object to enumerate items from.
  135. :return: List of scene items. Release with
  136. :py:func:`sceneitem_list_release()`.
  137. .. py:function:: obs_add_main_render_callback(callback)
  138. **Lua only:** Adds a primary output render callback. This callback
  139. has no parameters.
  140. :param callback: Render callback. Use
  141. :py:func:`obs_remove_main_render_callback()` or
  142. :py:func:`remove_current_callback()` to remove the
  143. callback.
  144. .. py:function:: obs_remove_main_render_callback(callback)
  145. **Lua only:** Removes a primary output render callback.
  146. :param callback: Render callback.
  147. .. py:function:: signal_handler_connect(handler, signal, callback)
  148. Adds a callback to a specific signal on a signal handler. This
  149. callback has one parameter: the calldata_t object.
  150. :param handler: A signal_handler_t object.
  151. :param signal: The signal on the signal handler (string)
  152. :param callback: The callback to connect to the signal. Use
  153. :py:func:`signal_handler_disconnect()` or
  154. :py:func:`remove_current_callback()` to remove the
  155. callback.
  156. .. py:function:: signal_handler_disconnect(handler, signal, callback)
  157. Removes a callback from a specific signal of a signal handler.
  158. :param handler: A signal_handler_t object.
  159. :param signal: The signal on the signal handler (string)
  160. :param callback: The callback to disconnect from the signal.
  161. .. py:function:: signal_handler_connect_global(handler, callback)
  162. Adds a global callback to a signal handler. This callback has two
  163. parameters: the first parameter is the signal string, and the second
  164. parameter is the calldata_t object.
  165. :param handler: A signal_handler_t object.
  166. :param callback: The callback to connect. Use
  167. :py:func:`signal_handler_disconnect_global()` or
  168. :py:func:`remove_current_callback()` to remove the
  169. callback.
  170. .. py:function:: signal_handler_disconnect_global(handler, callback)
  171. Removes a global callback from a signal handler.
  172. :param handler: A signal_handler_t object.
  173. :param callback: The callback to disconnect.
  174. .. py:function:: obs_hotkey_register_frontend(name, description, callback)
  175. Adds a frontend hotkey. The callback takes one parameter: a boolean
  176. 'pressed' parameter.
  177. :param name: Unique name identifier string of the hotkey.
  178. :param description: Hotkey description shown to the user.
  179. :param callback: Callback for the hotkey. Use
  180. :py:func:`obs_hotkey_unregister()` or
  181. :py:func:`remove_current_callback()` to remove
  182. the callback.
  183. .. py:function:: obs_hotkey_unregister(callback)
  184. Unregisters the hotkey associated with the specified callback.
  185. :param callback: Callback of the hotkey to unregister.
  186. .. py:function:: obs_properties_add_button(properties, setting_name, text, callback)
  187. Adds a button property to an obs_properties_t object. The callback
  188. takes two parameters: the first parameter is the obs_properties_t
  189. object, and the second parameter is the obs_property_t for the
  190. button.
  191. :param properties: An obs_properties_t object.
  192. :param setting_name: A setting identifier string.
  193. :param text: Button text.
  194. :param callback: Button callback. This callback is automatically
  195. cleaned up.
  196. .. py:function:: remove_current_callback()
  197. Removes the current callback being executed. Does nothing if not
  198. within a callback.
  199. .. py:function:: source_list_release(source_list)
  200. Releases the references of a source list.
  201. :param source_list: Array of sources to release.
  202. .. py:function:: sceneitem_list_release(item_list)
  203. Releases the references of a scene item list.
  204. :param item_list: Array of scene items to release.
  205. .. py:function:: calldata_source(calldata, name)
  206. Casts a pointer parameter of a calldata_t object to an obs_source_t
  207. object.
  208. :param calldata: A calldata_t object.
  209. :param name: Name of the parameter.
  210. :return: A borrowed reference to an obs_source_t object.
  211. .. py:function:: calldata_sceneitem(calldata, name)
  212. Casts a pointer parameter of a calldata_t object to an
  213. obs_sceneitem_t object.
  214. :param calldata: A calldata_t object.
  215. :param name: Name of the parameter.
  216. :return: A borrowed reference to an obs_sceneitem_t object.