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

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

    Cinema 4D SDK
    python
    3
    4
    1.5k
    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.
    • B
      bentraje
      last edited by a_block

      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.

      1 Reply Last reply Reply Quote 0
      • eZioPanE
        eZioPan
        last edited by

        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
        
        
        B 1 Reply Last reply Reply Quote 3
        • a_blockA
          a_block
          last edited by

          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

          1 Reply Last reply Reply Quote 0
          • B
            bentraje @eZioPan
            last edited by

            @eziopan Thanks mate. It works as expected.

            @a_block Thanks. Will double check next time.

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