2024 1.0 Help

Anatomy of a LightWave Python Plugin

Python 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++).

‘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.

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.

09 December 2024