Digital Art Zone

 
     
Simple rotation and translation question
Posted: 27 September 2012 09:31 PM   [ Ignore ]
New Member
Total Posts:  1
Joined  2008-04-16

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<I3DShTreeElementobject

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.

 

Profile
 
 
Posted: 28 September 2012 08:30 PM   [ Ignore ]   [ # 1 ]
Member
Avatar
Rank
Total Posts:  79
Joined  2006-06-18
TTreeTransform tr=tree->GetLocalTreeTransform();
  
TVector3 &Offset=tr.GetOffset();
  
OffsetTVector3(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(trkApplyConstraintfalsefalsekXTreeBehaviorDefaultfalsetrue); 

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.

 

Profile