UNSOLVED Insert User Data Track in Python

Hello,
I would like to add track (timeline) for each user data I have set on a object .
Here is the code I tried, but I don't know why it's not working

Here is the code I wrote :

import c4d
from c4d import gui


#Création d'une piste pour chaque donnée utilisateur
def AddAnimationTrack(op):
        
    LED = c4d.CTrack(op,c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA,1,c4d.DTYPE_GROUP),c4d.DescLevel(c4d.ID_USERDATA,2,c4d.DTYPE_GROUP),c4d.DescLevel(c4d.ID_USERDATA,3,c4d.DTYPE_REAL)))
    op.InsertTrackSorted(LED)

    return True
# Main function
def main():
    AddAnimationTrack(op)

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

I work on R21.
You can see the scene here : ADD_TRACK - 1.c4d

Thank you for helping

Your DescID looks weird. Try something like:

c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER),
                    c4d.DescLevel(userdata_entry,c4d.DTYPE_LONG))

with userdata_entry being the actual ID of the user data.

(Disclaimer: I didn't sit down and try this, but the code is from a script dealing with animations (FindTrack), so I'm confident it should work.)

@cairyn said in Insert User Data Track in Python:

c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER),
c4d.DescLevel(userdata_entry,c4d.DTYPE_LONG))

Hello ! Thank you for replying !

I've put your code but get a error :
"Traceback (most recent call last):
File "scriptmanager", line 19, in <module>
File "scriptmanager", line 15, in main
File "scriptmanager", line 9, in AddAnimationTrack
NameError: global name 'userdata_entry' is not defined"

Maybe I didn't get what I should do with "userdata_entry"

This is what the code looks like now :

import c4d
from c4d import gui


#Création d'une piste pour chaque donnée utilisateur
def AddAnimationTrack(op):

    LED = c4d.CTrack(c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER),
                    c4d.DescLevel(c4d.ID_USERDATA,3,c4d.DTYPE_LONG)))
    op.InsertTrackSorted(LED)

    return True
# Main function
def main():
    AddAnimationTrack(op)

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

@hugo-battistella userdata_entry is a variable I was using in the script. It stands for the ID of the user data entry as shown in the user data dialog on the right side, for example 1 here:

863eaf01-45bc-495d-b0cf-b5c5351eaeac-image.png

If you have that user data in your object, you can query op (the selected object) in the console like this:

op[c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER),c4d.DescLevel(1,c4d.DTYPE_REAL))]

This is also true if the data entry is part of a group.

@hugo-battistella The DescID represents a hierarchical structure, so it makes no sense to put ID_USERDATA into the second DescLevel. The first DescLevel identifies the UserData structure within the object. The second DescLevel identifies the entry in that UserData by its ID (for which you'd have to define a proper constant yourself).

Also, the constructor for a track requires the object as first parameter. This code works for me:

import c4d
from c4d import gui

def AddAnimationTrack(op):

    LED = c4d.CTrack(op, 
                c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA,c4d.DTYPE_SUBCONTAINER),
                c4d.DescLevel(1,c4d.DTYPE_REAL)))
    op.InsertTrackSorted(LED)

    return True

def main():
    AddAnimationTrack(op)
    c4d.EventAdd()

if __name__=='__main__':
    main()

(provided I have a user data with ID 1 as shown in the screenshot from my last post, and this object is selected.)

Hello @Cairyn,
thank you a lot for this answer. I try this quikly but now I have a better understanding of the Desclevels concept !

That works find ! Thank you so much

Hello @Hugo-BATTISTELLA,

thank you for reaching out to us. And thank you at @Cairyn for answering the question. We do not have to add anything here, because @Cairyn did already a fantastic job.

If you have however follow-up questions, please do not hesitate to ask them.

Cheers,
Ferdinand