PDA

View Full Version : Problem with moving points with Python



skarloc
05-08-2013, 02:20 PM
Hi,

been some time since I've had a few moments to play around with LW Modeler scripting. I'm going back to a question I asked previously - however, I believe I have a bug, hence why I'm starting a new topic.

I've taken bits out of the gears.py example script so as to test moving points around. Here's the modified code :



class testmove(lwsdk.ICommandSequence):

def __init__(self, context):
super(testmove, self).__init__()

def get_commands(self, mod_command):
command_list = {}
for command in ["MAKEDISC",
"ROTATE",
"MOVE"]:
command_list[command] = mod_command.lookup(mod_command.data, command)

return command_list

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

# Initialise the dictionary of commands
cs_dict = self.get_commands(mod_command)

cs_options = lwsdk.marshall_dynavalues(([0.01, # radius
0.01,
0.01],
0.01, # top
0, # bottom
'Z', # axis
32, # number of sides
1, # number of segments
[0.0, 0.0, 0.0] # center
))
result = mod_command.execute(mod_command.data, cs_dict["MAKEDISC"], cs_options, lwsdk.OPSEL_USER)
cs_options = lwsdk.marshall_dynavalues((45.0, 'Y'))
result = mod_command.execute(mod_command.data, cs_dict["ROTATE"], cs_options, lwsdk.OPSEL_USER)
# If the next 2 lines are commented out, it'll crash...
#~ cs_options = lwsdk.marshall_dynavalues(([1.0, 1.0, 1.0]))
#~ result = mod_command.execute(mod_command.data, cs_dict["MOVE"], cs_options, lwsdk.OPSEL_USER)
return lwsdk.AFUNC_OK

As you can see from the code, there are 2 lines that are commented out (in bold, italic) - these, supposedly, should move the object 1,1,1. However, when I run the program (with the lines un-commented), I get the following error :

114194

If the lines are commented, it works (you get a disc rotated 45) - if you ran the code uncommented first, you'll need to restart modeler.

Now I've been looking at c code in the SDK, especially point.c. The command for move appears :


int csMove( double *offset )
{
static LWCommandCode ccode;
ModData *md = getModData();
DynaValue argv;

assert( md->edit == NULL );

argv.type = DY_VFLOAT;
argv.fvec.val[ 0 ] = offset[ 0 ];
argv.fvec.val[ 1 ] = offset[ 1 ];
argv.fvec.val[ 2 ] = offset[ 2 ];

LOOKUP( "MOVE" );
EXECUTE( 1, &argv );
return OK;
}


Which means it takes a vector as its sole and unique argument.

Looking at the rotate code :


int csRotate( double angle, char *axis, double *center )
{
static LWCommandCode ccode;
ModData *md = getModData();
DynaValue argv[ 3 ];

assert( md->edit == NULL );

argv[ 0 ].type = DY_FLOAT;
argv[ 0 ].flt.value = angle;

argv[ 1 ].type = DY_STRING;
argv[ 1 ].str.buf = axis;
argv[ 1 ].str.bufLen = 0;

if ( center ) {
argv[ 2 ].type = DY_VFLOAT;
argv[ 2 ].fvec.val[ 0 ] = center[ 0 ];
argv[ 2 ].fvec.val[ 1 ] = center[ 1 ];
argv[ 2 ].fvec.val[ 2 ] = center[ 2 ];
}
else argv[ 2 ].type = DY_NULL;

LOOKUP( "ROTATE" );
EXECUTE( 3, argv );
return OK;
}


It has 2 or 3 arguments - rotation, axis and centre (optional). This corresponds to my python version ("stolen" from gears.py), so my assumptions concerning the move command appear to be correct.

Anyone any ideas? Am I doing something wrong, or have I detected another scripting bug?

Thanks in advance,

For info, the previous topic was http://forums.newtek.com/showthread.php?130588-Modeler-example.

iain_r
05-09-2013, 10:07 AM
Hi,

I've changed how you pass the parameter for the dynamic evaluation. It's expecting a vector so you need to define a variable of type vector and assign values to each point.

Regards

Iain

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

import sys
import math
import lwsdk

__author__ = ""
__date__ = ""
__copyright__ = ""
__version__ = "1.0"
__maintainer__ = ""
__email__ = ""
__status__ = ""
__lwver__ = "11"

class testmove(lwsdk.ICommandSequence):

def __init__(self, context):
super(testmove, self).__init__()

def get_commands(self, mod_command):
command_list = {}
for command in ["MAKEDISC",
"ROTATE",
"MOVE"]:
command_list[command] = mod_command.lookup(mod_command.data, command)

return command_list

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

# Initialise the dictionary of commands
cs_dict = self.get_commands(mod_command)

cs_options = lwsdk.marshall_dynavalues(([0.01, # radius
0.01,
0.01],
0.01, # top
0, # bottom
'Z', # axis
32, # number of sides
1, # number of segments
[0.0, 0.0, 0.0] # center
))
result = mod_command.execute(mod_command.data, cs_dict["MAKEDISC"], cs_options, lwsdk.OPSEL_USER)
cs_options = lwsdk.marshall_dynavalues((45.0, 'Y'))
result = mod_command.execute(mod_command.data, cs_dict["ROTATE"], cs_options, lwsdk.OPSEL_USER)
# If the next 2 lines are commented out, it'll crash...
point = lwsdk.Vector()
point.x = 1.0
point.y = 1.0
point.z = 1.0
cs_options = lwsdk.marshall_dynavalues(point)
result = mod_command.execute(mod_command.data, cs_dict["MOVE"], cs_options, lwsdk.OPSEL_USER)
return lwsdk.AFUNC_OK

ServerTagInfo = [
( "Python Test Move", lwsdk.SRVTAG_USERNAME | lwsdk.LANGID_USENGLISH ),
( "Test Move", lwsdk.SRVTAG_BUTTONNAME | lwsdk.LANGID_USENGLISH ),
( "Utilities/Python", lwsdk.SRVTAG_MENU | lwsdk.LANGID_USENGLISH )
]

ServerRecord = { lwsdk.CommandSequenceFactory("LW_PyTestMove", testmove) : ServerTagInfo }

skarloc
05-09-2013, 12:22 PM
Hi,

I've changed how you pass the parameter for the dynamic evaluation. It's expecting a vector so you need to define a variable of type vector and assign values to each point.

Regards

Iain


(reduced)

point = lwsdk.Vector()
point.x = 1.0
point.y = 1.0
point.z = 1.0
cs_options = lwsdk.marshall_dynavalues(point)
result = mod_command.execute(mod_command.data, cs_dict["MOVE"], cs_options, lwsdk.OPSEL_USER)


Yay, give that man a banana! Works fine.

However (and this isn't important), I'm confused as to why the makedisc works when the vectors are passed as [0.0, 0.0, 0.0]... Unless if points aren't vectors (in my eyes, there's little difference between the two).

Anyawy, cheers for your help :beerchug: - I can continue onwards and further !

iain_r
05-09-2013, 03:43 PM
No problem, I did try other variations for the vector, don't know why the makedisc works.

Regards

Iain