View Full Version : How do I get an objects local translation in a varible using xform?

01-17-2004, 04:29 PM
I tried to get an objects local translation in to a variable using xform like this:

$variable = `xform -q -os -t pCube1`;

I have tried with floats, float arrays and vectors but it dosen't seem to work for me.

Does anyone know how to do this?


01-17-2004, 11:55 PM
float $hello[] = `xform -q -os -t pCube1`;
print( $hello[0]+"\n" );
print( $hello[1]+"\n" );
print( $hello[2]+"\n" );

- Matthew Durante

01-18-2004, 10:44 AM
Ok, dont know what I did wrong when I tried that but thanx a lot! :thumbsup:

01-18-2004, 04:26 PM
The difference is that you didn't declare your variable as an array.

$variable (single variable)
$variable[] (array of variables, eg. variable[0], variable[1], variable[2], etc)

The xform command returns 3 values (x, y and z) so you need to assign it to a variable that can actually hold those values, like matthew demonstrated...

01-18-2004, 06:49 PM
Thanx for the replies!

I bumped in to yet another problem with xform though. I tried to get the objects speed along its x-axis using the script below but it only works when not rotating the object. If I rotate it during animation it seems to return world space values since they get negative.

So with the risk to making a fool out of myself again...does any one know how to fix this?


float $pos1;
float $pos2;
float $speed;
float $localPos[] = `xform -q -os -t pCube1`;

$pos1 = $localPos[0];
$speed = $pos1 - $pos2;
$pos2 = $localPos[0];

move -os -r 0.1 0 0 pCube1;
rotate -os -r 0 2 0 pCube1;

01-19-2004, 04:29 PM
i added comments to your script to let you see what happens:
float $pos1; // = 0
float $pos2; // = 0
float $speed; // = 0
float $localPos[] = `xform -q -os -t pCube1`;
// 3 floats either neg or pos = x,y,z

$pos1 = $localPos[0]; // = x value
$speed = $pos1 - $pos2; // x- 0 = x
$pos2 = $localPos[0]; // = x value

move -os -r 0.1 0 0 pCube1;
rotate -os -r 0 2 0 pCube1;

so, $pos2 should be assigned the array-value before you substract it from the other variable.
$pos1 = $localPos[0];
$pos2 = $localPos[1]; //??? [see below]
$speed = $pos1 - $pos2;

and the [0]-element for $pos2 is probably a typing error? 'cause otherwise your equation looks like "x-x=0"

if you want to make sure your "pos1$pos2" returns a positive value you can write "abs(pos1-$pos2)"

you go to lengthy effort to produce some variables but you don't use them to move or rotate?


01-20-2004, 11:54 AM
Hi Brubin

Thanx for answering! What I wanted to do with my speed varaiable was to mesure the distance travelled from frame to frame. So its not a typing error when i again assign the varaible pos2 with localPos[0]. If you uncomment the rotate line and print the $speed varable you will se that its gives a constant value equal to the x value of the move command, regardless i wich direction its going in worldspace. What confuses me is why dont i get at constant value of speed when it rotates at the same time?

Maybe this is not the best way of calculating an objects speed along a local axis, but I'm really new to this so this is the only way I could figure out.


01-20-2004, 08:03 PM
Maybe the rotate center and translate center are not in the same place. This way you get weird translation info if the object is also rotating.

:: Galactor ::

01-22-2004, 02:44 PM

I checked the pivot positions under localspace on the transformnode and they where the same. Was that what u ment?


CGTalk Moderation
01-17-2006, 05:00 AM
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.