Hello,
After some research in the forum, i've found this post that show how to react to a button pressed.
With that, it juste a matter of using AddUserData and his friend RemoveUserData
Let me try to explain a bit more what i've done.
In the UserData tab, i've a got a group that will be used as a container, so i can add or remove thing inside without touching other userdata. I just browse through all user data using GetUserDataContainer and check the parent of the fields with DESC_PARENTGROUP
I'm pretty sure you will go through this code easily but if you got any questions, feel free to ask.
i'm adding the file so you can play with the code directly
AddRemoveGroup.c4d
And the code. At the top, i've got the ID of both button and the group that will serve as a container.
import c4d
# the group id where all the fiels will be created.
addButtonID = 1
removeButtonID = 2
mainGroupID = 3
def AddGroupOfUserData():
"""
Creating the group with differents field.
Called by button Add
"""
obj = op.GetObject() # get the object where we want the user data
parentGroup = obj.GetUserDataContainer()[mainGroupID][0]
newGroup = CreateGroup(parentGroup)
nameString = AddString(newGroup, "Name")
check = AddBool (newGroup, "Use Sound")
timeField = AddTime(newGroup, "StartTime")
AddSoundPath(newGroup, "Sound")
#as function are returning the id of the userdata we can change their value
obj[nameString] = "Default value for the string"
#Set the bool to tru by default
obj[check] = True
#set the time to 0
obj[timeField] = c4d.BaseTime(0)
def RemoveGroupOfUserData():
"""
this will iterate trough all group and remove the last one
including his children
"""
obj = op.GetObject() # get the object where we want the user data
lastGroupID, count = GetLastGroupID()
if lastGroupID > -1:
# we find a group so let remove all the child of that group we found
for descID, bc in op.GetObject().GetUserDataContainer():
if IsChildOfGroup(bc, lastGroupID):
obj.RemoveUserData(descID)
del(obj[descID]) #remove the information in the basecontainer
obj.RemoveUserData(lastGroupID)
del(obj[lastGroupID])
obj.Message(c4d.MSG_UPDATE)
def IsChildOfGroup(bc, groupID = mainGroupID):
"""
Check if the parent is the same id of groupID
"""
if bc[c4d.DESC_PARENTGROUP].GetDepth() > 1:
if bc[c4d.DESC_PARENTGROUP][1].id == groupID:
return True
return False
def GetLastGroupID(parent = None):
"""
get the last group in the user data
"""
groupID = -1
count = 0
for descID, bc in op.GetObject().GetUserDataContainer():
if descID[1].dtype == c4d.DTYPE_GROUP:
if IsChildOfGroup(bc):
count += 1
groupID = descID[1].id
return groupID, count
def CreateGroup(parent, trackNumber = -1):
"""
this will create a group in the user data below "myGroup" with all the different data needed
"""
if trackNumber < 0:
# we must calculate the track number
lastGroupID, trackNumber = GetLastGroupID()
#Get the default container for the group type
bc = c4d.GetCustomDatatypeDefault(c4d.DTYPE_GROUP)
longName = "Track " + str(trackNumber)
shortName = "Track" + str(trackNumber)
bc[c4d.DESC_NAME] = longName
bc[c4d.DESC_SHORT_NAME] = shortName
bc[c4d.DESC_TITLEBAR] = True
bc[c4d.DESC_GUIOPEN] = False
bc[c4d.DESC_PARENTGROUP] = parent
#return the group id added so we can add other element to it
return op.GetObject().AddUserData(bc)
def AddBool (group, name):
"""
this will create a boolean gadget in the group
"""
bc = c4d.GetCustomDatatypeDefault(c4d.DTYPE_BOOL)
bc[c4d.DESC_NAME] = name
bc[c4d.DESC_SHORT_NAME] = name
bc[c4d.DESC_PARENTGROUP] = group
return op.GetObject().AddUserData(bc)
def AddTime(group, name):
"""
this will add the field start time for exemple
"""
bc = c4d.GetCustomDatatypeDefault(c4d.DTYPE_TIME)
bc[c4d.DESC_NAME] = name
bc[c4d.DESC_SHORT_NAME] = name
#bc[c4d.DESC_UNIT] = c4d.DESC_UNIT_TIME
bc[c4d.DESC_PARENTGROUP] = group
return op.GetObject().AddUserData(bc)
def AddSoundPath(group, name):
"""
this will add the field for file
"""
bc = c4d.GetCustomDatatypeDefault(c4d.DTYPE_FILENAME)
bc[c4d.DESC_NAME] = name
bc[c4d.DESC_SHORT_NAME] = name
bc[c4d.DESC_PARENTGROUP] = group
return op.GetObject().AddUserData(bc)
def AddString(group, name):
"""
this will add a static string to a group
"""
bc = c4d.GetCustomDatatypeDefault(c4d.DTYPE_STATICTEXT)
bc[c4d.DESC_NAME] = name
bc[c4d.DESC_SHORT_NAME] = name
#bc[c4d.DESC_CUSTOMGUI] = c4d.CUSTOMGUI_STATICTEXT
bc[c4d.DESC_PARENTGROUP] = group
return op.GetObject().AddUserData(bc)
def message(msg_type, data) :
if msg_type == c4d.MSG_NOTIFY_EVENT:
event_data = data['event_data']
if event_data['msg_id'] == c4d.MSG_DESCRIPTION_COMMAND:
desc_id = event_data['msg_data']['id']
if desc_id[1].id == addButtonID:
AddGroupOfUserData()
elif desc_id[1].id == removeButtonID:
RemoveGroupOfUserData()
def main() :
pass