base_visualizer.py
Go to the documentation of this file.
1 import abc
2 import time
3 from pathlib import Path
4 
5 import numpy as np
6 
7 from .. import pinocchio_pywrap_default as pin
8 from ..shortcuts import createDatas
9 
10 try:
11  import imageio
12 
13  IMAGEIO_SUPPORT = True
14 except ImportError:
15  IMAGEIO_SUPPORT = False
16 
17 
18 class BaseVisualizer(abc.ABC):
19  """
20  Pinocchio visualizers are employed to easily display a model at a given
21  configuration.
22  BaseVisualizer is not meant to be directly employed, but only to provide a uniform
23  interface and a few common methods.
24  New visualizers should extend this class and override its methods as neeeded.
25  """
26 
27  _video_writer = None
28 
29  _video_writer = None
30 
31  def __init__(
32  self,
33  model=pin.Model(),
34  collision_model=None,
35  visual_model=None,
36  copy_models=False,
37  data=None,
38  collision_data=None,
39  visual_data=None,
40  ):
41  """
42  Construct a display from the given model, collision model, and visual model.
43  If copy_models is True, the models are copied. Otherwise, they are simply kept
44  as a reference.
45  """
46 
47  if copy_models:
48  self.model = model.copy()
49  self.collision_model = collision_model.copy()
50  self.visual_model = visual_model.copy()
51  else:
52  self.model = model
53  self.collision_model = collision_model
54  self.visual_model = visual_model
55 
56  if data is None:
57  self.data = self.model.createData()
58  else:
59  self.data = data
60 
61  if collision_data is None and self.collision_model is not None:
63  else:
64  self.collision_data = collision_data
65 
66  if visual_data is None and self.visual_model is not None:
67  self.visual_data = self.visual_model.createData()
68  else:
69  self.visual_data = visual_data
70 
71  def rebuildData(self):
72  """Re-build the data objects. Needed if the models were modified.
73  Warning: this will delete any information stored in all data objects."""
74  self.data, self.collision_data, self.visual_data = createDatas(
75  self.model, self.collision_model, self.visual_model
76  )
77 
78  def getViewerNodeName(self, geometry_object, geometry_type):
79  """Return the name of the geometry object inside the viewer."""
80  pass
81 
82  def initViewer(self, *args, **kwargs):
83  """Init the viewer by loading the gui and creating a window."""
84  pass
85 
86  def loadViewerModel(self, *args, **kwargs):
87  """Create the scene displaying the robot meshes in the viewer"""
88  pass
89 
90  def reload(self, new_geometry_object, geometry_type=None):
91  """Reload a geometry_object given by its type"""
92  pass
93 
94  def clean(self):
95  """Delete all the objects from the whole scene"""
96  pass
97 
98  @abc.abstractmethod
99  def display(self, q=None):
100  """
101  Display the robot at configuration q or refresh the rendering
102  from the current placements contained in data by placing all the bodies in the
103  viewer.
104  """
105 
106  @abc.abstractmethod
107  def displayCollisions(self, visibility):
108  """Set whether to display collision objects or not."""
109 
110  @abc.abstractmethod
111  def displayVisuals(self, visibility):
112  """Set whether to display visual objects or not."""
113 
114  @abc.abstractmethod
116  """Set the visualizer background color."""
117 
118  @abc.abstractmethod
119  def setCameraTarget(self, target):
120  """Set the camera target."""
121 
122  @abc.abstractmethod
123  def setCameraPosition(self, position: np.ndarray):
124  """Set the camera's 3D position."""
125 
126  @abc.abstractmethod
127  def setCameraZoom(self, zoom: float):
128  """Set camera zoom value."""
129 
130  @abc.abstractmethod
131  def setCameraPose(self, pose: np.ndarray = np.eye(4)):
132  """Set camera 6D pose using a 4x4 matrix."""
133 
134  @abc.abstractmethod
135  def captureImage(self, w=None, h=None):
136  """Captures an image from the viewer and returns an RGB array."""
137 
138  @abc.abstractmethod
140  """Disable camera manual control"""
141 
142  @abc.abstractmethod
144  """Enable camera manual control"""
145 
146  @abc.abstractmethod
147  def drawFrameVelocities(self, *args, **kwargs):
148  """Draw current frame velocities."""
149 
150  def sleep(self, dt):
151  time.sleep(dt)
152 
153  def has_video_writer(self):
154  return self._video_writer is not None
155 
156  def play(self, q_trajectory, dt=None, callback=None, capture=False, **kwargs):
157  """
158  Play a trajectory with given time step. Optionally capture RGB images and
159  returns them.
160  """
161  nsteps = len(q_trajectory)
162  if not capture:
163  capture = self.has_video_writer()
164 
165  imgs = []
166  for i in range(nsteps):
167  t0 = time.time()
168  self.display(q_trajectory[i])
169  if callback is not None:
170  callback(i, **kwargs)
171  if capture:
172  img_arr = self.captureImage()
173  if not self.has_video_writer():
174  imgs.append(img_arr)
175  else:
176  self._video_writer.append_data(img_arr)
177  t1 = time.time()
178  elapsed_time = t1 - t0
179  if dt is not None and elapsed_time < dt:
180  self.sleep(dt - elapsed_time)
181  if capture and not self.has_video_writer():
182  return imgs
183 
184  def create_video_ctx(self, filename=None, fps=30, directory=None, **kwargs):
185  """
186  Create a video recording context, generating the output filename if necessary.
187 
188  Code inspired from https://github.com/petrikvladimir/RoboMeshCat.
189  """
190  if not IMAGEIO_SUPPORT:
191  import contextlib
192  import warnings
193 
194  warnings.warn(
195  "Video context cannot be created because imageio is not available.",
196  UserWarning,
197  )
198  return contextlib.nullcontext()
199  if filename is None:
200  if directory is None:
201  from tempfile import gettempdir
202 
203  directory = gettempdir()
204  directory = Path(directory)
205  f_fmt = "%Y%m%d_%H%M%S"
206  ext = "mp4"
207  filename = directory / time.strftime(f"{f_fmt}.{ext}")
208  return VideoContext(self, fps, filename)
209 
210 
212  def __init__(self, viz, fps, filename, **kwargs):
213  self.viz = viz
214  self.vid_writer = imageio.get_writer(filename, fps=fps, **kwargs)
215 
216  def __enter__(self):
217  print("[Entering video recording context]")
218  self.viz._video_writer = self.vid_writer
219 
220  def __exit__(self, *args):
221  self.vid_writer.close()
222  self.viz._video_writer = None
223 
224 
225 __all__ = ["BaseVisualizer"]
pinocchio.visualize.base_visualizer.BaseVisualizer.disableCameraControl
def disableCameraControl(self)
Definition: base_visualizer.py:139
pinocchio.visualize.base_visualizer.VideoContext.vid_writer
vid_writer
Definition: base_visualizer.py:214
pinocchio.visualize.base_visualizer.BaseVisualizer.has_video_writer
def has_video_writer(self)
Definition: base_visualizer.py:153
pinocchio.visualize.base_visualizer.BaseVisualizer.setCameraPose
def setCameraPose(self, np.ndarray pose=np.eye(4))
Definition: base_visualizer.py:131
pinocchio.visualize.base_visualizer.BaseVisualizer.displayCollisions
def displayCollisions(self, visibility)
Definition: base_visualizer.py:107
pinocchio.visualize.base_visualizer.BaseVisualizer.visual_model
visual_model
Definition: base_visualizer.py:41
pinocchio.visualize.base_visualizer.BaseVisualizer.setBackgroundColor
def setBackgroundColor(self)
Definition: base_visualizer.py:115
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.base_visualizer.BaseVisualizer._video_writer
_video_writer
Definition: base_visualizer.py:27
pinocchio.visualize.base_visualizer.BaseVisualizer.data
data
Definition: base_visualizer.py:48
pinocchio.visualize.base_visualizer.VideoContext.__init__
def __init__(self, viz, fps, filename, **kwargs)
Definition: base_visualizer.py:212
pinocchio.visualize.base_visualizer.BaseVisualizer.sleep
def sleep(self, dt)
Definition: base_visualizer.py:150
pinocchio.visualize.base_visualizer.BaseVisualizer.rebuildData
def rebuildData(self)
Definition: base_visualizer.py:71
pinocchio.visualize.base_visualizer.BaseVisualizer.getViewerNodeName
def getViewerNodeName(self, geometry_object, geometry_type)
Definition: base_visualizer.py:78
pinocchio.visualize.base_visualizer.BaseVisualizer.drawFrameVelocities
def drawFrameVelocities(self, *args, **kwargs)
Definition: base_visualizer.py:147
pinocchio.visualize.base_visualizer.VideoContext.__enter__
def __enter__(self)
Definition: base_visualizer.py:216
pinocchio.visualize.base_visualizer.BaseVisualizer.setCameraZoom
def setCameraZoom(self, float zoom)
Definition: base_visualizer.py:127
pinocchio.shortcuts.createDatas
def createDatas(*models)
Definition: shortcuts.py:111
pinocchio.visualize.base_visualizer.BaseVisualizer.setCameraPosition
def setCameraPosition(self, np.ndarray position)
Definition: base_visualizer.py:123
pinocchio.visualize.base_visualizer.BaseVisualizer.captureImage
def captureImage(self, w=None, h=None)
Definition: base_visualizer.py:135
pinocchio.visualize.base_visualizer.BaseVisualizer.reload
def reload(self, new_geometry_object, geometry_type=None)
Definition: base_visualizer.py:90
pinocchio::createData
ConstraintDataTpl< Scalar, Options, ConstraintCollectionTpl > createData(const ConstraintModelTpl< Scalar, Options, ConstraintCollectionTpl > &cmodel)
Definition: constraint-model-visitor.hpp:239
pinocchio.visualize.base_visualizer.BaseVisualizer.setCameraTarget
def setCameraTarget(self, target)
Definition: base_visualizer.py:119
pinocchio.visualize.base_visualizer.BaseVisualizer.create_video_ctx
def create_video_ctx(self, filename=None, fps=30, directory=None, **kwargs)
Definition: base_visualizer.py:184
pinocchio.visualize.base_visualizer.BaseVisualizer.displayVisuals
def displayVisuals(self, visibility)
Definition: base_visualizer.py:111
pinocchio.visualize.base_visualizer.BaseVisualizer.display
def display(self, q=None)
Definition: base_visualizer.py:99
pinocchio.visualize.base_visualizer.BaseVisualizer.__init__
def __init__(self, model=pin.Model(), collision_model=None, visual_model=None, copy_models=False, data=None, collision_data=None, visual_data=None)
Definition: base_visualizer.py:31
pinocchio.visualize.base_visualizer.BaseVisualizer.model
model
Definition: base_visualizer.py:39
pinocchio.visualize.base_visualizer.BaseVisualizer.enableCameraControl
def enableCameraControl(self)
Definition: base_visualizer.py:143
pinocchio.visualize.base_visualizer.VideoContext
Definition: base_visualizer.py:211
pinocchio.visualize.base_visualizer.BaseVisualizer
Definition: base_visualizer.py:18
pinocchio.visualize.base_visualizer.VideoContext.viz
viz
Definition: base_visualizer.py:213
pinocchio.visualize.base_visualizer.VideoContext.__exit__
def __exit__(self, *args)
Definition: base_visualizer.py:220
pinocchio.visualize.base_visualizer.BaseVisualizer.play
def play(self, q_trajectory, dt=None, callback=None, capture=False, **kwargs)
Definition: base_visualizer.py:156
pinocchio.visualize.base_visualizer.BaseVisualizer.clean
def clean(self)
Definition: base_visualizer.py:94
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.base_visualizer.BaseVisualizer.initViewer
def initViewer(self, *args, **kwargs)
Definition: base_visualizer.py:82


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