Skip to content

Commit

Permalink
[python/viewer] Hot fix viewer issues. (#349)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexis Duburcq <alexis.duburcq@wandercraft.eu>
  • Loading branch information
duburcqa and Alexis Duburcq authored Jun 8, 2021
1 parent d125efd commit 7d62def
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cmake_minimum_required(VERSION 3.10)

# Set the build version
set(BUILD_VERSION 1.6.19)
set(BUILD_VERSION 1.6.20)

# Set compatibility
if(CMAKE_VERSION VERSION_GREATER "3.11.0")
Expand Down
3 changes: 2 additions & 1 deletion python/jiminy_py/src/jiminy_py/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,14 @@ def render(self,
# Set the camera pose if requested
if camera_xyzrpy is not None:
self.viewer.set_camera_transform(*camera_xyzrpy)
self.viewer.wait(require_client=False)

# Make sure the graphical window is open if required
if not return_rgb_array:
self.viewer.open_gui()

# Try refreshing the viewer
self.viewer.refresh(wait=True)
self.viewer.refresh()

# Compute rgb array if needed
if return_rgb_array:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,10 @@ def get_framerate(self) -> Optional[float]:
return self.framerate

def save_screenshot(self, filename: Optional[str] = None) -> bool:
# Refresh the scene if no graphical window is available
if any(isinstance(win, GraphicsWindow) for win in self.winList):
self.step()

# Generate filename based on current time if not provided
if filename is None:
template = 'screenshot-%Y-%m-%d-%H-%M-%S.png'
Expand Down Expand Up @@ -1162,6 +1166,10 @@ def get_screenshot(self,
scheduler. The framerate limit must be disable manually to avoid
such limitation.
"""
# Refresh the scene if no graphical window is available
if any(isinstance(win, GraphicsWindow) for win in self.winList):
self.step()

# Capture frame as raw texture
texture = self.buff.get_screenshot()

Expand Down
23 changes: 13 additions & 10 deletions python/jiminy_py/src/jiminy_py/viewer/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ def get_camera_transform(self) -> Tuple[Tuple3FType, Tuple3FType]:
rot = pin.Quaternion(*quat).matrix()
rpy = matrixToRpy(rot @ CAMERA_INV_TRANSFORM_PANDA3D.T)
else:
xyz, rpy = Viewer._camera_xyzrpy
xyz, rpy = deepcopy(Viewer._camera_xyzrpy)
return xyz, rpy

@__must_be_open
Expand Down Expand Up @@ -1421,25 +1421,28 @@ def set_camera_transform(self,
How to apply the transform:
- **None:** absolute.
- **'camera':** relative to the current camera pose.
- **'camera':** relative to current camera pose.
- **other:** relative to a robot frame, not accounting for the
rotation of the frame during travalling. It supports both frame
name and index in model.
:param wait: Whether or not to wait for rendering to finish.
"""
# Handling of position and rotation arguments
if position is None or rotation is None:
position_current, rotation_current = self.get_camera_transform()
position = np.asarray(position or position_current)
rotation = np.asarray(rotation or rotation_current)
if position is None or rotation is None or relative == 'camera':
position_camera, rotation_camera = self.get_camera_transform()
if position is None:
position = position_camera
if rotation is None:
rotation = rotation_camera
position = np.asarray(position)
rotation = np.asarray(rotation)

# Compute associated rotation matrix
rotation_mat = rpyToMatrix(rotation)

# Compute the relative transformation if necessary
if relative == 'camera':
H_orig = SE3(rpyToMatrix(
Viewer._camera_xyzrpy[1]), Viewer._camera_xyzrpy[0])
H_orig = SE3(rpyToMatrix(rotation_camera), position_camera)
elif relative is not None:
# Get the body position, not taking into account the rotation
if isinstance(relative, str):
Expand Down Expand Up @@ -1477,7 +1480,7 @@ def set_camera_transform(self,
translate=position_meshcat, angles=rotation_meshcat))

# Backup updated camera pose
Viewer._camera_xyzrpy = deepcopy([position, rotation])
Viewer._camera_xyzrpy = [position, rotation]

# Wait for the backend viewer to finish rendering if requested
if wait:
Expand Down Expand Up @@ -2118,7 +2121,7 @@ def refresh(self,
self.set_camera_lookat(np.zeros(3), frame)

if Viewer._camera_motion is not None:
self.set_camera_transform()
self.set_camera_transform(*Viewer._camera_xyzrpy)

# Update pose, color and scale of the markers, if any
if Viewer.backend.startswith('panda3d'):
Expand Down

0 comments on commit 7d62def

Please sign in to comment.