PDA

View Full Version : Create a square



clintonman
02-29-2012, 11:25 PM
This is my first attempt at trying to understand the new Python scripting. It's a modeler script that creates a square polygon and assigns values for uv, weight and vertex color maps. I tried to find a way to set the actual surface color, but couldn't see how to do it.


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

"""
This is a LightWave Command Sequence plug-in (Modeler) that creates
a square.
"""

import sys
import lwsdk

__author__ = "Clinton Reese"
__date__ = "Feb 29 2012"
__copyright__ = "Copyright (C) 2011 Clinton's 3D Creations"
__version__ = "1.0"
__maintainer__ = "Clinton Reese"
__email__ = "[email protected]"
__status__ = "Example modified"
__lwver__ = "11"

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

# LWCommandSequence -----------------------------------
def process(self, mod_command):
edit_op_result = lwsdk.EDERR_NONE
mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER)
if not mesh_edit_op:
print >>sys.stderr, 'Failed to engage mesh edit operations!'
return lwsdk.AFUNC_OK

ptID = []
pt = [ -1.0, 0.0, -1.0 ]
uv = [ 0.0, 0.0 ]
rgb = [1.0, 0.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
# different from c, dimension of vmap is not used?
#mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV,"MyUVMap", 2, uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ -1.0, 0.0, 1.0 ]
uv = [ 0.0, 1.0 ]
rgb = [0.0, 1.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.25)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, 1.0 ]
uv = [ 1.0, 1.0 ]
rgb = [0.0, 0.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.75)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, -1.0 ]
uv = [ 1.0, 0.0 ]
rgb = [1.0, 1.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_WGHT, "MyWeightMap", 1.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

polygon = mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, "MySurface", ptID)

#mesh_edit_op.pntVPMap would be used at this point to create discontinuous uv values for the polygons

mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

return lwsdk.AFUNC_OK


ServerTagInfo = [
( "Python Basic Square", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Make Square", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PyMakeSquare", make_square) : ServerTagInfo }

Gorbag
03-01-2012, 10:53 AM
Ideally, you'd just be able to append code like this to your script to set a color for the surface:


# apply a color to 'MySurface'
surface_funcs = lwsdk.LWSurfaceFuncs()
surface = surface_funcs.first()
while surface:
if surface_funcs.name(surface) == "MySurface":
surface_funcs.setFlt(surface, lwsdk.SURF_COLR, [0.0, 1.0, 1.0])
break
surface = surface_funcs.next()

or


new_color = lwsdk.Color(0.0, 1.0, 1.0)
surface_funcs = lwsdk.LWSurfaceFuncs()
surface = surface_funcs.first()
while surface:
if surface_funcs.name(surface) == "MySurface":
surface_funcs.setFlt(surface, lwsdk.SURF_COLR, new_color)
break
surface = surface_funcs.next()

However, I've discovered an implementation bug with respect to the setFlt() method with prevents it from functioning properly. I'll have to get this fixed.

shrox
03-01-2012, 11:35 AM
....

Such a witty avatar and so few posts!

Gorbag
03-01-2012, 03:29 PM
Such a witty avatar and so few posts!

Um, what in particular is "witty" about my face? A lot of people use their faces as their avatars. You yourself are using your face. Sure, I'm a lot older than I used to be, but I don't think that's reason to make fun of somebody--I've tried to take care of myself.

I mean, here's a recent photo:

http://www.lucidgears.com/images/WittyAvatar.png

Yeah, I've got a lot less stair than I used to have, but I really don't think it's appropriate to make fun of somebody's face, that's all I'm saying.

clintonman
03-01-2012, 08:19 PM
Ideally, you'd just be able to append code like this to your script to set a color for the surface:
...

However, I've discovered an implementation bug with respect to the setFlt() method with prevents it from functioning properly. I'll have to get this fixed.
Is it because the function takes a double and python only does floats?

