View Full Version : LW11 Python example: Select Connected Polygons in Modeler

09-19-2012, 07:21 PM
It took me a long time to figure this out, so I'm posting this LW11 Python code here in hopes of cutting down on someone else's learning curve.

Usage: select a polygon on a mesh and run this script to execute a Select Connected operation on the polygons.

#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

import lwsdk

__author__ = "Jennifer Hachigian"
__date__ = "September 19, 2012"
__copyright__ = ""
__version__ = "1.0"
__maintainer__ = "Jennifer Hachigian"
__email__ = "[email protected]"
__status__ = "Select Connected Example"
__lwver__ = "11"

class selectConnected(lwsdk.ICommandSequence):
def __init__(self, context):
super(selectConnected, self).__init__()

# LWCommandSequence -----------------------------------
def process(self, mod_command):

command = mod_command.lookup(mod_command.data, "SEL_POLYGON")
cs_options = lwsdk.marshall_dynavalues(("SET", "CONNECT"))
result = mod_command.execute(mod_command.data, command, cs_options, lwsdk.OPSEL_USER)

return lwsdk.AFUNC_OK

ServerTagInfo = [
( "Python Select Connected", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Select Connected", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PyRenameSurfaces", selectConnected) : ServerTagInfo }

"mod_command.lookup" converts "SEL_POLYGON" into an integer that "mod_command.execute" understands as its second argument.

"lwsdk.marshall_dynavalues" converts the tuple into a list of arguments that "mod_command.execute" understands as its third argument. "SET" means add to the existing selection; "CONNECT" works in conjunction with "SET." The opposite of "SET" ("select") is "CLEAR" ("deselect") -- and the C SDK indicates that "CLEAR" does not work with the "CONNECT" option.

"mod_command.execute" executes the actual Select Connected command. The first argument is the mesh data. The second argument is the command, the third are the options, and the fourth is the mode. A "Select Connected" command must be executed in USER mode for it to work ("lwsdk.OPSEL_USER").

LScript equivalent:

// LScript defines ...

@script modeler
@version 2.3
@name "Select Connected Polygons"

selmode(USER); // scripts can't select data outside of USER mode
selpolygon( SET, CONNECT ); // select connected

09-19-2012, 07:39 PM
Awesome! Thanks for sharing.