© Copyright, 2025 G. Schaer.

SPDX-License-Identifier: GPL-3.0-only

Visualizer Example Usage

This module gives example usage of the Visualizer class. In this, we load a cube into the visualizer and update its position and color while tracking it with the camera.

[1]:
import time
import condynsate
from condynsate import __assets__ as assets
import numpy as np

First we create an instance of the Visualizer class that is running at 60 fps and not recording.

[2]:
# Create an instance of the visualizer
vis = condynsate.Visualizer(frame_rate=60.0, record=False)
You can open the visualizer by visiting the following URL:
http://127.0.0.1:7000/static/

Now that we have the visualizer window open, we can begin to manipulate what the scene looks like. In this case, let’s turn off the grid plane and the axes.

[3]:
# Turn off the axes and the ground plane
vis.set_axes(False) #Returns 0 on success
vis.set_grid(False )#Returns 0 on success
[3]:
0

We can also move the camera around in the scene. Doing so will cause the camera to automatically pan to focus on its previous target. In the default case, the target is (0, 0, 0).

[4]:
# Set the initial position of the camera
vis.set_cam_position((0.0, -3.0, 3.0)) #Returns 0 on success
[4]:
0

Next, we will add objects to the visualzier.

[6]:
# Add a plane as the ground from an obj file and apply a texture
vis.add_object('Ground', # Name of the object in the scene
               assets['plane.obj'], # The path to the file describing its geometryu
               scale=(10., 10., 1.), # We scale up the ground plane to 10x10
               tex_path=assets['concrete.png'] # We can also apply textures to objects defined via obj files
              ) #Returns 0 on success
[6]:
0
[7]:
# Add a cube from an stl file.
vis.add_object('Cube',
               assets['cube.stl'],
               color=(0.121, 0.403, 0.749), # Set initial color
               scale=(0.5, 0.5, 0.5), # Scale cube to 0.5x0.5x0.5
               position=(0., 0., 0.25) # Set initial position on ground
               ) #Returns 0 on success
[7]:
0

Next, let’s run a visualization loop. In this loop we will

  1. Move the cube object around using the set_transform function

  2. Change the color of the cube using the set_material function

  3. Set the camera’s target to look at the cube’s new position

  4. Move the camera around in space to circle the object

[8]:
N = 1750
P0 = np.array([0., 0., 0.25])
P1 = np.array([-0.5, 0.5, 4])
for i in range(N):
    t = i / (N-1)

    # Get the position and orientation to set the cube to
    p = (1-t)*P0 + t*P1
    roll = np.sin(5*t)
    pitch = np.sin(7*t)
    yaw = np.sin(11*t)

    # Transform the cube to the desired position and orientation
    vis.set_transform('Cube',
                      position=p,
                      roll=roll,
                      pitch=pitch,
                      yaw=yaw,
                      scale=(0.5, 0.5, 0.5), # You must reset the scale
                      # lest it return to default (1,1,1)
                      )

    # Select a new color to apply to the cube and apply it
    color = (0.121+np.sin(5*t),
             0.403+np.cos(7*t),
             0.749+np.cos(11*t))
    color = tuple(float(max(0,min(c,1))) for c in color)
    vis.set_material('Cube', color=color)

    # Set the camera target to the new position of the cube
    vis.set_cam_target(p)

    # Move the camera's position in a dramatic way
    vis.set_cam_position((3*np.sin(4*t), -3*np.cos(4*t), 3.+4.*t))

    # Run the updates at about triple the frame rate
    time.sleep(0.0056)

When done, terminate ensure all children threads exit gracefully.

[10]:
vis.terminate() #Returns 0 on success
[10]:
-1
[ ]: