Animated URDF with MeshesΒΆ

This example shows how to load a URDF with STL meshes and animate it. This example must be run from within the examples folder or the main folder because it uses a hard-coded path to the URDF file and the meshes.

vis moving urdf with meshes

import os
import numpy as np
from pytransform3d.urdf import UrdfTransformManager
import pytransform3d.visualizer as pv

def animation_callback(step, n_frames, tm, graph):
    angle = 2.79253 * np.sin(2.0 * np.pi * (step / n_frames))
    tm.set_joint("joint", angle)
    return graph

BASE_DIR = "test/test_data/"
data_dir = BASE_DIR
search_path = "."
while (not os.path.exists(data_dir) and
       os.path.dirname(search_path) != "pytransform3d"):
    search_path = os.path.join(search_path, "..")
    data_dir = os.path.join(search_path, BASE_DIR)

tm = UrdfTransformManager()
with open(data_dir + "simple_mechanism.urdf", "r") as f:
    tm.load_urdf(, mesh_path=data_dir)

fig = pv.figure("URDF with meshes")
graph = fig.plot_graph(
    tm, "lower_cone", s=0.1, show_connections=True, show_visuals=True)
n_frames = 100
if "__file__" in globals():
        animation_callback, n_frames, loop=True, fargs=(n_frames, tm, graph))

Total running time of the script: ( 0 minutes 0.296 seconds)

Gallery generated by Sphinx-Gallery