4 from numpy.linalg
import norm
6 from ..
import pinocchio_pywrap_default
as pin
7 from ..utils
import npToTuple
8 from .
import BaseVisualizer
13 WITH_HPP_FCL_BINDINGS =
True
15 WITH_HPP_FCL_BINDINGS =
False
19 """A Pinocchio display using Gepetto Viewer"""
22 """Return the name of the geometry object inside the viewer"""
23 if geometry_type
is pin.GeometryType.VISUAL:
25 elif geometry_type
is pin.GeometryType.COLLISION:
31 windowName="python-pinocchio",
35 """Init GepettoViewer by loading the gui and creating a window."""
38 import gepetto.corbaserver
43 "Error while importing the viewer client.\n"
44 "Check whether gepetto-gui is properly installed"
46 warnings.warn(msg, category=UserWarning, stacklevel=2)
49 self.
viewer = gepetto.corbaserver.Client()
if viewer
is None else viewer
53 window_l = gui.getWindowList()
54 if windowName
not in window_l:
60 scene_l = gui.getSceneList()
61 if sceneName
not in scene_l:
62 gui.createScene(sceneName)
64 gui.addSceneToWindow(sceneName, self.
windowID)
72 "Error while starting the viewer client.\n"
73 "Check whether gepetto-viewer is properly started"
75 warnings.warn(msg, category=UserWarning, stacklevel=2)
80 meshColor = geometry_object.meshColor
82 geom = geometry_object.geometry
83 if isinstance(geom, hppfcl.Capsule):
84 return gui.addCapsule(
85 meshName, geom.radius, 2.0 * geom.halfLength,
npToTuple(meshColor)
87 elif isinstance(geom, hppfcl.Cylinder):
88 return gui.addCylinder(
89 meshName, geom.radius, 2.0 * geom.halfLength,
npToTuple(meshColor)
91 elif isinstance(geom, hppfcl.Box):
93 return gui.addBox(meshName, w, h, d,
npToTuple(meshColor))
94 elif isinstance(geom, hppfcl.Sphere):
95 return gui.addSphere(meshName, geom.radius,
npToTuple(meshColor))
96 elif isinstance(geom, hppfcl.Cone):
98 meshName, geom.radius, 2.0 * geom.halfLength,
npToTuple(meshColor)
100 elif isinstance(geom, hppfcl.Plane)
or isinstance(geom, hppfcl.Halfspace):
101 res = gui.createGroup(meshName)
104 planeName = meshName +
"/plane"
105 res = gui.addFloor(planeName)
109 rot = pin.Quaternion.FromTwoVectors(normal, pin.ZAxis)
110 alpha = geom.d / norm(normal, 2) ** 2
111 trans = alpha * normal
112 plane_offset = pin.SE3(rot, trans)
113 gui.applyConfiguration(planeName, pin.SE3ToXYZQUATtuple(plane_offset))
114 elif isinstance(geom, hppfcl.Convex):
116 npToTuple(geom.points(geom.polygons(f)[i]))
117 for f
in range(geom.num_polygons)
120 gui.addCurve(meshName, pts,
npToTuple(meshColor))
121 gui.setCurveMode(meshName,
"TRIANGLES")
122 gui.setLightingMode(meshName,
"ON")
123 gui.setBoolProperty(meshName,
"BackfaceDrawing",
True)
125 elif isinstance(geom, hppfcl.ConvexBase):
126 pts = [
npToTuple(geom.points(i))
for i
in range(geom.num_points)]
127 gui.addCurve(meshName, pts,
npToTuple(meshColor))
128 gui.setCurveMode(meshName,
"POINTS")
129 gui.setLightingMode(meshName,
"OFF")
132 msg = f
"Unsupported geometry type for {geometry_object.name} ({type(geom)})"
133 warnings.warn(msg, category=UserWarning, stacklevel=2)
137 """Load a single geometry object"""
142 meshPath = geometry_object.meshPath
143 meshTexturePath = geometry_object.meshTexturePath
144 meshScale = geometry_object.meshScale
145 meshColor = geometry_object.meshColor
148 if WITH_HPP_FCL_BINDINGS
and isinstance(
149 geometry_object.geometry, hppfcl.ShapeBase
155 "Display of geometric primitives is supported only if "
156 "pinocchio is build with HPP-FCL bindings."
158 warnings.warn(msg, category=UserWarning, stacklevel=2)
160 success = gui.addMesh(meshName, meshPath)
163 except Exception
as e:
165 "Error while loading geometry object: "
166 f
"{geometry_object.name}\nError message:\n{e}"
168 warnings.warn(msg, category=UserWarning, stacklevel=2)
171 gui.setScale(meshName,
npToTuple(meshScale))
172 if geometry_object.overrideMaterial:
173 gui.setColor(meshName,
npToTuple(meshColor))
174 if meshTexturePath !=
"":
175 gui.setTexture(meshName, meshTexturePath)
178 """Create the scene displaying the robot meshes in gepetto-viewer"""
196 if self.collision_model
is not None:
197 for collision
in self.collision_model.geometryObjects:
201 self.collision_model
is not None and self.visual_model
is None
204 if self.visual_model
is not None:
205 for visual
in self.visual_model.geometryObjects:
214 Display the robot at configuration q in the viewer by placing all the bodies.
216 if "viewer" not in self.__dict__:
222 pin.forwardKinematics(self.model, self.data, q)
225 pin.updateGeometryPlacements(
226 self.model, self.data, self.collision_model, self.collision_data
228 gui.applyConfigurations(
231 for collision
in self.collision_model.geometryObjects
234 pin.SE3ToXYZQUATtuple(
235 self.collision_data.oMg[
236 self.collision_model.getGeometryId(collision.name)
239 for collision
in self.collision_model.geometryObjects
244 pin.updateGeometryPlacements(
245 self.model, self.data, self.visual_model, self.visual_data
247 gui.applyConfigurations(
250 for visual
in self.visual_model.geometryObjects
253 pin.SE3ToXYZQUATtuple(
254 self.visual_data.oMg[
255 self.visual_model.getGeometryId(visual.name)
258 for visual
in self.visual_model.geometryObjects
265 """Set whether to display collision objects or not"""
268 if self.collision_model
is None:
272 visibility_mode =
"ON"
274 visibility_mode =
"OFF"
276 for collision
in self.collision_model.geometryObjects:
278 gui.setVisibility(nodeName, visibility_mode)
281 """Set whether to display visual objects or not"""
284 if self.visual_model
is None:
288 visibility_mode =
"ON"
290 visibility_mode =
"OFF"
292 for visual
in self.visual_model.geometryObjects:
294 gui.setVisibility(nodeName, visibility_mode)
297 raise NotImplementedError()
300 raise NotImplementedError()
303 raise NotImplementedError()
306 raise NotImplementedError()
309 raise NotImplementedError()
312 raise NotImplementedError()
315 raise NotImplementedError()
318 raise NotImplementedError()
321 raise NotImplementedError()
324 __all__ = [
"GepettoVisualizer"]