AttributeError in Isaac Sim 2022.2.1: 'omni.kit' has no attribute 'viewport_legacy' - Debugging and Resolution

In Isaac Sim 2022.2.1,
When executing the script containing the following .py file, an error occurred.

https://github.com/NVIDIA-AI-IOT/robot_freespace_seg_Isaac_TAO/blob/main/IsaacSim/replicator/warehouse_freespace.py

The error is as follows:

Traceback (most recent call last):
  File "/home/nvidia/robot_freespace_seg_Isaac_TAO/IsaacSim/replicator/warehouse_freespace.py", line 430, in <module>
    max_queue_size=args.max_queue_size, floor_dr=args.floor_dr, wall_dr=args.wall_dr,light_dr=args.light_dr, yaml_path=args.yaml_path)
  File "/home/nvidia/robot_freespace_seg_Isaac_TAO/IsaacSim/replicator/warehouse_freespace.py", line 95, in __init__
    self._setup_world(scenario_path)
  File "/home/nvidia/robot_freespace_seg_Isaac_TAO/IsaacSim/replicator/warehouse_freespace.py", line 129, in _setup_world
    self.add_camera_to_viewport()
  File "/home/nvidia/robot_freespace_seg_Isaac_TAO/IsaacSim/replicator/warehouse_freespace.py", line 158, in add_camera_to_viewport
    self.viewport = omni.kit.viewport_legacy.get_viewport_interface()
AttributeError: module 'omni.kit' has no attribute 'viewport_legacy'
2023-08-31 00:48:53 [279,131ms] [Warning] [carb.audio.context] 1 contexts were leaked
./python.sh: line 41: 251664 Segmentation fault      (core dumped) $python_exe "$@" $args
There was an error running python

The relevant code is as follows:

    def add_camera_to_viewport(self):
        # Add a camera to the scene and attach it to the viewport
        self.camera_rig = UsdGeom.Xformable(create_prim("/Root/CameraRig", "Xform"))
        self.camera = create_prim("/Root/CameraRig/Camera", "Camera", position=np.array([20, 30.650, 2]), orientation=euler_angles_to_quat(np.array([90, 0.0, 0]), degrees=True))
        self.viewport = omni.kit.viewport_legacy.get_viewport_interface()
        viewport_handle = self.viewport.get_instance("Viewport")
        self.viewport_window = self.viewport.get_viewport_window(viewport_handle)
        self.viewport_window.set_active_camera(str(self.camera.GetPath()))

It is likely that an error is occurring in the code related to viewport_legacy.

Additionally, when executing the script in Isaac Sim 2022.1.1, the generation of synthetic data was successful.
I believe that the issue might be due to changes in the viewport_legacy code to something else.
However, since I would like to run this code in Isaac Sim 2022.2.1, please provide a solution if possible.

warehouse_freespace_2022.1.1.sh (2.1 KB)
warehouse_freespace_2022.2.1.sh (2.1 KB)
error (22.2 KB)

Hi @k_m - I think this post might be helpful to you: Get_default_viewport_window() doesn't work in Isaac Sim 2022.2 - #2 by rthaker

Hi there,

it could be that the error you are receiving is due to the python modules import, namely the omni related imports should be done after the creation of the SimulationApp.

Let me know if that fixes the issue for you.

Best,
Andrei

The following code changes resolved the error, but is it functioning correctly?

    def add_camera_to_viewport(self):
        # Add a camera to the scene and attach it to the viewport
        self.camera_rig = UsdGeom.Xformable(create_prim("/Root/CameraRig", "Xform"))
        self.camera = create_prim("/Root/CameraRig/Camera", "Camera", position=np.array([20, 30.650, 2]), orientation=euler_angles_to_quat(np.array([90, 0.0, 0]), degrees=True))
 
        self.active_vp_window = omni.kit.viewport.utility.get_active_viewport()
        self.active_vp_window.camera_path = self.camera.GetPath()

        kit.update()

Hi ahaidu,
Thank you for your response.
I changed the order of imports, but the same error is occurred.

before

import omni
from omni.isaac.kit import SimulationApp
kit = SimulationApp(launch_config=CONFIG)

after

from omni.isaac.kit import SimulationApp
kit = SimulationApp(launch_config=CONFIG)
import omni

I modified the add_camera_to_viewport().
As a result, the randomly placed camera sometimes captures images with a poor orientation, appearing sideways or not properly capturing the indoor scene.

While I haven’t specifically adjusted the camera’s orientation, its behavior is not correct. Images taken in version 2022.1.1 were captured correctly.

The camera-related code is as follows, but are there any issues with it?

   def setup_replicator(self):
        camera = rep.get.prims(path_pattern=str(self.camera.GetPath()))
        floor = rep.get.prims(path_pattern="/Root/Group_Floor")
        wall = rep.get.prims(path_pattern="/Root/Group_Wall")

        light_1 = rep.get.prims(path_pattern="/Root/RectLight")
        light_2 = rep.get.prims(path_pattern="/Root/RectLight_01")

        with self.rep.new_layer():
            with self.rep.trigger.on_frame():
                
                with camera:
                    rep.modify.pose(
                    position=rep.distribution.uniform((-4.39, -1.39, 0.2), (-1.30, 20, 0.3)),
                    rotation=rep.distribution.uniform((0,0, 0),(0, 360, 0)))

isaac sim 2022.1.1 (correct)

isaac sim 2022.2.1 (not correct)



Hi there,

camera randomizations do not work well when using non-replicator created cameras (rep.create.camera()). This is because the replicator cameras have a 90 deg rotation on X and Z axis and every other transformation is applied to its parenting Xform. You can either apply these rotations to your existing camera, or try using a replicator camera.

I am not sure why this would work with a previous version though, since AFAIK it was always the case.

Best,
Andrei

Thank you for your response.
I tried using 'rep.create.camera(), but the issue of the image being rotated sideways was not resolved.
When capturing images without setting any parameters related to rotation, the images do not rotate sideways, but the images captured by the camera all appear very similar, and I cannot capture images with a diverse range of distances, as in Isaac Sim 2021.1.1.

In other words, I believe that the rotation of the camera is likely dependent on the settings of the rotation parameters.

To capture various images within the scene, similar to Isaac Sim 2021.1.1, how should I configure the parameters for ‘rep.create.camera()’ and ‘rep.modify.pose()’?

Below is the current code:"

    def add_camera_to_viewport(self):
        self.camera = rep.create.camera()
        
        self.active_vp_window = omni.kit.viewport.utility.get_active_viewport()
        self.active_vp_window.camera_path = "/Replicator/Camera_Xform/Camera"

        kit.update()
    def setup_replicator(self):
        # camera = rep.get.prims(path_pattern=str(self.camera.GetPath()))
        camera = rep.get.prims(path_pattern = "/Replicator/Camera_Xform/Camera")

        with self.rep.new_layer():
            with self.rep.trigger.on_frame():
                
                with camera:
                    rep.modify.pose(
                    position=rep.distribution.uniform((-4.39, -1.39, 0.2), (-1.30, 20, 0.3)))

here are the captured images. The camera’s captured images exhibit a similar sense of distance.

try using the created camera node in setup_replicatorself.camera:

    def setup_replicator(self):
        with self.rep.new_layer():
            with self.rep.trigger.on_frame():
                
                with self.camera:
                    rep.modify.pose(
                    position=rep.distribution.uniform((-4.39, -1.39, 0.2), (-1.30, 20, 0.3)))