1
0

scripting.rst 10 KB

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