PDA

View Full Version : python... passing arguments :|


jaydru
04-12-2010, 12:44 PM
not totaly sure what im doing wrong here, im a total python noob

def clusterCrush():
node = listRelatives(ls(o=True, sl=True), p=True)
cluster()[1].scaleX.set(0)
delete(node, ch=True)
select(node)
clusterCrush()

the above code works but when i try to pass the scaleX attr in as a variable it fails

def clusterCrush(scaleDir):
node = listRelatives(ls(o=True, sl=True), p=True)
cluster()[1]. + scaleDir + .set(0)
delete(node, ch=True)
select(node)
clusterCrush('scaleX')

NaughtyNathan
04-12-2010, 12:59 PM
is that pyMel? either way I'm guessing it has to do with the fact that you can't construct function and method calls dynamically like this using string catenation(+), and you also can't catenate non-strings together.

put the dodgy line into an exec command and that should probably work..?

exec('cluster()[1].' + scaleDir + '.set(0)')

jaydru
04-12-2010, 01:06 PM
yeah its pymel, could quite easily have done this in mel in seconds but tying to learn abit of this python witch craft.
what you said worked a treat, you always seem to have the answers :)
thanks nathan.

btw is exec that same sort of thing as eval in mel?

NaughtyNathan
04-12-2010, 01:17 PM
yeah James, exec is akin to eval in MEL, although confusingly python also has an eval command..? They do both kind of do the same thing, but eval is supposed to be used for evaluating expressions, where as exec is used for executing code constructs.. I'm not really confident in my understanding of the subtle differences but the py docs do explain it somewhere... :)

:nathaN

uiron
04-12-2010, 03:16 PM
you do not code in object-oriented languages like you did in MEL. for example, you try to use "scaleX", a variable NAME as a variable VALUE and vice versa. that's usually a bad practice in most cases, and often can be resolved by changing the algorithm instead.

how about making "crush" argument a 3-dimensional vector instead (which will just broaden the usage of your function), and then pass [1,0,0] for X (similar for Y and Z) when needed?

jaydru
04-12-2010, 03:38 PM
thats a good point uiron thanks for the input :)

Chadrik
04-12-2010, 04:52 PM
you need to use the attr method to retrieve an attribute using a variable

def clusterCrush(scaleDir):
node = listRelatives(ls(o=True, sl=True), p=True)
cluster()[1].attr(scaleDir).set(0)
delete(node, ch=True)
select(node)
clusterCrush('scaleX')

jaydru
04-12-2010, 05:06 PM
love it, i knew you guys would have thought of that :D

CGTalk Moderation
04-12-2010, 05:06 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.