Writing a client
A client for a new application is a small subclass of NyxClient. Existing ones to read:
nyx-bridge-blender and
nyx-bridge-maya.
from nyx_bridge.client import NyxClient
class HoudiniClient(NyxClient):
CLIENT_KIND = "houdini"
def client_version(self) -> str:
import hou
return hou.applicationVersionString()
def default_namespace(self) -> dict:
import hou
return {"hou": hou}
def run_on_main_thread(self, func):
...
Checklist
CLIENT_KIND: unique and lowercase; servers filter with it (find_clients(kind=)).default_namespace: the modules scripts will expect, imported lazily inside the method so importing your package works outside the application.run_on_main_thread: if the application can't be used from other threads, put the function on a queue drained by the application's own timer or idle callback, and block until it ran. Return its value and re-raise what it raised only if you need to;executealready catches exceptions and builds the error message, so the function passed in never raises for user code.- Where to start it:
start_in_thread()from the application's startup (plug-in load, add-onregister),stop()from its shutdown. In a headless or batch mode with no event loop, callserve_forever()and skiprun_on_main_thread. - Dependencies: install
sowwic-nyx-bridgewith the application's own Python; keep it the only requirement.
Things to watch
- Never block the main thread waiting for the server.
start_in_threadkeeps the connecting and retrying off it. - A request can run for a long time. The client answers nothing else meanwhile (see Architecture), so avoid long executions on a client that has to stay responsive.
- Cleanup.
on_disconnectedis the place to drop references to application objects created by scripts. The namespace is replaced on reconnect anyway. - Thread safety of captured output.
redirect_stdoutis process wide.