Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Beginner:How can i set a key-frame and value to a cube by python?

    Cinema 4D SDK
    3
    5
    2.2k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      ilad
      last edited by

      Hi~As a crazy beginner.
      what puzzled me recently is how I can set a Key-frame and value to a cube
      my question is so simple while the existed codes are not so friendly to a beginner due to the no explanation
      (BTW, the systematic python-C4D video tutorial is so rare 😅 )

      Python question:
      **I want to set cube Y position in 0cm in 1st key-frame and 100cm in 10th key-frame, BTW, let them ease in and out **

      未命名图片11.png

      import c4d
      
      
      
      input = c4d.BaseObject(5159)
      #????????????????
      #how to set key-frame?
      
      doc.InsertObject(input)
      
      c4d.EventAdd()
      
      
      

      How to do it ?
      Thanks if you can help me ,i know my question is simple,but i am in the way to be professional.😊

      1 Reply Last reply Reply Quote 0
      • ManuelM
        Manuel
        last edited by

        Hello,

        I've marked your thread as a question, feel free to mark it as solved when you think it is, see this thread for information : Q&A New Functionality.

        I have to make things clear here. Your screenshot show you are using a generator while you code add object to the document. You can't modify the scene in a generator. It's forbidden. For this kind of scenario, (add object to a scene and tracks and keys) it's better to use a script.

        a few links to have information, even if they are about c++ there are lot of information there.

        • DescID Manual
        • track manual
        • curve manual
        • key manual

        You have to create a track for each vector (position, rotation, scale) and for each vector's component (X,Y,Z)
        I've create a script example for only the position X parameter. Feel free to create a function for that and add other track (Y, Z and rotation, scale etc)

        import c4d
        
        # Welcome to the world of Python
        
        
        
        # Main function
        def main():
            # Creates the object in memory
            obj = c4d.BaseObject(c4d.Ocube)
            
            # Creates the track in memory. Defined by it's DESCID    
            trY = c4d.CTrack(obj, c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_POSITION, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0)))
            
            # Gets Curves for the track
            curveY = trY.GetCurve()
            
            # Retrieves the current time
            keyTime = c4d.BaseTime(0)
            
        
            # Adds the keys
            added = curveY.AddKey(keyTime)
            
            # Checks for error
            if added is None:
                raise TypeError("cannont create a key")
            
            # Retrieves the inserted key
            firstKey = added["key"]
            kIndex = added["nidx"]
            
            # Sets the value of the key
            firstKey.SetValue(curveY, 0)
            
            # Changes it's interpolation
            firstKey.SetInterpolation(curveY,c4d.CINTERPOLATION_SPLINE)
            
            # Sets the key to default status AutoTangent etc...
            curveY.SetKeyDefault(doc,kIndex)
        
            # Adds another key
            keyTime = c4d.BaseTime(10, doc.GetFps())
            added = curveY.AddKey(keyTime)
            if added is None:
                raise TypeError("cannont create a key")
            secondKey = added["key"]
            kIndex = added["nidx"]
            secondKey.SetValue(curveY, 100)
            secondKey.SetInterpolation(curveY, c4d.CINTERPOLATION_SPLINE)
            curveY.SetKeyDefault(doc, kIndex)
            
        
            # Inserts track to the object
            obj.InsertTrackSorted(trY)
        
            # Inserts the object in document
            doc.InsertObject(obj)
            
           
            
            # Pushes an update event to Cinema 4D
            c4d.EventAdd()    
        
        
        
        # Execute main()
        if __name__=='__main__':
            main()
        

        Cheers
        Manuel

        MAXON SDK Specialist

        MAXON Registered Developer

        I T 2 Replies Last reply Reply Quote 1
        • I
          ilad @Manuel
          last edited by

          @m_magalhaes
          Thanks !!!! You certainly help me a lot!!!😁
          I spend around half an hour to deeply understand code with the reference of the official document!
          so excited that I figure out this problem!

          Btw, what if I want to add the key to 'Size Y'to this cube,? I should replace

          ..........
          c4d.DescLevel(c4d.ID_BASEOBJECT_POSITION, c4d.DTYPE_VECTOR, 0)
          .........
          
          

          with

          ..........
          c4d.DescLevel(c4d.PRIM_CUBE_LEN, c4d.DTYPE_VECTOR_Y, 0)
          ...........
          
          

          right?

          1 Reply Last reply Reply Quote 0
          • T
            tummosoft @Manuel
            last edited by

            @m_magalhaes Thanks ! You certainly help me a lot!

            1 Reply Last reply Reply Quote 0
            • ManuelM
              Manuel
              last edited by Manuel

              hello,
              thanks @tummosoft, it's nice to see it's helpful 😄

              @ilad
              By the way instead of ID_BASEOBJECT_POSITION you could have use ID_BASEOBJECT_REL_POSITION. Both Ids are the same.

              If you want to know how to get those ids you can use the python console and drag and drop parameters, more information on this page

              In this case the DescID is composed of two DescLevel. The first define the Vector type, the second de Float type for X, Y and Z.

              (DescID can have less or more levels)

              To change the Size.Y of the cube, the descID will be :

              c4d.DescID(c4d.DescLevel(c4d.PRIM_CUBE_LEN, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0)))
              

              To change the scale of the object the descID will be :

              c4d.DescID(c4d.DescLevel(c4d.ID_BASEOBJECT_REL_SCALE, c4d.DTYPE_VECTOR, 0), c4d.DescLevel(c4d.VECTOR_Y, c4d.DTYPE_REAL, 0)))
              

              Cheers
              Manuel

              MAXON SDK Specialist

              MAXON Registered Developer

              1 Reply Last reply Reply Quote 1
              • First post
                Last post