panda3d_visualizer.py
Go to the documentation of this file.
1 import warnings
2 
3 import numpy as np
4 
5 from .. import pinocchio_pywrap_default as pin
6 from ..utils import npToTuple
7 from .base_visualizer import BaseVisualizer
8 
9 try:
10  import hppfcl
11 
12  WITH_HPP_FCL_BINDINGS = True
13 except ImportError:
14  WITH_HPP_FCL_BINDINGS = False
15 
16 
18  """
19  A Pinocchio display using panda3d engine.
20  """
21 
22  def initViewer(self, viewer=None, load_model=False): # pylint: disable=arguments-differ
23  """Init the viewer by attaching to / creating a GUI viewer."""
24  self.visual_group = None
25  self.collision_group = None
26  self.display_visuals = False
27  self.display_collisions = False
28  self.viewer = viewer
29 
30  from panda3d_viewer import Viewer as Panda3dViewer
31 
32  if viewer is None:
33  self.viewer = Panda3dViewer(window_title="python-pinocchio")
34 
35  if load_model:
36  self.loadViewerModel(group_name=self.model.name)
37 
38  def loadViewerModel(self, group_name, color=None): # pylint: disable=arguments-differ
39  """Create a group of nodes displaying the robot meshes in the viewer."""
40  self.visual_group = group_name + "/visuals"
41  self.collision_group = group_name + "/collisions"
42 
43  self.viewer.append_group(self.visual_group)
44  self.viewer.append_group(self.collision_group)
45 
46  def append(root, obj):
47  geom = obj.geometry
48  if WITH_HPP_FCL_BINDINGS and isinstance(geom, hppfcl.ShapeBase):
49  # append a primitive geometry
50  if isinstance(geom, hppfcl.Capsule):
51  r, fl = geom.radius, 2 * geom.halfLength
52  self.viewer.append_capsule(root, obj.name, r, fl)
53  elif isinstance(geom, hppfcl.Cylinder):
54  r, fl = geom.radius, 2 * geom.halfLength
55  self.viewer.append_cylinder(root, obj.name, r, fl)
56  elif isinstance(geom, hppfcl.Box):
57  size = npToTuple(2.0 * geom.halfSide)
58  self.viewer.append_box(root, obj.name, size)
59  elif isinstance(geom, hppfcl.Sphere):
60  self.viewer.append_sphere(root, obj.name, geom.radius)
61  else:
62  msg = f"Unsupported geometry type for {obj.name} ({type(geom)})"
63  warnings.warn(msg, category=UserWarning, stacklevel=2)
64  return
65  else:
66  # append a mesh
67  scale = npToTuple(obj.meshScale)
68  self.viewer.append_mesh(root, obj.name, obj.meshPath, scale)
69 
70  if obj.overrideMaterial:
71  rgba = npToTuple(obj.meshColor)
72  path = obj.meshTexturePath
73  self.viewer.set_material(root, obj.name, rgba, path)
74  elif color is not None:
75  self.viewer.set_material(root, obj.name, color)
76 
77  self.displayVisuals(False)
78  self.displayCollisions(False)
79 
80  for obj in self.visual_model.geometryObjects:
81  append(self.visual_group, obj)
82 
83  for obj in self.collision_model.geometryObjects:
84  append(self.collision_group, obj)
85 
86  self.displayVisuals(True)
87 
88  def getViewerNodeName(self, geometry_object, geometry_type):
89  """Return the name of the geometry object inside the viewer."""
90  if geometry_type is pin.GeometryType.VISUAL:
91  return self.visual_group + "/" + geometry_object.name
92  elif geometry_type is pin.GeometryType.COLLISION:
93  return self.collision_group + "/" + geometry_object.name
94 
95  def display(self, q=None):
96  """
97  Display the robot at configuration q in the viewer by placing all the bodies.
98  """
99  if q is not None:
100  pin.forwardKinematics(self.model, self.data, q)
101 
102  def move(group, model, data):
103  pin.updateGeometryPlacements(self.model, self.data, model, data)
104  name_pose_dict = {}
105  for obj in model.geometryObjects:
106  oMg = data.oMg[model.getGeometryId(obj.name)]
107  x, y, z, qx, qy, qz, qw = pin.SE3ToXYZQUATtuple(oMg)
108  name_pose_dict[obj.name] = (x, y, z), (qw, qx, qy, qz)
109  self.viewer.move_nodes(group, name_pose_dict)
110 
111  if self.display_visuals:
112  move(self.visual_group, self.visual_model, self.visual_data)
113 
114  if self.display_collisions:
115  move(self.collision_group, self.collision_model, self.collision_data)
116 
117  def displayCollisions(self, visibility):
118  """Set whether to display collision objects or not."""
119  self.viewer.show_group(self.collision_group, visibility)
120  self.display_collisions = visibility
121 
122  def displayVisuals(self, visibility):
123  """Set whether to display visual objects or not."""
124  self.viewer.show_group(self.visual_group, visibility)
125  self.display_visuals = visibility
126 
128  raise NotImplementedError()
129 
130  def setCameraTarget(self, target):
131  raise NotImplementedError()
132 
133  def setCameraPosition(self, position: np.ndarray):
134  raise NotImplementedError()
135 
136  def setCameraZoom(self, zoom: float):
137  raise NotImplementedError()
138 
139  def setCameraPose(self, pose: np.ndarray):
140  raise NotImplementedError()
141 
142  def captureImage(self, w=None, h=None):
143  raise NotImplementedError()
144 
146  raise NotImplementedError()
147 
149  raise NotImplementedError()
150 
151  def drawFrameVelocities(self, *args, **kwargs):
152  raise NotImplementedError()
153 
154 
155 __all__ = ["Panda3dVisualizer"]
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.captureImage
def captureImage(self, w=None, h=None)
Definition: panda3d_visualizer.py:142
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.setCameraTarget
def setCameraTarget(self, target)
Definition: panda3d_visualizer.py:130
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.viewer
viewer
Definition: panda3d_visualizer.py:28
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.displayVisuals
def displayVisuals(self, visibility)
Definition: panda3d_visualizer.py:122
pinocchio.visualize.base_visualizer.BaseVisualizer.displayCollisions
def displayCollisions(self, visibility)
Definition: base_visualizer.py:107
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.display
def display(self, q=None)
Definition: panda3d_visualizer.py:95
pinocchio.visualize.base_visualizer.BaseVisualizer.visual_model
visual_model
Definition: base_visualizer.py:41
pinocchio.visualize.base_visualizer.BaseVisualizer.collision_model
collision_model
Definition: base_visualizer.py:40
pinocchio.visualize.base_visualizer.BaseVisualizer.visual_data
visual_data
Definition: base_visualizer.py:58
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.display_visuals
display_visuals
Definition: panda3d_visualizer.py:26
pinocchio.visualize.base_visualizer.BaseVisualizer.data
data
Definition: base_visualizer.py:48
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.visual_group
visual_group
Definition: panda3d_visualizer.py:24
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.displayCollisions
def displayCollisions(self, visibility)
Definition: panda3d_visualizer.py:117
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.setBackgroundColor
def setBackgroundColor(self)
Definition: panda3d_visualizer.py:127
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.loadViewerModel
def loadViewerModel(self, group_name, color=None)
Definition: panda3d_visualizer.py:38
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.setCameraZoom
def setCameraZoom(self, float zoom)
Definition: panda3d_visualizer.py:136
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.collision_group
collision_group
Definition: panda3d_visualizer.py:25
pinocchio.visualize.base_visualizer.BaseVisualizer.displayVisuals
def displayVisuals(self, visibility)
Definition: base_visualizer.py:111
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.setCameraPose
def setCameraPose(self, np.ndarray pose)
Definition: panda3d_visualizer.py:139
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.drawFrameVelocities
def drawFrameVelocities(self, *args, **kwargs)
Definition: panda3d_visualizer.py:151
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.setCameraPosition
def setCameraPosition(self, np.ndarray position)
Definition: panda3d_visualizer.py:133
pinocchio.utils.npToTuple
def npToTuple(M)
Definition: bindings/python/pinocchio/utils.py:22
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.enableCameraControl
def enableCameraControl(self)
Definition: panda3d_visualizer.py:148
pinocchio.visualize.base_visualizer.BaseVisualizer.model
model
Definition: base_visualizer.py:39
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.disableCameraControl
def disableCameraControl(self)
Definition: panda3d_visualizer.py:145
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.display_collisions
display_collisions
Definition: panda3d_visualizer.py:27
pinocchio.visualize.base_visualizer.BaseVisualizer
Definition: base_visualizer.py:18
boost::fusion::append
result_of::push_front< V const, T >::type append(T const &t, V const &v)
Append the element T at the front of boost fusion vector V.
Definition: fusion.hpp:32
pinocchio.visualize.base_visualizer.BaseVisualizer.loadViewerModel
def loadViewerModel(self, *args, **kwargs)
Definition: base_visualizer.py:86
pinocchio.visualize.base_visualizer.BaseVisualizer.collision_data
collision_data
Definition: base_visualizer.py:53
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.initViewer
def initViewer(self, viewer=None, load_model=False)
Definition: panda3d_visualizer.py:22
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer
Definition: panda3d_visualizer.py:17
pinocchio.visualize.panda3d_visualizer.Panda3dVisualizer.getViewerNodeName
def getViewerNodeName(self, geometry_object, geometry_type)
Definition: panda3d_visualizer.py:88


pinocchio
Author(s):
autogenerated on Thu Dec 19 2024 03:41:32