-
Notifications
You must be signed in to change notification settings - Fork 1
add support for scene #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
usd-core | ||
usd-core | ||
compas >= 2.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import compas | ||
from compas.scene import Scene | ||
from compas.geometry import Box, Sphere, Translation | ||
from compas.datastructures import Mesh | ||
from compas_usd.conversions import stage_from_scene | ||
|
||
scene = Scene() | ||
group1 = scene.add_group(name="Boxes") | ||
group2 = scene.add_group(name="Spheres", transformation=Translation.from_vector([0, 5, 0])) | ||
|
||
mesh = Mesh.from_obj(compas.get("tubemesh.obj")) | ||
mesh.flip_cycles() | ||
scene.add(mesh, name="MeshObj", transformation=Translation.from_vector([-5, 0, 0])) | ||
|
||
for i in range(5): | ||
group1.add(Box(0.5), name=f"Box{i}", transformation=Translation.from_vector([i, 0, 0])) | ||
group2.add(Sphere(0.5), name=f"Sphere{i}", transformation=Translation.from_vector([i, 0, 0])) | ||
|
||
print(scene) | ||
|
||
stage = stage_from_scene(scene, "temp/scene.usda") | ||
print(stage) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from compas.scene import Scene | ||
from compas.scene import SceneObject | ||
from compas.data import Data | ||
from compas.geometry import Box | ||
from compas.geometry import Sphere | ||
from compas.geometry import Transformation | ||
from compas.datastructures import Mesh | ||
from compas_usd.conversions import prim_from_transformation | ||
from compas_usd.conversions import prim_from_box | ||
from compas_usd.conversions import prim_from_sphere | ||
from compas_usd.conversions import prim_from_mesh | ||
|
||
from pxr import Usd, UsdGeom | ||
|
||
|
||
def stage_from_scene(scene: Scene, file_path: str) -> Usd.Stage: | ||
""" | ||
Converts a :class:`compas.scene.Scene` to a USD stage. | ||
|
||
Parameters | ||
---------- | ||
scene : :class:`compas.scene.Scene` | ||
The scene to convert. | ||
file_path : str | ||
The file path to the USD stage. | ||
|
||
Returns | ||
------- | ||
:class:`pxr.Usd.Stage` | ||
The USD stage. | ||
""" | ||
stage = Usd.Stage.CreateNew(file_path) | ||
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z) | ||
for obj in scene.root.children: | ||
prim_from_sceneobject(stage, obj, parent_path=[scene.name]) | ||
|
||
stage.Save() | ||
return stage | ||
|
||
|
||
def prim_from_sceneobject(stage: Usd.Stage, sceneobject: SceneObject, parent_path=[]) -> Usd.Prim: | ||
""" | ||
Converts a :class:`compas.scene.SceneObject` to a USD prim. | ||
|
||
Parameters | ||
---------- | ||
stage : :class:`pxr.Usd.Stage` | ||
The USD stage. | ||
sceneobject : :class:`compas.scene.SceneObject` | ||
The scene object to convert. | ||
parent_path : list[str], optional | ||
The path to the parent prim. | ||
|
||
Returns | ||
------- | ||
:class:`pxr.Usd.Prim` | ||
The USD prim. | ||
""" | ||
path = parent_path + [f"{sceneobject.name}"] | ||
|
||
transformation = ( | ||
sceneobject.transformation | ||
if sceneobject.transformation is not None | ||
else Transformation() | ||
) | ||
|
||
prim = prim_from_transformation(stage, "/" + "/".join(path), transformation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this path concat using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we not use |
||
if sceneobject.item is not None: | ||
prim_from_item(stage, sceneobject.item, parent_path=path) | ||
|
||
for child in sceneobject.children: | ||
prim_from_sceneobject(stage, child, parent_path=path) | ||
|
||
return prim | ||
|
||
|
||
def prim_from_item(stage: Usd.Stage, item: Data, parent_path=[]) -> Usd.Prim: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
""" | ||
Converts a :class:`compas.scene.SceneObject` to a USD prim. | ||
|
||
Parameters | ||
---------- | ||
stage : :class:`pxr.Usd.Stage` | ||
The USD stage. | ||
item : :class:`compas.scene.SceneObject` | ||
The item to convert. | ||
parent_path : list[str], optional | ||
The path to the parent prim. | ||
|
||
Returns | ||
------- | ||
:class:`pxr.Usd.Prim` | ||
The USD prim. | ||
""" | ||
path = parent_path + [f"{item.name}"] | ||
if isinstance(item, Box): | ||
prim = prim_from_box(stage, "/" + "/".join(path), item) | ||
elif isinstance(item, Sphere): | ||
prim = prim_from_sphere(stage, "/" + "/".join(path), item) | ||
elif isinstance(item, Mesh): | ||
prim = prim_from_mesh(stage, "/" + "/".join(path), item) | ||
Comment on lines
+96
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question about whether these paths using the unix slash work on Windows. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment about using |
||
return prim |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a bit convoluted syntax, why not plain: