Results 1 to 12 of 12

Thread: Create a square

  1. #1

    Create a square

    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.

    Code:
    #! /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.state, 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.state, 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.state, 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.state, 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 }

  2. #2
    Geist im Maschine Gorbag's Avatar
    Join Date
    Jul 2003
    Location
    Lakewood, CO
    Posts
    39
    Ideally, you'd just be able to append code like this to your script to set a color for the surface:

    Code:
            # 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

    Code:
            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.
    Last edited by Gorbag; 03-01-2012 at 11:28 AM.

  3. #3
    Man of many cells. shrox's Avatar
    Join Date
    Aug 2006
    Location
    Hendersonville, NC
    Posts
    6,959
    Quote Originally Posted by Gorbag View Post
    ....
    Such a witty avatar and so few posts!
    shrox www.shrox.com
    -----------------------
    Heavy Metal Landing


    -----------------------
    I build the best spaceships, the biggest spaceships, they're great, you'll love them.

  4. #4
    Geist im Maschine Gorbag's Avatar
    Join Date
    Jul 2003
    Location
    Lakewood, CO
    Posts
    39
    Quote Originally Posted by shrox View Post
    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:



    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.

  5. #5
    Quote Originally Posted by Gorbag View Post
    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?

  6. #6
    Geist im Maschine Gorbag's Avatar
    Join Date
    Jul 2003
    Location
    Lakewood, CO
    Posts
    39
    Quote Originally Posted by clintonman View Post
    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):

    Code:
      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.

  7. #7
    Makes sense to me. Thanks.

  8. #8
    Registered User
    Join Date
    Jun 2012
    Location
    Belgium
    Posts
    4
    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

  9. #9
    Code:
    #! /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.state, 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.state, 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.state, 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.state, 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

  10. #10
    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.

    Code:
    #! /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.state, 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.state, 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.state, 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.state, 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 }

  11. #11
    Geist im Maschine Gorbag's Avatar
    Join Date
    Jul 2003
    Location
    Lakewood, CO
    Posts
    39
    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.

  12. #12
    Explorer souzou's Avatar
    Join Date
    Sep 2003
    Location
    London, UK
    Posts
    420
    Quote Originally Posted by Gorbag View Post
    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:



    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.
    Matt Knott / VERSUS
    http://www.versus.nu

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •