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

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 :sweat_smile: )

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


import c4d

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



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.:blush:


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.

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
    # Sets the key to default status AutoTangent etc...

    # 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

    # Inserts the object in document
    # Pushes an update event to Cinema 4D

# Execute main()
if __name__=='__main__':


MAXON SDK Specialist

MAXON Registered Developer

Thanks !!!! You certainly help me a lot!!!:grin:
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.PRIM_CUBE_LEN, c4d.DTYPE_VECTOR_Y, 0)


@m_magalhaes Thanks ! You certainly help me a lot!

thanks @tummosoft, it's nice to see it's helpful :D

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


MAXON SDK Specialist

MAXON Registered Developer