View Full Version : Python callbacks or persistent loops?

05-22-2012, 02:20 PM
Haven't really played with Python in LightWave yet, but here's my question:

Can I have a persistent plug-in using Python that, say, listens to a TCP/IP stream, then executes a command based on what it receives?

Or can I only write a user triggered event plug-in (i.e. one where the user has to execute the script each time it executes?

Thanks in advance.

05-22-2012, 03:45 PM
Yes, in Layout. If your going to execute native commands most correct and safest bet is to use the Master Plugin class. But you can execute commands from any type of plugin, and other persistent ones, like Motion Modifiers, but it is not 100% supported, should be avoided as they are not meant to issue commands and might do it when LW is not designed to process commands. Both Master class and Motion modifiers are available to Python. Likely you would design the Master plugin to receive the messages in a separate thread, queue them up or whatever, issue a "Refresh" or somesuch from wherever to trigger the master plugins process callback to evaluate (safest place to issue commands), then execute the queued commands. Preferably wait for and check that LW is not in a state where you probably shouldn't issue commands (ex during motion and mesh update). Possibly NT has improved how LW itself queues up commands issued from wherever, so they are executed only when it is safe to do so. Not sure. "Refresh" however is always safe to call to my understanding and experience (but should not be used in process callbacks without very tight control (conditional statements) as it might cause LW to enter an endless loop of processing and refreshing, unless there are controls to prevent it).

For Modeler there is only one persistent type, the custom polygon class, and issuing commands from inside one of those is quite volatile, unsupported and crash-prone, as there is no easy way of identifying what state Modeler is in and issued commands are processed immediately! I don't know if this is available yet for python, I'm betting on no. But you shouldn't and practically can't use it for issuing commands anyway.

05-22-2012, 04:04 PM
Thanks for the detailed feedback. One quick question: how do I check the current state?

05-22-2012, 04:18 PM
The master plugins event callback functions eventCode parameter. See (c/c++) sdk docs for the master plugin class.

05-22-2012, 04:50 PM
Thx. I'll check it out.