Save images from two cameras

Hello, I have created a function to save images from two cameras pre-created in usd file. This function will be called after every update. There is only one default viewport. When i run the code, the viewport will change to camera_third from perspective in a few seconds. But it will not change to camera_first. So two images saved are both saved with camera_third. How to solve this problem ? PPPPlease

def save_image(self):
        camera_first_path = "/ur5/eef_link/Camera_first"
        camera_third_path = "/ur5/Camera_third"
  
        vp_api = get_active_viewport()
  
        # save image from camera_first
        vp_api.set_active_camera(camera_first_path)
        time.sleep(0.5)
        file_path = os.path.join(
            self.data_path + f'/random_{self.generate_num}' + f'/epoch_{self._action_sequence_index}',
            'Camera_first')
        capture_viewport_to_file(vp_api, file_path)
        # save image from camera_third
        vp_api.set_active_camera(camera_third_path)
        time.sleep(0.5)
        file_path = os.path.join(
            self.data_path + f'/random_{self.generate_num}' + f'/epoch_{self._action_sequence_index}',
            'Camera_third')
        capture_viewport_to_file(vp_api, file_path)

The video when i run the code

Hi!(Maybe you are Chinese? Me too!)
Have you solved this please?
I also have a robot file with two cameras installed and would like to get the images this way, the viewport also switches after the code is run, appreciate if you have any solution!

Hello,

I have solved this problem now.
I have created some viewports from cameras of different angles by function create_viewport_for_camera after having cleaned the viewport default by function destroy_all_viewports. And in every update, i just run save_image()

def save_image(self):
    """
    Save images from the cameras
    """
    self.save_image_from_viewport(vp_name="Viewport face")
    self.save_image_from_viewport(vp_name="Viewport top")
    self.save_image_from_viewport(vp_name="Viewport left")
    self.save_image_from_viewport(vp_name="Viewport right")

def save_image_from_viewport(self, vp_name):
    vp_api = get_viewport_from_window_name(vp_name)
    file_path = os.path.join(self.demo_folder_path, f"capture_{self._action_sequence_index}_" + vp_name)
    capture_viewport_to_file(vp_api, file_path)

So the solution is keeping all the viewports active.
Note: you should make every viewport have his own window to display like the image below. If not, you will find that the images from only one viewport are saved.

Thank you for your reply!
I actually used this approach later and it did work, thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.