Skip to content

blender_demo_transition_machine

CameraMover

Source code in examples/blender_demo_transition_machine.py
class CameraMover:
    def __init__(self, cam, delta=0.25):
        self.cam = cam
        self.delta = delta

        self._appwindow = omni.appwindow.get_default_app_window()
        self._input = carb.input.acquire_input_interface()
        self._keyboard = self._appwindow.get_keyboard()
        self._sub_keyboard = self._input.subscribe_to_keyboard_events(self._keyboard, self._sub_keyboard_event)

    def print_cam_pose(self):
        print(f"cam pose: {self.cam.get_position_orientation()}")

    @property
    def input_to_function(self):
        return {
            carb.input.KeyboardInput.SPACE: lambda: take_photo(use_rt=True),
            carb.input.KeyboardInput.KEY_1: lambda: take_photo(use_rt=True),
            carb.input.KeyboardInput.KEY_2: lambda: take_photo(use_rt=False),
            carb.input.KeyboardInput.P: lambda: self.print_cam_pose(),
        }

    @property
    def input_to_command(self):
        return {
            carb.input.KeyboardInput.D: np.array([self.delta, 0, 0]),
            carb.input.KeyboardInput.A: np.array([-self.delta, 0, 0]),
            carb.input.KeyboardInput.W: np.array([0, 0, -self.delta]),
            carb.input.KeyboardInput.S: np.array([0, 0, self.delta]),
            carb.input.KeyboardInput.T: np.array([0, self.delta, 0]),
            carb.input.KeyboardInput.G: np.array([0, -self.delta, 0]),
        }

    def _sub_keyboard_event(self, event, *args, **kwargs):
        """Handle keyboard events
        Args:
            event (int): keyboard event type
        """
        if event.type == carb.input.KeyboardEventType.KEY_PRESS \
                or event.type == carb.input.KeyboardEventType.KEY_REPEAT:

            if event.type == carb.input.KeyboardEventType.KEY_PRESS and event.input in self.input_to_function:
                self.input_to_function[event.input]()

            else:
                command = self.input_to_command.get(event.input, None)

                if command is not None:
                    # Convert to world frame to move the camera
                    transform = T.quat2mat(self.cam.get_orientation())
                    delta_pos_global = transform @ command
                    self.cam.set_position(self.cam.get_position() + delta_pos_global)

        return True

euler2quat(euler, seq='xyz')

Converts euler angles into quaternion form

Parameters:

Name Type Description Default
euler np.array

(r,p,y) angles

required

Returns:

Type Description

np.array: (x,y,z,w) float quaternion angles

Raises:

Type Description
AssertionError

[Invalid input shape]

Source code in examples/blender_demo_transition_machine.py
def euler2quat(euler, seq="xyz"):
    """
    Converts euler angles into quaternion form

    Args:
        euler (np.array): (r,p,y) angles

    Returns:
        np.array: (x,y,z,w) float quaternion angles

    Raises:
        AssertionError: [Invalid input shape]
    """
    return R.from_euler(seq, euler).as_quat()