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

    How to deepcopy DescID?

    General Talk
    2
    3
    1.0k
    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.
    • mikeudinM
      mikeudin
      last edited by

      Hi, guys! How to properly copy the dictionary with DescIDs?🤔
      Here is example code:

      import c4d 
      from copy import deepcopy
      
      def main():
          
          dId_one = c4d.DescID(c4d.DescLevel(10), c4d.DescLevel(20), c4d.DescLevel(30))
          dId_two = c4d.DescID(c4d.DescLevel(50), c4d.DescLevel(200), c4d.DescLevel(1000))
          
          dict1 = {'1':dId_one,'2':dId_two}
          dict2 =  deepcopy(dict1)
      
          print 'dict1 ',dict1
          print 'dict2 ',dict2
      
          # dict1 {'1': ((10, 0, 0), (20, 0, 0), (30, 0, 0)), '2': ((50, 0, 0), (200, 0, 0), (1000, 0, 0))}
          # dict2 {'1': ( ), '2': ( )} <--- WHY??? 
      
      if __name__ == '__main__':
          main()
      

      Checkout my python tutorials, plugins, scripts, xpresso presets and more
      https://mikeudin.net

      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by m_adam

        Hi @mikeudin, due to how our Classic Python API is built deepcopy does not work since our python objects only store pointer to our internal C++ object and not directly to other Python objects, so deepcopy fail in this regards since, the deepcopy operation, free and try to recreate these data, which is not possible since they are not PyObject but real C++ object but copy.copy does works, but since it's a shallow copy operation it only works for one level (e.g. a dict of dict of DescID will fail).

        For more information about the difference please read Shallow vs Deep Copying of Python Objects.

        However here a couple methods to copy a dict.

        import copy
        dict1 = {'1':dId_one,'2':dId_two}
        dict2 =  copy.copy(dict1)
        
        dict1 = {'1':dId_one,'2':dId_two}
        dict2 =  dict1.copy()
        
        dict1 = {'1':dId_one,'2':dId_two}
        dict2 =  dict(dict1)
        

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        mikeudinM 1 Reply Last reply Reply Quote 3
        • mikeudinM
          mikeudin @m_adam
          last edited by

          @m_adam Thank you!

          Checkout my python tutorials, plugins, scripts, xpresso presets and more
          https://mikeudin.net

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