# Simple rotation and translation question

Posts: 0
edited December 1969

I LOVE that Carrara is openly extensible and the samples and cookbook are helpful.
However, I really would like a sample code of how to
1. change an object's x,y,z translation
2. rotate the object
3. set a keyframe

I have achieved setting a keyframe. But every time I try to rotate an object the scale changes also.

I use the I3dShTreeElement:

``TMCCountedPtr object;``

I set the time using:

``````MicroTick inTime=180000;
scene->SetTime(inTime,true,false,false,false);``````

Thanks to Carrara's auto keyframing, any changes set will create a keyframe at the above time.
But I can't transform an object accurately. So can someone give me an example that is the best practice
that is equivalent to the following:

``````object.xPosition+=10;
object.yPosition+=10;
object.zPosition+=10;

object.xRotation=45 degrees;
object.yRotation=45 degrees;
object.zRotation=45 degrees;``````

I appreciate any help greatly.

## Comments

• Posts: 0
edited September 2012

``````
TTreeTransform tr=tree->GetLocalTreeTransform();
TVector3 &Offset=tr.GetOffset();
Offset= TVector3(Offset.X(), Offset.Y(), newZ); // <<< change X Y Z here. You have old Offset to make relative move.
tr.SetOffset(Offset);

// rotation is more complicated, and there are several ways to do it.
// here is one way, which makes the object point it's Z axis towards
// a vector (calculated elsewhere) without changing the yaw.
TVector3 R1;     // unit vector that points where the object's X axis is: (1,0,0) = no rotation
TVector3 R2;     // unit vector that points where the object's Y axis is: (0,1,0) = no rotation
TVector3 R3;     // unit vector that points where the object's Z axis is: (0,0,1) = no rotation
tr.GetRotation().GetColumns(R1,R2,R3);

R3=normal;
R1=R2 ^ R3;
R1.Normalize(R1);
R2=R3 ^ R1;
R2.Normalize(R2);

TMatrix33 RM=TMatrix33();
RM.SetColumns(R1,R2,R3);
tr.SetRotation(RM);
// for other ways to do rotation, Google "matrix transformation rotation"

// when you're done, apply the transform to the object:
tree->SetLocalTreeTransform(tr, kApplyConstraint, false, false, kXTreeBehaviorDefault, false, true);

``````

If you don't want to handle matrixes, then look at SetPhyThetaPsy() or Euler angles. But Matrixes are more flexible, and are easier to do things like point at another object.

Post edited by briandaz_3e696c2bd8 on
Sign In or Register to comment.