View Full Version : vectors in procedure - weirdness

 biorhythm05 May 2007, 03:28 AMhi all, i have a local procedure which declears heaps of vectors then loops through creating a locator at each location specified by the vector, problem is when i run the script its fine but if i put the code in a local procedure block the vectors all seem to be << 0 0 0 >> or at least thats where the locators are created Can anyone shed some light on this? thanks in advance:) proc makeLocators() { // The vectors - for locator cords vector \$cord_0 = << 0, 74.734874, -3.342664 >> ; vector \$cord_1 = << 0, 83.171021, -3.961567 >> ; vector \$cord_2 = << 0, 92.630768, -6.181639 >> ; vector \$cord_3 = << 0, 101.195753, -8.178419 >> ; vector \$cord_4 = << 0, 109.844457, -9.991547 >> ; vector \$cord_5 = << 0, 118.161794, -10.402352 >> ; vector \$cord_6 = << 0, 125.885139, -9.441655 >> ; vector \$cord_7 = << 0, 131.20147, -7.62474 >> ; vector \$cord_8 = << 0, 137.202108, -6.298182 >> ; vector \$cord_9 = << 0, 144.859997, -4.372298 >> ; vector \$cord_10 = << 0, 173.079097, -5.033683 >> ; vector \$cord_11 = << 0, 140.340532, -2.277912 >> ; vector \$cord_12 = << 0, 135.04945, 15.359026 >> ; vector \$cord_13 = << 0, 157, 0.159908 >> ; vector \$cord_14 = << 7.973591, 156.875886, 3.448159 >> ; vector \$cord_15 = << 2.577232, 119.317561, -4.196853 >> ; vector \$cord_16 = << 13.333011, 123.506653, -6.7231 >> ; vector \$cord_17 = << 36.656067, 120.449748, -6.842157 >> ; vector \$cord_18 = << 54.544625, 121.015841, 0 >> ; vector \$cord_19 = << 55.615145, 121.270859, 0.480361 >> ; vector \$cord_20 = << 65.92934, 121.015841, 9.539275 >> ; vector \$cord_21 = << 70.483506, 120.291591, 12.937732 >> ; vector \$cord_22 = << 73.97268, 119.83991, 15.595288 >> ; vector \$cord_23 = << 77.631401, 119.302788, 18.450428 >> ; vector \$cord_24 = << 67.385499, 121.015841, 6.584129 >> ; vector \$cord_25 = << 72.958755, 120.291591, 9.385536 >> ; vector \$cord_26 = << 77.113232, 119.969562, 11.504951 >> ; vector \$cord_27 = << 82.648519, 119.445024, 14.301284 >> ; vector \$cord_28 = << 68.523036, 121.015841, 3.44102 >> ; vector \$cord_29 = << 73.70676, 120.291591, 5.296916 >> ; vector \$cord_30 = << 78.363794, 119.969562, 6.988502 >> ; vector \$cord_31 = << 82.609822, 119.445024, 8.522702 >> ; vector \$cord_32 = << 69.235767, 121.015841, 0.352518 >> ; vector \$cord_33 = << 73.907842, 120.457225, 1.008555 >> ; vector \$cord_34 = << 77.82165, 120.080065, 1.758798 >> ; vector \$cord_35 = << 81.525837, 119.76463, 2.503182 >> ; vector \$cord_36 = << 55.520234, 121.015841, 5.139353 >> ; vector \$cord_37 = << 55.846794, 119.040924, 8.836771 >> ; vector \$cord_38 = << 56.338091, 117.001962, 13.179378 >> ; vector \$cord_39 = << 56.961477, 114.832304, 17.338316 >> ; vector \$cord_40 = << 8.789346, 123.137594, -12.995807 >> ; vector \$cord_41 = << 7.785293, 110.712938, -14.342652 >> ; vector \$cord_42 = << 9.455215, 65.494614, 0 >> ; vector \$cord_43 = << 9.037271, 33.382862, 0 >> ; vector \$cord_44 = << 9.037271, 9.65095, -7.548991 >> ; vector \$cord_45 = << 9.037271, 0, 7 >> ; vector \$cord_46 = << 9.037271, 0, 22 >> ; // The variables - for the loop int \$count = 47 ; float \$x ; float \$y ; float \$z ; // The loop - loop through vectors and create locators for( \$i=0; \$i < \$count; \$i++ ) { string \$lctName = ("locator_" + \$i) ; string \$sphName = ("sphere_" + \$i) ; eval("float \$x = \$cord_"+\$i+".x") ; eval("float \$y = \$cord_"+\$i+".y") ; eval("float \$z = \$cord_"+\$i+".z") ; curve -d 1 -p 1 0 0 -p -1 0 0 -p 0 0 0 -p 0 0 1 -p 0 0 -1 -p 0 0 0 -p 0 -1 0 -p 0 1 0 ; rename "curve1" \$lctName ; move -rpr \$x \$y \$z \$lctName ; setAttr (\$lctName + ".sx") 3 ; setAttr (\$lctName + ".sy") 3 ; setAttr (\$lctName + ".sz") 3 ; makeIdentity -apply true -t 1 -r 1 -s 1 -n 0; sphere -n \$sphName -p \$x \$y \$z ; CenterPivot ; pointConstraint \$lctName \$sphName ; } select "*locator_*" ; group -n locator_GRP ; select "*sphere_*" ; group -n sphere_GRP ; }
trancor
05 May 2007, 05:44 AM
Something is telling me this isn't going to post for a day because I'm new but -

problem is when i run the script its fine but if i put the code in a local procedure block the vectors all seem to be << 0 0 0 >> or at least thats where the locators are created

I looked through your code, everything looked fine, I ran your script and it created the spheres and locators in the shape of a biped character rig, and that is probably what you were going for. But the only thing I changed was that I took the proc away and ran the code streight. Normaly this shouldn't matter by try putting global before prob, prehaps maya will eval all the valuables propperly then.

biorhythm
05 May 2007, 08:12 AM
On further inspection, i decided to add a few extra lines in the loop to print the value of \$x \$y \$z,

print \$x ;
print \$y ;
print \$z ;

and found that eval doesn't return the value of the vectors, instead returns their values as 0.

i have read through the command reference and googled but still found no reason for this to happen, if anyone can shed some light on the matter i would be most appreciative.

GiantG
05 May 2007, 10:17 AM
If you use a procedure your Vector Definitions must be global.
If you don't use a procedure it run's like this:

// The vectors - for locator cords
vector \$cord_0 = << 0, 74.734874, -3.342664 >> ;
vector \$cord_1 = << 0, 83.171021, -3.961567 >> ;
vector \$cord_2 = << 0, 92.630768, -6.181639 >> ;
vector \$cord_3 = << 0, 101.195753, -8.178419 >> ;
vector \$cord_4 = << 0, 109.844457, -9.991547 >> ;
vector \$cord_5 = << 0, 118.161794, -10.402352 >> ;
vector \$cord_6 = << 0, 125.885139, -9.441655 >> ;
vector \$cord_7 = << 0, 131.20147, -7.62474 >> ;
vector \$cord_8 = << 0, 137.202108, -6.298182 >> ;
vector \$cord_9 = << 0, 144.859997, -4.372298 >> ;
vector \$cord_10 = << 0, 173.079097, -5.033683 >> ;
vector \$cord_11 = << 0, 140.340532, -2.277912 >> ;
vector \$cord_12 = << 0, 135.04945, 15.359026 >> ;
vector \$cord_13 = << 0, 157, 0.159908 >> ;
vector \$cord_14 = << 7.973591, 156.875886, 3.448159 >> ;
vector \$cord_15 = << 2.577232, 119.317561, -4.196853 >> ;
vector \$cord_16 = << 13.333011, 123.506653, -6.7231 >> ;
vector \$cord_17 = << 36.656067, 120.449748, -6.842157 >> ;
vector \$cord_18 = << 54.544625, 121.015841, 0 >> ;
vector \$cord_19 = << 55.615145, 121.270859, 0.480361 >> ;
vector \$cord_20 = << 65.92934, 121.015841, 9.539275 >> ;
vector \$cord_21 = << 70.483506, 120.291591, 12.937732 >> ;
vector \$cord_22 = << 73.97268, 119.83991, 15.595288 >> ;
vector \$cord_23 = << 77.631401, 119.302788, 18.450428 >> ;
vector \$cord_24 = << 67.385499, 121.015841, 6.584129 >> ;
vector \$cord_25 = << 72.958755, 120.291591, 9.385536 >> ;
vector \$cord_26 = << 77.113232, 119.969562, 11.504951 >> ;
vector \$cord_27 = << 82.648519, 119.445024, 14.301284 >> ;
vector \$cord_28 = << 68.523036, 121.015841, 3.44102 >> ;
vector \$cord_29 = << 73.70676, 120.291591, 5.296916 >> ;
vector \$cord_30 = << 78.363794, 119.969562, 6.988502 >> ;
vector \$cord_31 = << 82.609822, 119.445024, 8.522702 >> ;
vector \$cord_32 = << 69.235767, 121.015841, 0.352518 >> ;
vector \$cord_33 = << 73.907842, 120.457225, 1.008555 >> ;
vector \$cord_34 = << 77.82165, 120.080065, 1.758798 >> ;
vector \$cord_35 = << 81.525837, 119.76463, 2.503182 >> ;
vector \$cord_36 = << 55.520234, 121.015841, 5.139353 >> ;
vector \$cord_37 = << 55.846794, 119.040924, 8.836771 >> ;
vector \$cord_38 = << 56.338091, 117.001962, 13.179378 >> ;
vector \$cord_39 = << 56.961477, 114.832304, 17.338316 >> ;
vector \$cord_40 = << 8.789346, 123.137594, -12.995807 >> ;
vector \$cord_41 = << 7.785293, 110.712938, -14.342652 >> ;
vector \$cord_42 = << 9.455215, 65.494614, 0 >> ;
vector \$cord_43 = << 9.037271, 33.382862, 0 >> ;
vector \$cord_44 = << 9.037271, 9.65095, -7.548991 >> ;
vector \$cord_45 = << 9.037271, 0, 7 >> ;
vector \$cord_46 = << 9.037271, 0, 22 >> ;

// The variables - for the loop
int \$count = 47 ;
float \$x ;
float \$y ;
float \$z ;
int \$i;

// The loop - loop through vectors and create locators
for( \$i=0; \$i < \$count; \$i++ )
{

string \$lctName = ("locator_" + \$i) ;
string \$sphName = ("sphere_" + \$i) ;
eval("float \$x = \$cord_"+\$i+".x") ;
eval("float \$y = \$cord_"+\$i+".y") ;
eval("float \$z = \$cord_"+\$i+".z") ;

curve -d 1 -p 1 0 0 -p -1 0 0 -p 0 0 0 -p 0 0 1 -p 0 0 -1 -p 0 0 0 -p 0 -1 0 -p 0 1 0 ;

rename "curve1" \$lctName ;

move -rpr \$x \$y \$z \$lctName ;
setAttr (\$lctName + ".sx") 3 ;
setAttr (\$lctName + ".sy") 3 ;
setAttr (\$lctName + ".sz") 3 ;
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0;

sphere -n \$sphName -p \$x \$y \$z ;
CenterPivot ;
pointConstraint \$lctName \$sphName ;

}

select "*locator_*" ;
group -n locator_GRP ;
select "*sphere_*" ;
group -n sphere_GRP ;

biorhythm
05 May 2007, 07:16 AM
ok, so after making all the vectors global, they still eval with a 0 value.

i have also played with declaring the values in the loop, as well as before the procedure but regardless of where and how i declare still they have eval of << 0 0 0 >>

im wondering if using eval is not the way to go here, does anyone have any ideas as how to go about accessing data from vectors/arrays using for loops?

trancor
05 May 2007, 09:29 PM
How did you figure out those vectors anyway?

Your problem ends up being in -

eval("float \$x = \$cord_"+\$i+".x") ;
eval("float \$y = \$cord_"+\$i+".y") ;
eval("float \$z = \$cord_"+\$i+".z") ;

If you print the variables after they are set in the proccess it prints out 0, but if you run it without being in the proc everything has number values.

Maybe what you could do is generate the spheres manually prior to the for look and once in the look getAttr of the position of the sphere you want to put the locator at. Only way I can think of doing this would be having your sphere command after every single vector in your set up.

If you don't want to do that, maybe you could set up an array of all the Xs then another array of all the Ys and then same for the Zs and call up the array[\$i] in your for loop.

I can't imaging you'd have an ik structure you constructed for this character you are generating and printed out the vector data for each joint, you could just do a getAttr for the joints and add them to the arrays to be read in your for loop.

I have some work to get done before friday so I didn't write up anything to do this for you, but I know you can do it yourself, good luck.

trancor
05 May 2007, 09:32 PM
On further inspection, i decided to add a few extra lines in the loop to print the value of \$x \$y \$z,

print \$x ;
print \$y ;
print \$z ;

and found that eval doesn't return the value of the vectors, instead returns their values as 0.

i have read through the command reference and googled but still found no reason for this to happen, if anyone can shed some light on the matter i would be most appreciative.

oops, I'm oblivious.

biorhythm
05 May 2007, 05:46 PM
well after stairing at the same code for days i finaly got it, silly mistake too
all i need to was add global to the following variables

// The variables - for the loop
int \$count = 47 ;
global float \$x ;
global float \$y ;
global float \$z ;
int \$i;

anyways, thanks for the help:)

CGTalk Moderation
05 May 2007, 05:46 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.

1