XR Core events, action maps and eventgenerators for modifying controls (original) (raw)

Update: I was able to get my own Grab mechanic up and runnig, but i encountered an issue where the commit_link_transform does not apply the transform to the Cube object that i am testing with. Here is my code so far:

def _init_input_system(self):
        stage = omni.usd.get_context().get_stage()
        action_map = self._xr_core.get_action_map()
        name = action_map.get_name() if action_map else "Unknown"
        tools = action_map.get_tool_list() if action_map else []
        print(f"Input system initialized with: AM Name:{name}, Tools: {[tool for tool in tools]}")
        self.usd = self._xr_core.create_xr_usd_layer("/_xr/gui/controllers")
        self.base_path_right: str = self.usd.ensure_device_prim_path("/user/hand/right")
        self.beam_prim_path_right: str = self.return_beam_path(self.base_path_right)
        self.beam_mat = "/_xr/gui/controllers/_coord/assets/_generic_materials__selection_emissive_material_usd/_coord/_ref/Looks/selection_emissive_material"
        self.link_prim_path_right: str = self.return_link_path(self.base_path_right)
        if stage.GetPrimAtPath(self.beam_prim_path_right):
            self.usd.remove(self.beam_prim_path_right)

        self.usd.add_beam(self.beam_prim_path_right, group= "my_grab_interaction", material_reference=self.beam_mat, max_length=20, tube_radius=10, visible=False)

        self.message_bus = self._xr_core.get_message_bus()
        #msg_type_grab_press = carb.events.type_from_string("xr_right_grab.press")
        #msg_type_grab_release = carb.events.type_from_string("xr_right_grab.release")
        #self.sub_grab_press=self.message_bus.create_subscription_to_pop_by_type(msg_type_grab_press, self.on_xr_right_grab_press)
        #self.sub_grab_release=self.message_bus.create_subscription_to_pop_by_type(msg_type_grab_release, self.on_xr_right_grab_release)
        print("Trying to bind right controls, left is untouched")
        self.right_vive = self._xr_core.get_input_device("/user/hand/right")
        self.mybinding = self.right_vive.bind_event_generator("squeeze","my_grab", ("press", "release"))
        #if self.mybinding is not None:
        #    input_device_name: str = str(self.mybinding.get_input_device_name())
         #   input_name: str = str(self.mybinding.get_input_name())
           # print(f"Binding right controls: Device Name: {input_device_name}, Input Name: {input_name}")
        
        msg_type_grab_press = carb.events.type_from_string("my_grab.press")
        msg_type_grab_release = carb.events.type_from_string("my_grab.release")
        self.sub_my_grab_press=self.message_bus.create_subscription_to_pop_by_type(msg_type_grab_press, self.on_my_grab_press)
        self.sub_my_grab_release=self.message_bus.create_subscription_to_pop_by_type(msg_type_grab_release, self.on_my_grab_release)
        self.grab_state= {"grabbed_usd_path": None, "has_link": False}

    def return_beam_path(self, base_path : str):
        return f"{base_path}/my_beam"
    def return_link_path(self, base_path : str):
        return f"{base_path}/my_link"

    def on_my_grab_press(self, msg):
            print("my right grab pressed")
            self.usd.show(self.beam_prim_path_right)
            target_info = self.usd.get_target_info(self.beam_prim_path_right)
            self.grab_state["grabbed_usd_path"] = target_info.get_target_enclosing_model_usd_path()
            print(f"Target Path: {self.grab_state['grabbed_usd_path']}")
            if self.grab_state["grabbed_usd_path"] is not None:
                self.grab_state["link"] = self.usd.add_link(self.link_prim_path_right, group="my_grab_link", link_path=self.grab_state["grabbed_usd_path"], transform_type=XRTransformType.stage)
                self.grab_state["has_link"] = True

    def on_my_grab_release(self, msg):
            print("my right grab released")
            self.usd.hide(self.beam_prim_path_right)
            if self.grab_state["has_link"]:
                #link_world = self.usd.get_transform(self.link_prim_path_right,
                #                                    transform_type=XRTransformType.world,
                 #                                   use_usd=False
                  #                                  )
                
                #layer = self._xr_core.suggest_edit_layer_for_prim(self.grab_state["grabbed_usd_path"])

                #self._xr_core.set_world_transform_matrix(self.grab_state["grabbed_usd_path"], link_world, layer_identifier=layer)
                self.usd.commit_link_transform(self.grab_state["link"])
                self.usd.remove(self.link_prim_path_right)
                self.grab_state["has_link"] = False
                self.grab_state["grabbed_usd_path"] = None