Anatomy of a LightWave Python Plugin
Last modified: 10 March 2025Python plug-ins using the first-tier integration (see Introduction are built directly atop the LightWave SDK. This means that, in the Python environment, LightWave plug-ins are constructed in a fashion that is quite similar to that of native code plug-ins (C/C++).
warning
An understanding of the design and intent of the LightWave SDK – and the LightWave plug-in architecture in general – would likely be of great assistance to you going forward. Before proceeding through this documentation, it is highly recommended that you first cover those introductory documents provided with the LightWave SDK.
‘Hello, Worlds!’
Before we delve exclusively into the realm of Python, let’s see a brief comparison of a LightWave plug-in written in standard C to one that performs the same action written purely in Python.
note
Do not be too concerned at the moment about the actual details of either example. This is intended as an overall comparison in terms of amount of code.
We shall use the time-honoured Hello, World example for our purposes to keep the code as short as possible, and we’ll use the Generic Class
as our functional plug-in architecture.
‘Hello’ - C
In the following C example, you have what is probably the shortest LightWave plug-in possible (of course, this could be a bit longer, but we have omitted unnecessary elements, like comments, for the sake of the example). All it does is post a prefabricated informational dialog with the text ‘Hello, World!’ displayed.
#include <lwserver.h>
#include <lwgeneric.h>
#include <lwhost.h>
XCALL_(int) HelloGeneric(int version, GlobalFunc *global, LWLayoutGeneric *local, void *serverData)
{
LWMessageFuncs *msg;
if(version != LWLAYOUTGENERIC_VERSION)
return AFUNC_BADVERSION;
msg = (*global)(LWMESSAGEFUNCS_GLOBAL, GFUSE_TRANSIENT);
if(!msg)
return AFUNC_BADGLOBAL;
(*msg->info)("Hello, World!", NULL);
return AFUNC_OK;
}
ServerRecord ServerDesc[] =
{
{ LWLAYOUTGENERIC_CLASS, "HelloWorld", HelloGeneric },
{ NULL }
}
While the individual lines may seem like Greek, it does illustrate the minimum amount of code required to actually produce a LightWave plug-in (usefulness notwithstanding).
'Hello' - Python
The corresponding LightWave Python plug-in would look like this (as with the C example, only the minimum elements that absolutely must be present for the script to function are included):
import lwsdk
__lwver__ = "11"
class hello_world(lwsdk.IGeneric):
def __init__(self, context):
super(hello_world, self).__init__()
def process(self, generic_access):
lwsdk.LWMessageFuncs().info("Hello, World!","")
return lwsdk.AFUNC_OK
ServerTagInfo = [
( "Python Hello World", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Hello, World!", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]
ServerRecord = { lwsdk.GenericFactory("LW_PyHelloWorld", hello_world) : ServerTagInfo }
As you can see, in terms of lines of code, the Python version is on par with that of the C version. Most elements are Python-specific in nature, relating more to the idioms of the language.
But now, let’s examine a full-blown LightWave Python plug-in in all its gory details. Click here.
note
Contributed to by Yannick Tholomier (TAIS)