PDA

View Full Version : float changes to int?


SAmunategui
12-08-2011, 12:35 AM
I am making part of a script right now and it does what I need but something strange happens and I don't understand why.

It searches through the joints of a skeleton and finds the highest Y positioned joint using xform worldspace

//go through skeleton one by one and store the highest Y translated joint //select all joints in scene

select -r `ls -type joint`;

$jointChain = `ls -sl`;

//go through all the joints and compare world space Y values and store the joint if it's higher than the previous

for ($joints in $jointChain)

{

$jointXForm = `xform -q -ws -t $joints`;

float $jointXFormY = $jointXForm[1];

float $maxYJoint;

string $maxJoint;



if ($jointXFormY > $maxYJoint)

{

$maxJoint = $joints;

$maxYJoint = $jointXFormY;

print ($maxJoint +"\n");

print ($maxYJoint +"\n");

}

}

print ($maxYJoint +"\n");



In the first print (inside the if statement), $maxYJoint is a float. The second time (outside the entire thing) it becomes an int? :hmm:

This is what prints out with the skeleton i'm using right now:

print ($maxYJoint +"\n"); LillianPonyTail2
1.330159945
LillianPonyTail3
1.33016099
Lillian_CenterHead
1.384508372
Lillian_Hair1
1.497386631
Lillian_Hair2 <- correct highest joint name
1.508277113 <- correct highest joint worldspace y value
1 <- ??????

WesHowe
12-08-2011, 06:36 AM
When you declare a variable within a block (a pair of curly braces) the scope is limited to that block, and any blocks nested within that. If you look at your code, you do not declare the float $maxYJoint until after the first opening curly brace of your for() loop.

Your second print statement is after the matching closing curly brace, meaning that the float declaration is gone, it is out-of-scope, and you are getting a newly created int variable as a default. The fix should be to move the declaration above the first curly brace.

<* Wes *>

Nyro
12-08-2011, 11:11 AM
I'm surprised the first print prints anything at all, since you just declare an empty float variable $maxYJoint and then proceed to compare some float to it ($joinXFormY > $maxYJoint)

Bu that aside, there's something else in your script that you should take alook at:

If at all possible, never never use the select command to get lists of objects. I did it too in my early days because I hadn't realised that you can pass a list of objects to virtually any command; they don't just operate on the active list.
Selecting is a very slow process in MEL (sometimes slow even in realtime terms, not just floating point calculation terms), so it unnecessarily slows down your script once you have lots and lots of joints.

Its funny anyway, if I may say so: You select all joints using the `ls` command, but then you list all the joints using the selection (that used the `ls`). It's like writing ls(`ls -type joint`);

Instead of writing

select -r `ls -type joint`;

$jointChain = `ls -sl`;

just write

$jointChain = `ls -type joint`;

benio33
12-08-2011, 03:40 PM
Exactly like guys said - both WesHowe and Nyro were correct. When I ran your code I got "Error: Line 39.26: "$maxYJoint" is an undeclared variable. " error. It basically means you had the maxYJoint stored in the memory, so you got "1" all the time. This one works though.

$jointChain = `ls -type joint`; float $maxYJoint;
string $maxJoint;
for ($joints in $jointChain)
{
$jointXForm = `xform -q -ws -t $joints`;
float $jointXFormY = $jointXForm[1];
if ($jointXFormY > $maxYJoint)
{
$maxJoint = $joints;
$maxYJoint = $jointXFormY;
print ($maxJoint +"\n");
print ($maxYJoint +"\n");
}
}
print ($maxYJoint +"\n");

SAmunategui
12-08-2011, 05:55 PM
heh, thanks guys

More than answered my question. I didn't realize variables within blocks get limited to that block. I'm surprised I haven't run into this before. Clearly i'm not a pro (yet), but i've written a handful of things before

Also, not sure why I keep overlooking putting selections into one line. I pretty much do that for anything else I do :)

CGTalk Moderation
12-08-2011, 05:55 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.