Skip to content

Commit

Permalink
move rewrite asset logic to scene_3d
Browse files Browse the repository at this point in the history
  • Loading branch information
swheaton committed May 8, 2024
1 parent fe1de76 commit 960d88a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
44 changes: 44 additions & 0 deletions fiftyone/core/threed/scene_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,57 @@ def traverse(self, include_self=False):
Args:
include_self: whether to include the current node in the traversal
Yields:
:class:`Object3D`
"""
if include_self:
yield self

for child in self.children:
yield from child.traverse(include_self=True)

def update_asset_paths(self, asset_rewrite_paths: dict):
"""Update asset paths in this scene according to an input dict mapping.
Asset path is unchanged if it does not exist in ``asset_rewrite_paths``
Args:
asset_rewrite_paths: ``dict`` mapping asset path to new asset path
Returns:
``True`` if the scene was modified.
"""
scene_modified = False
for node in self.traverse():
for path_attribute in node._asset_path_fields:
asset_path = getattr(node, path_attribute, None)
new_asset_path = asset_rewrite_paths.get(asset_path)

if asset_path is not None and asset_path != new_asset_path:
setattr(node, path_attribute, new_asset_path)
scene_modified = True

# modify scene background paths, if any
if self.background is not None:
if self.background.image is not None:
new_asset_path = asset_rewrite_paths.get(self.background.image)
if new_asset_path != self.background.image:
self.background.image = new_asset_path
scene_modified = True

if self.background.cube is not None:
new_cube = [
asset_rewrite_paths.get(face)
for face in self.background.cube
]
if new_cube != self.background.cube:
self.background.cube = new_cube
scene_modified = True

return scene_modified

def get_scene_summary(self):
"""Returns a summary of the scene."""
node_types = Counter(map(type, self.traverse()))
Expand Down
30 changes: 1 addition & 29 deletions fiftyone/utils/data/exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,35 +1199,7 @@ def _handle_fo3d_file(self, fo3d_path, fo3d_output_path, export_mode):
elif export_mode == "symlink":
etau.symlink_file(absolute_asset_path, asset_output_path)

is_scene_modified = False

for node in scene.traverse():
for path_attribute in node._asset_path_fields:
asset_path = getattr(node, path_attribute, None)
new_asset_path = input_to_output_paths.get(asset_path)

if asset_path is not None and asset_path != new_asset_path:
setattr(node, path_attribute, new_asset_path)
is_scene_modified = True

# modify scene background paths, if any
if scene.background is not None:
if scene.background.image is not None:
new_asset_path = input_to_output_paths.get(
scene.background.image
)
if new_asset_path != scene.background.image:
scene.background.image = new_asset_path
is_scene_modified = True

if scene.background.cube is not None:
new_cube = [
input_to_output_paths.get(face)
for face in scene.background.cube
]
if new_cube != scene.background.cube:
scene.background.cube = new_cube
is_scene_modified = True
is_scene_modified = scene.update_asset_paths(input_to_output_paths)

if is_scene_modified:
# note: we can't have different behavior for "symlink" because
Expand Down

0 comments on commit 960d88a

Please sign in to comment.