pinocchio  2.4.0-dirty
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Display a model using Meshcat
1 # This examples shows how to load and move a robot in meshcat.
2 # Note: this feature requires Meshcat to be installed, this can be done using
3 # pip install --user meshcat
4 
5 import pinocchio as pin
6 pin.switchToNumpyMatrix()
7 import numpy as np
8 import sys
9 import os
10 from os.path import dirname, join, abspath
11 
12 from pinocchio.visualize import MeshcatVisualizer
13 
14 # Load the URDF model.
15 # Conversion with str seems to be necessary when executing this file with ipython
16 pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))),"models")
17 
18 model_path = join(pinocchio_model_dir,"others/robots")
19 mesh_dir = model_path
20 urdf_filename = "talos_reduced.urdf"
21 urdf_model_path = join(join(model_path,"talos_data/urdf"),urdf_filename)
22 
23 model, collision_model, visual_model = pin.buildModelsFromUrdf(urdf_model_path, mesh_dir, pin.JointModelFreeFlyer())
24 
25 viz = MeshcatVisualizer(model, collision_model, visual_model)
26 
27 # Start a new MeshCat server and client.
28 # Note: the server can also be started separately using the "meshcat-server" command in a terminal:
29 # this enables the server to remain active after the current script ends.
30 #
31 # Option open=True pens the visualizer.
32 # Note: the visualizer can also be opened seperately by visiting the provided URL.
33 try:
34  viz.initViewer(open=True)
35 except ImportError as err:
36  print("Error while initializing the viewer. It seems you should install Python meshcat")
37  print(err)
38  sys.exit(0)
39 
40 # Load the robot in the viewer.
41 viz.loadViewerModel()
42 
43 # Display a robot configuration.
44 q0 = pin.neutral(model)
45 viz.display(q0)
46 
47 # Display another robot.
48 viz2 = MeshcatVisualizer(model, collision_model, visual_model)
49 viz2.initViewer(viz.viewer)
50 viz2.loadViewerModel(rootNodeName = "pinocchio2")
51 q = q0.copy()
52 q[1] = 1.0
53 viz2.display(q)
54