Skip to content

Commit

Permalink
fix bug in scene.copy
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Nov 5, 2018
1 parent b51069f commit f3e38fb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
22 changes: 22 additions & 0 deletions tests/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ def test_scaling(self):
assert scene.md5() == md5
assert converted.md5() != md5

def test_dupe(self):
m = g.get_mesh('tube.obj')

assert m.body_count == 1

s = g.trimesh.scene.split_scene(m)
assert len(s.graph.nodes) == 2
assert len(s.graph.nodes_geometry) == 1
assert len(s.duplicate_nodes) == 1
assert len(s.duplicate_nodes[0]) == 1

c = s.copy()
assert len(c.graph.nodes) == 2
assert len(c.graph.nodes_geometry) == 1
assert len(c.duplicate_nodes) == 1
assert len(c.duplicate_nodes[0]) == 1

u = s.convert_units('in', guess=True)
assert len(u.graph.nodes_geometry) == 1
assert len(u.duplicate_nodes) == 1
assert len(u.duplicate_nodes[0]) == 1

def test_3DXML(self):
s = g.get_mesh('rod.3DXML')

Expand Down
37 changes: 22 additions & 15 deletions trimesh/scene/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ def __init__(self,
# mesh name : Trimesh object
self.geometry = collections.OrderedDict()

# graph structure of instances
if graph is None:
# create a new graph
self.graph = TransformForest(base_frame=base_frame)
else:
# if we've been passed a graph use it
self.graph = graph
# create a new graph
self.graph = TransformForest(base_frame=base_frame)

# create our cache
self._cache = caching.Cache(id_function=self.md5)
Expand All @@ -61,6 +56,10 @@ def __init__(self,
self.metadata = {}
self.metadata.update(metadata)

if graph is not None:
# if we've been passed a graph override the default
self.graph = graph

def add_geometry(self,
geometry,
node_name=None,
Expand Down Expand Up @@ -684,7 +683,7 @@ def explode(self, vector=None, origin=None):

def scaled(self, scale):
"""
Return a copy of the current scene, with meshes and scene graph
Return a copy of the current scene, with meshes and scene
transforms scaled to the requested factor.
Parameters
Expand All @@ -700,11 +699,16 @@ def scaled(self, scale):
scale = float(scale)
scale_matrix = np.eye(4) * scale

transforms = np.array([self.graph[i][0]
for i in self.graph.nodes_geometry])
geometries = np.array([self.graph[i][1]
for i in self.graph.nodes_geometry])
# preallocate transforms and geometries
nodes = self.graph.nodes_geometry
transforms = np.zeros((len(nodes), 4, 4))
geometries = [None] * len(nodes)

# collect list of transforms
for i, node in enumerate(nodes):
transforms[i], geometries[i] = self.graph[node]

# result is a copy
result = self.copy()
result.graph.clear()

Expand All @@ -722,10 +726,13 @@ def scaled(self, scale):

for node, t in zip(self.graph.nodes_geometry[group],
transforms[group]):
transform = util.multi_dot([scale_matrix,
t,
np.linalg.inv(new_geom)])
# generate the new transforms
transform = util.multi_dot(
[scale_matrix,
t,
np.linalg.inv(new_geom)])
transform[:3, 3] *= scale
# update scene with new transforms
result.graph.update(frame_to=node,
matrix=transform,
geometry=geometry)
Expand Down
5 changes: 4 additions & 1 deletion trimesh/scene/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def copy(self):
------------
copied: TransformForest
"""
copied = copy.deepcopy(self)
copied = TransformForest()
copied.base_frame = copy.deepcopy(self.base_frame)
copied.transforms = copy.deepcopy(self.transforms)

return copied

def to_flattened(self, base_frame=None):
Expand Down
2 changes: 1 addition & 1 deletion trimesh/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.35.18'
__version__ = '2.35.19'

0 comments on commit f3e38fb

Please sign in to comment.