gepetto_visualizer.py
Go to the documentation of this file.
1 from .. import pinocchio_pywrap as pin
2 from ..shortcuts import buildModelsFromUrdf, createDatas
3 from ..utils import npToTuple
4 
5 from . import BaseVisualizer
6 
7 import warnings
8 
9 try:
10  import hppfcl
11  WITH_HPP_FCL_BINDINGS = True
12 except:
13  WITH_HPP_FCL_BINDINGS = False
14 
15 class GepettoVisualizer(BaseVisualizer):
16  """A Pinocchio display using Gepetto Viewer"""
17 
18  def getViewerNodeName(self, geometry_object, geometry_type):
19  """Return the name of the geometry object inside the viewer"""
20  if geometry_type is pin.GeometryType.VISUAL:
21  return self.viewerVisualGroupName + '/' + geometry_object.name
22  elif geometry_type is pin.GeometryType.COLLISION:
23  return self.viewerCollisionGroupName + '/' + geometry_object.name
24 
25  def initViewer(self, viewer=None, windowName="python-pinocchio", sceneName="world", loadModel=False):
26  """Init GepettoViewer by loading the gui and creating a window."""
27 
28  try:
29  import gepetto.corbaserver
30  except ImportError:
31  import warnings
32  msg = ("Error while importing the viewer client.\n"
33  "Check whether gepetto-gui is properly installed"
34  )
35  warnings.warn(msg, category=UserWarning, stacklevel=2)
36 
37  try:
38  self.viewer = gepetto.corbaserver.Client() if viewer is None else viewer
39  gui = self.viewer.gui
40 
41  # Create window
42  window_l = gui.getWindowList()
43  if not windowName in window_l:
44  self.windowID = self.viewer.gui.createWindow(windowName)
45  else:
46  self.windowID = self.viewer.gui.getWindowID(windowName)
47 
48  # Create scene if needed
49  scene_l = gui.getSceneList()
50  if sceneName not in scene_l:
51  gui.createScene(sceneName)
52  self.sceneName = sceneName
53  gui.addSceneToWindow(sceneName, self.windowID)
54 
55  if loadModel:
56  self.loadViewerModel()
57  except:
58  import warnings
59  msg = ("Error while starting the viewer client.\n"
60  "Check whether gepetto-viewer is properly started"
61  )
62  warnings.warn(msg, category=UserWarning, stacklevel=2)
63 
64  def loadPrimitive(self, meshName, geometry_object):
65 
66  gui = self.viewer.gui
67 
68  meshColor = geometry_object.meshColor
69 
70  geom = geometry_object.geometry
71  if isinstance(geom, hppfcl.Capsule):
72  return gui.addCapsule(meshName, geom.radius, 2. * geom.halfLength, npToTuple(meshColor))
73  elif isinstance(geom, hppfcl.Cylinder):
74  return gui.addCylinder(meshName, geom.radius, 2. * geom.halfLength, npToTuple(meshColor))
75  elif isinstance(geom, hppfcl.Box):
76  w, h, d = npToTuple(2. * geom.halfSide)
77  return gui.addBox(meshName, w, h, d, npToTuple(meshColor))
78  elif isinstance(geom, hppfcl.Sphere):
79  return gui.addSphere(meshName, geom.radius, npToTuple(meshColor))
80  elif isinstance(geom, hppfcl.Cone):
81  return gui.addCone(meshName, geom.radius, 2. * geom.halfLength, npToTuple(meshColor))
82  elif isinstance(geom, hppfcl.Convex):
83  pts = [ npToTuple(geom.points(geom.polygons(f)[i])) for f in range(geom.num_polygons) for i in range(3) ]
84  gui.addCurve(meshName, pts, npToTuple(meshColor))
85  gui.setCurveMode(meshName, "TRIANGLES")
86  gui.setLightingMode(meshName, "ON")
87  gui.setBoolProperty(meshName, "BackfaceDrawing", True)
88  return True
89  elif isinstance(geom, hppfcl.ConvexBase):
90  pts = [ npToTuple(geom.points(i)) for i in range(geom.num_points) ]
91  gui.addCurve(meshName, pts, npToTuple(meshColor))
92  gui.setCurveMode(meshName, "POINTS")
93  gui.setLightingMode(meshName, "OFF")
94  return True
95  else:
96  msg = "Unsupported geometry type for %s (%s)" % (geometry_object.name, type(geom) )
97  warnings.warn(msg, category=UserWarning, stacklevel=2)
98  return False
99 
100  def loadViewerGeometryObject(self, geometry_object, geometry_type):
101  """Load a single geometry object"""
102 
103  gui = self.viewer.gui
104 
105  meshName = self.getViewerNodeName(geometry_object,geometry_type)
106  meshPath = geometry_object.meshPath
107  meshTexturePath = geometry_object.meshTexturePath
108  meshScale = geometry_object.meshScale
109  meshColor = geometry_object.meshColor
110 
111  try:
112  if WITH_HPP_FCL_BINDINGS and isinstance(geometry_object.geometry, hppfcl.ShapeBase):
113  success = self.loadPrimitive(meshName, geometry_object)
114  else:
115  if meshName == "":
116  msg = "Display of geometric primitives is supported only if pinocchio is build with HPP-FCL bindings."
117  warnings.warn(msg, category=UserWarning, stacklevel=2)
118  return
119  success = gui.addMesh(meshName, meshPath)
120  if not success:
121  return
122  except Exception as e:
123  msg = "Error while loading geometry object: %s\nError message:\n%s" % (geometry_object.name, e)
124  warnings.warn(msg, category=UserWarning, stacklevel=2)
125  return
126 
127  gui.setScale(meshName, npToTuple(meshScale))
128  if geometry_object.overrideMaterial:
129  gui.setColor(meshName, npToTuple(meshColor))
130  if meshTexturePath != '':
131  gui.setTexture(meshName, meshTexturePath)
132 
133  def loadViewerModel(self, rootNodeName="pinocchio"):
134  """Create the scene displaying the robot meshes in gepetto-viewer"""
135 
136  # Start a new "scene" in this window, named "world", with just a floor.
137  gui = self.viewer.gui
138  self.viewerRootNodeName = self.sceneName + "/" + rootNodeName
139 
140  if not gui.nodeExists(self.viewerRootNodeName):
141  gui.createGroup(self.viewerRootNodeName)
142 
143  self.viewerCollisionGroupName = self.viewerRootNodeName + "/" + "collisions"
144  if not gui.nodeExists(self.viewerCollisionGroupName):
145  gui.createGroup(self.viewerCollisionGroupName)
146 
147  self.viewerVisualGroupName = self.viewerRootNodeName + "/" + "visuals"
148  if not gui.nodeExists(self.viewerVisualGroupName):
149  gui.createGroup(self.viewerVisualGroupName)
150 
151  # iterate over visuals and create the meshes in the viewer
152  if self.collision_model is not None:
153  for collision in self.collision_model.geometryObjects:
154  self.loadViewerGeometryObject(collision,pin.GeometryType.COLLISION)
155  # Display collision if we have them and there is no visual
156  self.displayCollisions(self.collision_model is not None and self.visual_model is None)
157 
158  if self.visual_model is not None:
159  for visual in self.visual_model.geometryObjects:
160  self.loadViewerGeometryObject(visual,pin.GeometryType.VISUAL)
161  self.displayVisuals(self.visual_model is not None)
162 
163  # Finally, refresh the layout to obtain your first rendering.
164  gui.refresh()
165 
166  def display(self, q = None):
167  """Display the robot at configuration q in the viewer by placing all the bodies."""
168  if 'viewer' not in self.__dict__:
169  return
170 
171  gui = self.viewer.gui
172  # Update the robot kinematics and geometry.
173  if q is not None:
174  pin.forwardKinematics(self.model,self.data,q)
175 
176  if self.display_collisions:
177  pin.updateGeometryPlacements(self.model, self.data, self.collision_model, self.collision_data)
178  gui.applyConfigurations (
179  [ self.getViewerNodeName(collision,pin.GeometryType.COLLISION) for collision in self.collision_model.geometryObjects ],
180  [ pin.SE3ToXYZQUATtuple(self.collision_data.oMg[self.collision_model.getGeometryId(collision.name)]) for collision in self.collision_model.geometryObjects ]
181  )
182 
183  if self.display_visuals:
184  pin.updateGeometryPlacements(self.model, self.data, self.visual_model, self.visual_data)
185  gui.applyConfigurations (
186  [ self.getViewerNodeName(visual,pin.GeometryType.VISUAL) for visual in self.visual_model.geometryObjects ],
187  [ pin.SE3ToXYZQUATtuple(self.visual_data.oMg[self.visual_model.getGeometryId(visual.name)]) for visual in self.visual_model.geometryObjects ]
188  )
189 
190  gui.refresh()
191 
192  def displayCollisions(self,visibility):
193  """Set whether to display collision objects or not"""
194  gui = self.viewer.gui
195  self.display_collisions = visibility
196  if self.collision_model is None: return
197 
198  if visibility:
199  visibility_mode = "ON"
200  else:
201  visibility_mode = "OFF"
202 
203  for collision in self.collision_model.geometryObjects:
204  nodeName = self.getViewerNodeName(collision,pin.GeometryType.COLLISION)
205  gui.setVisibility(nodeName,visibility_mode)
206 
207  def displayVisuals(self,visibility):
208  """Set whether to display visual objects or not"""
209  gui = self.viewer.gui
210  self.display_visuals = visibility
211  if self.visual_model is None: return
212 
213  if visibility:
214  visibility_mode = "ON"
215  else:
216  visibility_mode = "OFF"
217 
218  for visual in self.visual_model.geometryObjects:
219  nodeName = self.getViewerNodeName(visual,pin.GeometryType.VISUAL)
220  gui.setVisibility(nodeName,visibility_mode)
221 
222 __all__ = ['GepettoVisualizer']
def loadViewerGeometryObject(self, geometry_object, geometry_type)
def initViewer(self, viewer=None, windowName="python-pinocchio", sceneName="world", loadModel=False)
def getViewerNodeName(self, geometry_object, geometry_type)


pinocchio
Author(s):
autogenerated on Fri Jun 23 2023 02:38:30