View Full Version : Selecting polys and points etc..

01-12-2014, 06:22 AM
So I've created my first plugin - which does nothing more that makes a box using the 'makebox' command. The next step will be to create another box and move it on top of the first, which I think I can handle. But after that I would like to select the uppermost polygon from the second box. I can see there are commands for selecting polys, but they require an ID. How do I find the ID for the poly that has the maximum Y value? Do I need to iterate through all of them returning their ID's until I find the one with the max Y?


01-16-2014, 05:52 PM

Had a little think and thought that using the bounding box and selecting based on the polygon whose points were all within the limits of a box might be better than looping through all the points and polygons to get ids. Came up with this little bit of lscript that might give you some ideas for python. I'll have a go myself when I get a little bit of time.

Steps are find the bounding box for the layer, set the lower limit and upper limit for a volume based on values from the bounding box(in this case taking the upper limit and doing a +/- on the y), then only select those polygons which have all their points within the limits that you set.



@version 2.8
@script modeler

// debug();
buf = boundingbox();
v1 = buf[2];
v2 = buf[1];
s = string(v1);
sv2 = string(v2);
token = parse(" ", s);
t2 = parse(" ", sv2);
v1y = token[2];
v1y1 = number(v1y) + 0.1;
sv1y1 = string(v1y1);
v1y2 = number(v1y) - 0.01;
sv1y2 = string(v1y2);
s1 = string(token[1]);
s2 = string(token[3]);
s3 = string(t2[1]);
s4 = string(t2[3]);
lvec = s3 + " " + sv1y2 + " " + s4;
hvec = s1 + " " + sv1y1 + " " + s2;
vlow = vector(lvec);
vhigh = vector(hvec);
selpolygon(SET,VOLEXCL,vlow, vhigh);

01-16-2014, 06:50 PM
Thanks Iain. I too had started to think along these lines, but didn't really know how I should start. So thanks for code, it will help when thinking through the logic.

01-19-2014, 10:58 AM
Hi, here's a python version, just save a sel_poly.py



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

This is a LightWave Command Sequence plug-in (Modeler) that creates
a couple of boxes and selects the upper most polygon.

import sys
import lwsdk
from lwsdk import Vector

__author__ = "Iain"
__date__ = "Jan 18 2014"
__copyright__ = ""
__version__ = "1.0"
__maintainer__ = "Iain"
__email__ = ""
__status__ = "Example"
__lwver__ = "11"

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

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

cs_options = lwsdk.marshall_dynavalues(([-0.5, -0.5, -0.5], [0.5, 0.5, 0.5], None))
cs_makebox = mod_command.lookup(mod_command.data, "MAKEBOX")
result, dyna_value = mod_command.execute(mod_command.data, cs_makebox, cs_options, lwsdk.OPSEL_USER)

cs_options = lwsdk.marshall_dynavalues(([-0.5, 0.5, -0.5], [0.5, 1.5, 0.5], None))
cs_makebox = mod_command.lookup(mod_command.data, "MAKEBOX")
result, dyna_value = mod_command.execute(mod_command.data, cs_makebox, cs_options, lwsdk.OPSEL_USER)

bb = lwsdk.LWStateQueryFuncs().bbox(lwsdk.OPLYR_FG)
b1 = bb[1]
y1 = b1[3] - 0.01
lvec = lwsdk.Vector()
lvec.x = b1[0]
lvec.y = y1
lvec.z = b1[4]
hvec = lwsdk.Vector()
hvec.x = b1[1]
hvec.y = b1[3]
hvec.z = b1[5]

cs_options = lwsdk.marshall_dynavalues(("SET", "VOLEXCL", lvec, hvec))
cs_selpolygon = mod_command.lookup(mod_command.data, "SEL_POLYGON")
result, dyna_value = mod_command.execute(mod_command.data, cs_selpolygon, cs_options, lwsdk.OPSEL_USER)
if result != 0:
print "Unable to select polygon"

return lwsdk.AFUNC_OK

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

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PySelPoly", sel_poly) : ServerTagInfo }

01-19-2014, 11:09 AM
He great thanks for this! I managed to get as far as returning the bbox tuple, but then wasn't sure how to pass the values through to the volume selection.

I've been trying to execute commands with the evaluate method though as it appeared to be simpler, but so far didn't get it to work. Couldn't find any sample scripts to help either. I'll go with your version. Next up, see if I can perform some editing on the selected polygon :)

01-19-2014, 12:04 PM
I used the SDK to get the lscript to work, also if you have 11.6 installed look in the PRIS directory at the init py file. There are some examples in the PRIS directory but I couldn't get them to work. So I ran this using 11.5. Hardest part was remembering how to convert the values back into vectors before passing as parameters, and rembering that arrays start at 0 in python and not 1 as in lscript.



01-19-2014, 05:31 PM
Thanks for the tips. There still a lot I don't understand and find it quite difficult to interpret the lwsdk. Still, I remember it being like this when I first started with t-SQL. It's an engaging challenge!

01-21-2014, 01:15 PM
No problem, yes the SDK can be a little bit challenging when trying to find anything. Things don't always appear in the section that you think they should. Good luck with the next steps.