Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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
    • Recent
    • Tags
    • Users
    • Login

    Update User Data during loop

    Scheduled Pinned Locked Moved PYTHON Development
    4 Posts 0 Posters 326 Views
    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.
    • H Offline
      Helper
      last edited by

      On 01/10/2013 at 02:38, xxxxxxxx wrote:

      Hello everybody. Below I have a very simplified version of what I'm trying to create.

      The simplified code:
      Runs a loop to get all of the user data and prints it to the console
      Creates a new user data object (just lifted that straight from the documentation)
      Runs the same loop again and print to the console again.

      The results in the console are identical and I have to run the script again to be able to access the new userdata.

      Import c4d

      def main() :
        obj = doc.SearchObject("Null")

      ud = obj.GetUserDataContainer()
       
        for id, bc in ud:
            print id, bc[1]   # The readout on console shows Test data 1 and Test data 2
           
        bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_LONG) # Create default container
        bc[c4d.DESC_NAME] = "Test data 3"                        # Rename the entry

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

      for id, bc in ud:
            print id, bc[1] #The readout on console shows Test data 1 and Test data 2 but no sign of Test data 3
           
      if __name__=='__main__':
        main()

      I know I can set all of the parameters before I create the userdata, however what I intend to do is create a group and then clone some userdata and nest it in the group without having to run the script twice.

      My question is: Can I access newly created/cloned userdata within a single execution of a script?

      Any help is much appreciated.

      Thanks

      Adam

      1 Reply Last reply Reply Quote 0
      • H Offline
        Helper
        last edited by

        On 01/10/2013 at 08:09, xxxxxxxx wrote:

        scripts are single threaded / executed from the c4d main thread. the general approach you would
        take on this would be a python tag. the python tag code single threaded too, but it allows you to
        use some fallthrough construct to split your task into two passes.

        if data not in userdata:
        UpdateMe()
        elif condition is met:
        DoSomething()
         
        edit: lol, I just realized that I have hit the posting count sweet spot for my nick 😉

        1 Reply Last reply Reply Quote 0
        • H Offline
          Helper
          last edited by

          On 01/10/2013 at 08:25, xxxxxxxx wrote:

          UserData works the same way as other objects.
          If you change it in some manner. You have to grab it again after the changes were made to get it's current values.

          import c4d  
          def main() :  
            obj = doc.SearchObject("Null")  
              
            #The master UD container  
            ud = obj.GetUserDataContainer()  
              
            #Print the current UD entries in the master container    
            for id, bc in ud:  
                print id, bc[1]  
                 
            bc = c4d.GetCustomDataTypeDefault(c4d.DTYPE_LONG)  
            bc[c4d.DESC_NAME] = "Test data 2"  
            
            element = obj.AddUserData(bc)     #Add this new userdata to the master UD container  
            obj[element] = 10                 #Assign a value to it  
            c4d.EventAdd()  
            
            #We've changed the master UD container above  
            #So we have to grab it again to get the current stuff in it  
            ud = obj.GetUserDataContainer()  
            for id, bc in ud:  
                print id, bc[1]  
                 
          if __name__=='__main__':  
            main()
          

          -ScottA

          1 Reply Last reply Reply Quote 0
          • H Offline
            Helper
            last edited by

            On 01/10/2013 at 08:36, xxxxxxxx wrote:

            @littledevil Ha, Demonic post! - I'm privileged. I'm actually using a Python Node for this bit and created a sloppy workaround which involved an Iterator Node to trigger the Python twice.

            @ScottA That's what I was looking for! Thank you. I feel like bashing my head off the desk for missing that. Solved!

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