PDA

View Full Version : incrementing variables in expressions


bendingiscool
04-13-2009, 09:10 PM
Hey, I'm trying to work out how to increment floats in expressions...



import maya.cmds as mc
#---------------------
for i in range(0,10,1):
exp = 'float $vel' + '%d' + '[];\n' % i

mc.expression(s=exp, name='expTest')


I'm getting this error...

# TypeError: not all arguments converted during string formatting #

Any help on this one would be great

Chris

strarup
04-16-2009, 12:10 AM
Hi,

not quite sure what you wanna do exactly... and I'm not that familar with python yet... however as I have understood
expressions and python aren't the best of friends in maya... and can be a bit tricky... but this might just be my lack of
knowlegde with python... :D

I'm also not quite aware of what you want... do you want the $vel to increase like $vel1[], $vel2[], or the number inside the []
like $vel[1], $vel[2]...

if you want the $vel1[], $vel2[] etc. use this... --> exp = "float $vel%d[];\n"% i
however if you want the number in the [] to increase use this... --> exp = "float $vel[%d];\n"% i

however with this method you have here you get a lot of separate expression... like expTest, expTest1, expTest2 etc.


in range you can just use this "for i in range(10):" instead of "for i in range(0,10,1)" since the step value for range is 1 by default
and the starting default value is 0... so unless you want other start or step values you can use that... e.g.
import maya.cmds as mc
#---------------------
for i in range(10):
exp = "float $vel%d[];\n"% i
mc.expression(s=exp, name="expTest")

you still get the lot of separate expressions thou...



you can do it with MEL like this... and get 1 expression with them all in it...
proc expressoTesto()
{
string $exp = "";
for($i = 0; $i<10; $i++)
{
$exp += ("float $vel"+$i+"[];\n");
}
//print $exp;
expression -s $exp -n "expTest";
}
expressoTesto;

however I don't see much use in just having an expression which says
float $vel0[];
float $vel1[];
float $vel2[];
and so on... :D

so a little bit more specific to what you intend and want.. would also help a great deal... to try and understand what you want to do and archieve...
would also help a great deal to help... if it was a bit more clear... :D

kind regards

Strarup

strarup
04-16-2009, 12:27 AM
lol... hmm... just need to get the mc.expression bit out of the loop to get one expression... :d
still need to learn a lot of python... I'm more a melscript monkey than a python snakie... :D

import maya.cmds as mc
#---------------------

for i in range(10):
exp += 'float $vel%d[];\n'% i
mc.expression(s=exp, name='expTest')

bendingiscool
04-16-2009, 09:31 AM
Thanks for the replies strarup, I ended up with this...


expGrpRY = 'float $vel%d[];\n' % i
expGrpRY += '$vel%d = `getAttr ' %i + agentRB + '.velocity`;\n'
expGrpRY += skelGrp + '.rotateY = atan2d($vel%d[0], $vel%d[2])/2;\n' % (i,i)

mc.expression(s=expGrpRY, name='expGrpRYTest')

It was just my dodgy syntaxing in the end lol

Chris

strarup
04-16-2009, 07:56 PM
Hi Chris,


hehe oki doki... :)

well that syntaxing can also be a bit something... I'm still like a blind man in a harem full off nude chicks when it comes to python syntaxing... I don't know where they exactly are just that they are there somehow... and sometimes lucky to find them... :D

I'm a completely python noob... but find it quite fun to start learning... however I still prefer Mel scripting... :)

but very nice you got it to work... :)

kind regards

Strarup

CGTalk Moderation
04-16-2009, 07:56 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.