How to deepcopy DescID?



  • 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()
    


  • 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.



  • @m_adam Thank you!