Gorbag
03-02-2012, 12:28 PM
Is it because the function takes a double and python only does floats?

No, it's because the function takes a double pointer (from lwsurf.h in the LWSDK):


int (*setFlt)(LWSurfaceID,const char *, double *);

And internally, that is not being processed by the wrapper layer in a form that Python can digest. Python doesn't really know what a "double *" is or how to handle it directly, so unless I deal with it explicitly (by some type of conversion), it gets passed around as an opaque type. As an opaque type, you cannot modify the data to which the pointer points.

Hope that made sense.

clintonman
03-02-2012, 07:07 PM
Makes sense to me. Thanks.

mitsura
06-11-2012, 05:18 AM
Hi,

were you able to find a solution to the setFlt problem? I am running into the same issue right now.
I don't really know how to solve it.
I was looking at ctypes since it seems ctypes allows you to work with pointers in Python but ctypes is not part of the LW Python distribution.

Any help much appreciated

clintonman
06-11-2012, 10:08 AM
#! /usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: ascii -*-

"""
This is a LightWave Command Sequence plug-in (Modeler) that creates
a square.
"""

import sys
import lwsdk

__author__ = "Clinton Reese"
__date__ = "Feb 29 2012"
__copyright__ = "Copyright (C) 2011 Clinton's 3D Creations"
__version__ = "1.0"
__maintainer__ = "Clinton Reese"
__email__ = "[email protected]"
__status__ = "Example modified"
__lwver__ = "11"

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

# LWCommandSequence -----------------------------------
def process(self, mod_command):
edit_op_result = lwsdk.EDERR_NONE
mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER)
if not mesh_edit_op:
print >>sys.stderr, 'Failed to engage mesh edit operations!'
return lwsdk.AFUNC_OK

ptID = []
pt = [ -1.0, 0.0, -1.0 ]
uv = [ 0.0, 0.0 ]
rgb = [1.0, 0.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
# different from c, dimension of vmap is not used?
#mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV,"MyUVMap", 2, uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ -1.0, 0.0, 1.0 ]
uv = [ 0.0, 1.0 ]
rgb = [0.0, 1.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.25)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, 1.0 ]
uv = [ 1.0, 1.0 ]
rgb = [0.0, 0.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.75)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, -1.0 ]
uv = [ 1.0, 0.0 ]
rgb = [1.0, 1.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_WGHT, "MyWeightMap", 1.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

polygon = mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, "MySurface", ptID)

#mesh_edit_op.pntVPMap would be used at this point to create discontinuous uv values for the polygons

mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

#new_color = lwsdk.Color(0.0, 1.0, 1.0)
new_color = [0.0, 1.0, 1.0]
surface_funcs = lwsdk.LWSurfaceFuncs()
surface = surface_funcs.first()
while surface:
if surface_funcs.name(surface) == "MySurface":
surface_funcs.setFlt(surface, lwsdk.SURF_COLR, new_color)
break
surface = surface_funcs.next()

thecommand = mod_command.lookup(mod_command.data, "SETLAYER")
i="1"
cs_options = lwsdk.marshall_dynavalues(i)
result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
i="2"
cs_options = lwsdk.marshall_dynavalues(i)
result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
i="3"
cs_options = lwsdk.marshall_dynavalues(i)
result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
i="9"
cs_options = lwsdk.marshall_dynavalues(i)
result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)

return lwsdk.AFUNC_OK


ServerTagInfo = [
( "Python Basic Square", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Make Square", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PyMakeSquare", make_square) : ServerTagInfo }

This code works for me. Be sure you are running Lightwave ver 11.0.1, it doesn't work in LW 11.0

clintonman
03-08-2013, 04:33 AM
Updated script for 11.5 which uses "surface_funcs.set_float" instead of "surface_funcs.setFlt(surface, lwsdk.SURF_COLR, new_color)". Also commented out the extra set layer commands.


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

"""
This is a LightWave Command Sequence plug-in (Modeler) that creates
a square.
"""

