NVlabs / sim-web-visualizer

Web Based Visualizer for Simulation Environments
MIT License
290 stars 9 forks source link

Cannot visualize sapien toy example #2

Closed aartykov closed 7 months ago

aartykov commented 10 months ago

Hello. I tried to visualize a toy example of sapien, but could not achieve. The example code is given below. Could you please help me?

`` import sapien.core as sapien from sapien.utils import Viewer import numpy as np

from meshcat.servers.zmqserver import start_zmq_server_as_subprocess from sim_web_visualizer import create_sapien_visualizer, bind_visualizer_to_sapien_scene

def main():

create simulation engine

engine = sapien.Engine()

# Create renderer
renderer = sapien.SapienRenderer()
engine.set_renderer(renderer)

# Create a simulation scene
scene = engine.create_scene()
scene.set_timestep(1 / 100.0) # Set the simulation frequency

# Add actors(rigid bodies)
scene.add_ground(altitude=0) # Add a ground
actor_builder = scene.create_actor_builder()
actor_builder.add_box_collision(half_size=[0.5,0.5,0.5])
actor_builder.add_box_visual(half_size=[0.5,0.5,0.5], color=[1., 0., 0.])
box = actor_builder.build(name='box') # Add a box
box.set_pose(sapien.Pose(p=[0, 0, 0.5]))

# Add some lights so that you can observe the scene
scene.set_ambient_light([0.5, 0.5, 0.5])
scene.add_directional_light([0, 1, -1], [0.5, 0.5, 0.5])

"""
viewer = Viewer(renderer)  # Create a viewer (window)
viewer.set_scene(scene)  # Bind the viewer and the scene

# The coordinate frame in Sapien is: x(forward), y(left), z(upward)
# The principle axis of the camera is the x-axis
viewer.set_camera_xyz(x=-4, y=0, z=2)
# The rotation of the free camera is represented as [roll(x), pitch(-y), yaw(-z)]
# The camera now looks at the origin
viewer.set_camera_rpy(r=0, p=-np.arctan2(2, 4), y=0)
viewer.window.set_camera_parameters(near=0.05, far=100, fovy=1)

while not viewer.closed:  # Press key q to quit
    scene.step()  # Simulate the world
    scene.update_render()  # Update the world to the renderer
    viewer.render()
"""

##### HEADLESS VIEWER #######

"""
def wrapped_setup_scene(scene_config: Optional[sapien.SceneConfig] = None):
    if scene_config is None:
        scene_config = self._get_default_scene_config()
    self._scene = self._engine.create_scene(scene_config)
    self._scene.set_timestep(1.0 / self._sim_freq)
    self._scene = bind_visualizer_to_sapien_scene(self._scene, self._engine, self._renderer)

def wrapped_setup_viewer(self):
    self._viewer.set_scene(self._scene._scene)
    self._viewer.scene = self._scene
    self._viewer.toggle_axes(False)
    self._viewer.toggle_camera_lines(False)
"""

start_zmq_server_as_subprocess()
create_sapien_visualizer(port=6000, host="localhost", keep_default_viewer=False) 

scene = bind_visualizer_to_sapien_scene(scene, engine, renderer)
#sapien_env.BaseEnv._setup_scene = wrapped_setup_scene
#sapien_env.BaseEnv._setup_viewer = wrapped_setup_viewer

viewer = Viewer(renderer)  # Create a viewer (window)
viewer.set_scene(scene)  # Bind the viewer and the scene
viewer.scene = scene
viewer.toggle_axes(False)
viewer.toggle_camera_lines(False)

#############################
while True:  
    try: 
        scene.step()  # Simulate the world
        scene.update_render()  # Update the world to the renderer

    except KeyboardInterrupt:
        break

if name == 'main': main()

yzqin commented 9 months ago

Hi @aartykov

Apologies for the delay in responding. We've committed a hello_world.py script in commit 5866ed1, which essentially mirrors the example you furnished, drawing from the SAPIEN tutorial.

The primary consideration is the necessity to initialize the sapien scene prior to instantiating any objects. This step ensures that the visualizer is aware of all objects within the scene. Here's the revised snippet of code that should function correctly:

import sapien.core as sapien
from meshcat.servers.zmqserver import start_zmq_server_as_subprocess
from sim_web_visualizer import create_sapien_visualizer, bind_visualizer_to_sapien_scene

def main():
    # create simulation engine
    engine = sapien.Engine()

    # Create renderer
    renderer = sapien.SapienRenderer()
    engine.set_renderer(renderer)

    # Create a simulation scene
    create_sapien_visualizer(port=6000, host="localhost", keep_default_viewer=False)
    scene = engine.create_scene()
    scene = bind_visualizer_to_sapien_scene(scene, engine, renderer)
    scene.set_timestep(1 / 100.0)  # Set the simulation frequency

    # Add actors(rigid bodies)
    scene.add_ground(altitude=0)  # Add a ground
    actor_builder = scene.create_actor_builder()
    actor_builder.add_box_collision(half_size=[0.5, 0.5, 0.5])
    actor_builder.add_box_visual(half_size=[0.5, 0.5, 0.5], color=[1.0, 0.0, 0.0])
    box = actor_builder.build(name="box")  # Add a box
    box.set_pose(sapien.Pose(p=[0, 0, 0.5]))

    # Add some lights so that you can observe the scene
    scene.set_ambient_light([0.5, 0.5, 0.5])
    scene.add_directional_light([0, 1, -1], [0.5, 0.5, 0.5])

    #############################
    while True:
        try:
            scene.step()  # Simulate the world
            scene.update_render()  # Update the world to the renderer

        except KeyboardInterrupt:
            break

if __name__ == "__main__":
    start_zmq_server_as_subprocess()
    main()