SOLVED Example Python Code "Add a Custom User Data": Not Working

Hi,

There is a code in the documentation file that allocates a new user data
Here is the code

import c4d

def AddLongDataType(obj):
  if obj is None: return

  bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_LONG) # Create default container
  bc[c4d.DESC_NAME] = "Test"                        # Rename the entry

  element = obj.AddUserData(bc)     # Add userdata container
  obj[element] = 30                 # Assign a value
  c4d.EventAdd()                    # Update

I select and object and execute the code. However, nothing happens.
Is there a way around this?

Thank you.

Edit a_block: Fixed code tags.

Hi @bentraje,

Here is the code that can do:

import c4d

def AddLongDataType(obj):
    if obj is None: return

    bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_LONG) # Create default container
    bc[c4d.DESC_NAME] = "Test" # Rename the entry

    element = obj.AddUserData(bc) # Add userdata container
    obj[element] = 30 # Assign a value
    c4d.EventAdd() # Update

def main():
    obj = doc.GetActiveObject()    # Here you get selected/active object from scene
    AddLongDataType(obj)           # Call the AddLongDataType function to add user data to selected object

if __name__=='__main__':           # The "actual" place that code start to execute 
    main()                         # Call "main" function, start the processing

Hi,
I have fixed the code tags in the first post, see Markdown help.
Please also consider using tags, see Read Before Posting.
And no worries ☺
Cheers,
Andreas

@eziopan Thanks mate. It works as expected.

@a_block Thanks. Will double check next time.