Skip to content
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

Compute clip range and move case transform #481

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/ansys/pyensight/core/utils/omniverse_dsg_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ def update_camera(self, camera):
cam = geom_cam.GetCamera()
# LOL, not sure why is might be correct, but so far it seems to work???
cam.focalLength = camera.fieldofview
cam.clippingRange = Gf.Range1f(0.1, 10)
dist = (target_pos - cam_pos).GetLength()
cam.clippingRange = Gf.Range1f(0.1 * dist, 10.0 * dist)
look_at = Gf.Matrix4d()
look_at.SetLookAt(cam_pos, target_pos, up_vec)
trans_row = look_at.GetRow(3)
Expand Down Expand Up @@ -717,6 +718,7 @@ def __init__(self, omni: OmniverseWrapper):
self._group_prims: Dict[int, Any] = dict()
self._root_prim = None
self._sent_textures = False
self._updated_camera = False

def add_group(self, id: int, view: bool = False) -> None:
super().add_group(id, view)
Expand All @@ -732,8 +734,46 @@ def add_group(self, id: int, view: bool = False) -> None:
pass

parent_prim = self._group_prims[group.parent_id]
# get the EnSight object type and the transform matrix
obj_type = self.get_dsg_cmd_attribute(group, "ENS_OBJ_TYPE")
matrix = self.group_matrix(group)
matrix = group.matrix4x4
# Is this a "case" group (it will contain part of the camera view in the matrix)
if obj_type == "ENS_CASE":
if (not self.session.vrmode) and (not self._updated_camera):
# if in camera mode, we need to update the camera matrix so we can
# use the identity matrix on this group. The camera should have been
# created in the "view" handler
cam_name = "/Root/Cam"
cam_prim = self._omni._stage.GetPrimAtPath(cam_name) # type: ignore
geom_cam = UsdGeom.Camera(cam_prim)
# get the camera
cam = geom_cam.GetCamera()
c = cam.transform
m = Gf.Matrix4d(*matrix).GetTranspose()
# move the model transform to the camera transform
cam.transform = c * m.GetInverse()
# set the updated camera
geom_cam.SetFromCamera(cam)
# We only want to do this once
self._updated_camera = True
matrix = [
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0,
]
prim = self._omni.create_dsg_group(
group.name, parent_prim, matrix=matrix, obj_type=obj_type
)
Expand Down Expand Up @@ -875,6 +915,8 @@ def begin_update(self) -> None:
# Upload a material to the Omniverse server
self._omni.uploadMaterial()
self._sent_textures = False
# We want to update the camera a single time within this update
self._updated_camera = False

def end_update(self) -> None:
super().end_update()
Expand Down
Loading