PDA

View Full Version : for loop issue


berniebernie
12-07-2008, 04:24 PM
Hello cgtalkers


I'm writing a small proc that will create x joints on a curve, but I'm failing in the for loop, and I can't figure out why.

Basic procedure:
- create a joint, stored in a var
- parent the joint to the previous joint
- next iteration of the for loop: make the new joint the 'old' joint and start over till the loop ends

Big ass screenshot:

http://mlkdesign.free.fr/dump/screenshotforloop.gif

Here's the code i wrote


global proc createJointsOnCurve(int $jointnum){
$sel = `ls -sl`;
$obj = $sel[0];
float $startpos[] = `pointOnCurve -pr 0 -p $obj`;
string $oldjoint = `joint -p $startpos[0] $startpos[1] $startpos[2]`; //starting joint
string $newjoint = "";
for($a = 0;$a<=$jointnum;$a++){
print("Start /// old: "+$oldjoint+" nu: "+$newjoint+"\n");
$currentUPos = $a/$jointnum;
float $jointpos[] = `pointOnCurve -pr $currentUPos -p $obj`;
$newjoint = `joint -p $jointpos[0] $jointpos[1] $jointpos[2]`;
joint -e -zso -oj xyz -sao yup $oldjoint;
string $oldjoint = $newjoint;
print("End /// old: "+$oldjoint+" nu: "+$newjoint+"\n");

}
}
createJointsOnCurve(10);




What did I do wrong ?
( in the script editor window, it's always printing 'joint 34', doesn't iterate)

Credo
12-07-2008, 05:08 PM
It looks like it's because you're declaring a new instance of a string inside it's own scope (the FOR loop). Try this change:


string $oldjoint = $newjoint;

// to

$oldjoint = $newjoint;


That way the new variable will be passed round to beginning of the loop again.

Also, you may need to declare the integers as floats. Maya doesn't tend to like it when you have something like:

float = int/int

ie:


float $Fa = $a; float $Fjointnum = $jointnum;
float $currentUPos = $Fa/$Fjointnum;


I can't see anything else with it that would need changing though :)

Matt

berniebernie
12-07-2008, 10:43 PM
thanks, will try that tomorrow and come back to bug the coders around here =)

CGTalk Moderation
12-07-2008, 10:43 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.