import sys
import lwsdk

__author__ = "Clinton Reese"
__date__ = "March 8 2013"
__copyright__ = "Copyright (C) 2011 Clinton's 3D Creations"
__version__ = "1.0"
__maintainer__ = "Clinton Reese"
__email__ = "[email protected]"
__status__ = "Example modified"
__lwver__ = "11.5"

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

# LWCommandSequence -----------------------------------
def process(self, mod_command):
edit_op_result = lwsdk.EDERR_NONE
mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER)
if not mesh_edit_op:
print >>sys.stderr, 'Failed to engage mesh edit operations!'
return lwsdk.AFUNC_OK

ptID = []
pt = [ -1.0, 0.0, -1.0 ]
uv = [ 0.0, 0.0 ]
rgb = [1.0, 0.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
# different from c, dimension of vmap is not used?
#mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV,"MyUVMap", 2, uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[0], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ -1.0, 0.0, 1.0 ]
uv = [ 0.0, 1.0 ]
rgb = [0.0, 1.0, 0.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.25)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[1], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, 1.0 ]
uv = [ 1.0, 1.0 ]
rgb = [0.0, 0.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_WGHT, "MyWeightMap", 0.75)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[2], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

pt = [ 1.0, 0.0, -1.0 ]
uv = [ 1.0, 0.0 ]
rgb = [1.0, 1.0, 1.0]
ptID.append(mesh_edit_op.addPoint(mesh_edit_op.sta te, pt))
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_TXUV, "MyUVMap", uv)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_WGHT, "MyWeightMap", 1.0)
mesh_edit_op.pntVMap(mesh_edit_op.state, ptID[3], lwsdk.LWVMAP_RGB, "MyVertexColorMap", rgb)

polygon = mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, "MySurface", ptID)

#mesh_edit_op.pntVPMap would be used at this point to create discontinuous uv values for the polygons

mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

#new_color = lwsdk.Color(0.0, 1.0, 1.0)
new_color = [0.0, 1.0, 1.0]
surface_funcs = lwsdk.LWSurfaceFuncs()
surface = surface_funcs.first()
while surface:
if surface_funcs.name(surface) == "MySurface":
#try float for lw11.5 - worked
#surface_funcs.setFlt(surface, lwsdk.SURF_COLR, new_color)
surface_funcs.set_float(surface, lwsdk.SURF_COLR, new_color)
break
surface = surface_funcs.next()

thecommand = mod_command.lookup(mod_command.data, "SETLAYER")
i="1"
cs_options = lwsdk.marshall_dynavalues(i)
result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
#remove to avoid confusion
#i="2"
#cs_options = lwsdk.marshall_dynavalues(i)
#result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
#i="3"
#cs_options = lwsdk.marshall_dynavalues(i)
#result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)
#i="9"
#cs_options = lwsdk.marshall_dynavalues(i)
#result = mod_command.execute(mod_command.data, thecommand, cs_options, lwsdk.OPSEL_USER)

return lwsdk.AFUNC_OK


ServerTagInfo = [
( "Python Basic Square", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Make Square", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PyMakeSquare", make_square) : ServerTagInfo }

Gorbag
03-08-2013, 08:04 AM
Yeah, I'm sure there will be lots of these little items that will need to be addressed. Integrating Python was not a simple undertaking, and it'll be on-going for a while yet.

Sorry for the inconvenience.

souzou
03-08-2013, 08:12 AM
Um, what in particular is "witty" about my face? A lot of people use their faces as their avatars. You yourself are using your face. Sure, I'm a lot older than I used to be, but I don't think that's reason to make fun of somebody--I've tried to take care of myself.

I mean, here's a recent photo:

http://www.lucidgears.com/images/WittyAvatar.png

Yeah, I've got a lot less stair than I used to have, but I really don't think it's appropriate to make fun of somebody's face, that's all I'm saying.

lol. I think he was just taken aback by your lack of banisters.