Closed JeffreyWardman closed 8 months ago
Interesting, what if we reuse the deep
keyword for it:
def __copy__(self):
return self.clone(deep=False)
def __deepcopy__(self, memo):
return self.clone(deep=memo)
def copy(self, deep=True):
"""Return a copy of the object. Alias of `clone()`."""
return self.clone(deep=deep)
def clone(self, deep=True):
poly = vtk.vtkPolyData()
if deep: # if a memo object is passed this checks as True
poly.DeepCopy(self.dataset)
else:
poly.ShallowCopy(self.dataset)
if isinstance(self, vedo.Mesh):
cloned = vedo.Mesh(poly)
else:
cloned = Points(poly)
cloned.transform = self.transform.clone()
cloned.copy_properties_from(self)
cloned.name = str(self.name)
cloned.filename = str(self.filename)
cloned.info = dict(self.info)
cloned.pipeline = utils.OperationNode("clone", parents=[self], shape="diamond", c="#edede9")
if isinstance(deep, memo_class):
# if 'memo_somthing' in deep.__class__.__name__: # or check by string
memo = deep
memo[id(self)] = cloned
return cloned
this would avoid the need of exposing a potentially confusing keyword to a very commonly used method...
I've updated to your feedback. Happy to merge if you're content with it.
But is that potentially misleading? You would have to set deep=True internally in clone to ensure a deepcopy is still made via this change. It's also allowing two very distinct data types for the variable deep
.
if deep: # if a memo object is passed this checks as True
-->
if deep or isinstance(deep, dict): # if a memo object is passed this checks as True
But is that potentially misleading? You would have to set deep=True internally in clone to ensure a deepcopy is still made via this change.
If a user is doing something like e.g. mesh.copy(deep=1)
it should have the right behavior from what I see... can you confirm
It's also allowing two very distinct data types for the variable
deep
.
Yes - not very elegant... but I guess it's least disrupting of the API...
Yes deep=1 isn't a duct so will have the same behaviour as current.
This PR enables the use of
copy.deepcopy
for meshes.Use case:
Overwriting the mesh with
mesh = mesh.copy()
would update the mesh to the new memory but would point to the same object for all copies ofData
.Doing the below results in the error underneath: