Animation position/rotation using Python

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/01/2011 at 14:27, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   12 
Platform:   Windows  ;   
Language(s) :       PYTHON  ;

---------
New to C4D but very experienced elsewhere. I'm trying to animate objects' position and rotation through the python API. I'll have to animate some other things eventually too. I tried using the typical approach of document.SetTime and SetAbsPos, but that didn't seem to have any effect. Is there a recording flag that needs to be turned on? Or are CTrack and CKey supposed to be used---but how? I see some forum posts relating to this involving DescIDs, where is the (meaningful) documentation for this? (in any programming language) Can switch to COFFEE if really necessary, but C++ is unacceptable for this application. Thanks.

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 15/01/2011 at 20:28, xxxxxxxx wrote:

Well,  I can show you the code I use to rotate my planet objects in PXG.   It is using C++ but it might help you with Python.

  
//ROTATE PLANET  
//====================================================//  
Bool PlanetXObject::RotatePlanet(BaseObject *op, GeListNode* node){  
    
  if (!op) return FALSE;  
  BaseContainer *bc = ((BaseList2D* )node)->GetDataInstance();   
  if (!bc) return FALSE;  
  BaseDocument *doc = op->GetDocument();  
  if(!doc) return FALSE;  
  BaseTime time = doc->GetTime();  
  Real fps = doc->GetFps();  
  
  //Rotation Track Vector  
  DescID idRotX = DescID(DescLevel(ID_BASEOBJECT_REL_ROTATION,DTYPE_VECTOR,0),DescLevel(VECTOR_X,DTYPE_REAL,0));  
  
  //Planet COUNTER CLOCKWISE Rotation  
  //==========================================//  
  if (bc->GetBool(PLANETX_GENERAL_ENABLE_ROTATION)  &&   
      bc->GetBool(PLANETX_GENERAL_ROTATION_DIRECTION) == PLANETX_GENERAL_ROTATION_COUNTER_CLOCKWISE){  
        
      //Remove Existing Track  
      //==========================================//  
      if (op->FindCTrack(idRotX)){  
          op->FindCTrack(idRotX)->Remove();  
      }  
      //Insert the New Track  
      //==========================================//  
      CTrack *track = op->FindCTrack(idRotX);  
      if (!track){  
            
          track = CTrack::Alloc(op,idRotX);  
          if (!track) return FALSE;  
  
          op->InsertTrackSorted(track);  
      }  
  
      //Get the Base Time  
      //==========================================//  
      BaseTime bstime = time;  
  
      //Set the Frame Rate (FPS)  
      //==========================================//  
      bstime.SetDenominator(fps);  
  
      Real deNomin;  
      deNomin = (60 *fps) / bc->GetReal(PLANETX_GENERAL_ROTATION_SPEED);  
  
      //First Key will be set at 0  
      //==========================================//  
      bstime.SetNumerator(0);  
      CKey *keyH = track->GetCurve()->AddKey(bstime,0);  
      if (!keyH) return false;  
      keyH->SetValue(track->GetCurve(),0); //Set Position to 0  
  
      //Second Key will be placed at deNomin  
      //==========================================//  
      bstime.SetNumerator(deNomin);  
      CKey *keyH2 = track->GetCurve()->AddKey(bstime,0);  
      if (!keyH2) return false;  
      keyH2->SetValue(track->GetCurve(),6.283185307); //Set Position to 360  
  
      track->SetAfter(CLOOP_REPEAT);  
  }  
  
  //Planet CLOCKWISE Rotation  
  //==========================================//  
  
  else if (bc->GetBool(PLANETX_GENERAL_ENABLE_ROTATION)  &&    
           bc->GetBool(PLANETX_GENERAL_ROTATION_DIRECTION) == PLANETX_GENERAL_ROTATION_CLOCKWISE)  
  {  
      //Remove Existing Track  
      //==========================================//  
      if (op->FindCTrack(idRotX))  
      {  
          op->FindCTrack(idRotX)->Remove();  
      }  
        
      //Insert the New Track  
      //==========================================//  
      CTrack *track = op->FindCTrack(idRotX);  
      if (!track)  
      {  
          track = CTrack::Alloc(op,idRotX);  
          if (!track) return FALSE;  
          op->InsertTrackSorted(track);  
      }  
  
      //Get the Base Time  
      //==========================================//  
      BaseTime bstime = time;  
        
      //Set the Frame Rate (FPS)  
      //==========================================================================  
      bstime.SetDenominator(fps);  
            
      Real deNomin;  
      deNomin = (60 *fps) / bc->GetReal(PLANETX_GENERAL_ROTATION_SPEED);  
  
      //First Key will be set at 0  
      //==========================================================================  
      bstime.SetNumerator(0);  
      CKey *keyH = track->GetCurve()->AddKey(bstime,0);  
      if (!keyH) return false;  
        
      keyH->SetValue(track->GetCurve(),6.283185307); //Set Position to 360  
        
      //Second Key will be placed at 3 times the location of the frame rate   
      //(i.e. if FPS is set to 30, then the key will be placed at frame 45.  
      //==========================================================================  
      bstime.SetNumerator(deNomin);  
      CKey *keyH2 = track->GetCurve()->AddKey(bstime,0);  
      if (!keyH2) return false;  
      keyH2->SetValue(track->GetCurve(),0); //Set Position to 0  
  
      track->SetAfter(CLOOP_REPEAT);  
  }  
  
  else  
  {  
      //If the track exists remove it!  
      if (op->FindCTrack(idRotX))  
      {  
  
          op->FindCTrack(idRotX)->Remove();  
      }  
  }  
  return TRUE;  
}  
  

Hope that helps you.  🙂

THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

On 20/01/2011 at 23:26, xxxxxxxx wrote:

This will show you the very basics:

import c4d
  
obj = op.GetObject()
  
def main() :
    t = doc.GetTime().Get()
  
    pos = c4d.Vector(0,t*100, 0)
    rot = c4d.Vector(0,t,0)
  
    obj.SetAbsPos(pos)
    obj.SetAbsRot(rot)