Download Interactive_scene/kitchen/interactive_objects/script/light_control.py from dtttttiiiii/ArtVIP: direct link, hf CLI and curl.
- Browser
- Download file 5.53 kB
-
https://huggingface.co/datasets/dtttttiiiii/ArtVIP/resolve/main/Interactive_scene/kitchen/interactive_objects/script/light_control.py
- Command line
-
hf download hf://datasets/dtttttiiiii/ArtVIP/Interactive_scene/kitchen/interactive_objects/script/light_control.py
-
curl -L -o light_control.py https://huggingface.co/datasets/dtttttiiiii/ArtVIP/resolve/main/Interactive_scene/kitchen/interactive_objects/script/light_control.py
5.53 kB
| import carb | |
| from omni.kit.scripting import BehaviorScript | |
| from omni.isaac.dynamic_control import _dynamic_control | |
| from enum import Enum | |
| from pxr import Gf | |
| from threading import Thread | |
| import marshal,json | |
| import omni.kit.commands | |
| from omni.isaac.core.prims import XFormPrim | |
| import numpy as np | |
| from pxr import Gf, Sdf, Usd, UsdGeom | |
| from omni.isaac.core import SimulationContext, World | |
| import math | |
| from omni.replicator.core import utils | |
| def find_prim_path_by_name(prim_name: str, root_path: str = "/") -> str: | |
| """Recursively find the full path by Prim name""" | |
| stage = omni.usd.get_context().get_stage() | |
| prim = stage.GetPrimAtPath(root_path) | |
| def _find_prim(prim): | |
| if prim.GetName() == prim_name: | |
| return prim.GetPath().pathString | |
| for child in prim.GetChildren(): | |
| result = _find_prim(child) | |
| if result: | |
| return result | |
| return None | |
| found_path = _find_prim(prim) | |
| if not found_path: | |
| raise RuntimeError(f"Prim '{prim_name}' not found under {root_path}") | |
| return found_path | |
| # 1. prim init | |
| joint_prim = XFormPrim(find_prim_path_by_name("switch_E_button_09")) | |
| print("joint_prim",joint_prim.prim_path) | |
| # 2. articulation Root prim | |
| root_articulation_prim = find_prim_path_by_name("switch_E_body_28") | |
| print("root_articulation_prim:",root_articulation_prim) | |
| # 3. joint name | |
| joint_names = ["RevoluteJoint_switch"] | |
| # 5. light prim | |
| light_scopes=[ | |
| XFormPrim(find_prim_path_by_name("kitchen_all_light")) | |
| ] | |
| print("light_scopes", light_scopes) | |
| class RoomLightControl(BehaviorScript): | |
| def on_init(self): | |
| self.phsx_freq = 120 | |
| self.joint_handles = [] | |
| self.dc = _dynamic_control.acquire_dynamic_control_interface() | |
| light_scopes[0].set_visibility(False) | |
| # init_pose | |
| self.local_pose_button, self.local_ort_button_down = joint_prim.get_local_pose() | |
| # self.local_pose_door, self.local_ort_drawer_up = door_prim.get_local_pose() | |
| # Find Root | |
| # self.art = self.dc.get_articulation(root_articulation_prim) | |
| # if self.art == _dynamic_control.INVALID_HANDLE: | |
| # print('the prim is not an articulation') | |
| def on_destroy(self): | |
| pass | |
| def on_play(self): | |
| # | |
| self.simulation_context = SimulationContext( | |
| stage_units_in_meters=1.0, | |
| physics_dt=1.0/120, | |
| rendering_dt=1.0/120 | |
| ) | |
| # 配置物理引擎参数 | |
| # print(self.simulation_context.get_physics_context().get_gravity()) | |
| # self.simulation_context.get_physics_context().set_broadphase_type("MBP") | |
| # self.simulation_context.get_physics_context().set_solver_type("PGS") | |
| # self.simulation_context.get_physics_context().enable_gpu_dynamics(False) | |
| def on_pause(self): | |
| pass | |
| def on_stop(self): | |
| self.init_start=True | |
| for light in light_scopes: | |
| light.set_visibility(False) | |
| # light_scopes[0].set_visibility(False) | |
| # light_scopes[1].set_visibility(False) | |
| # back to initial pose | |
| joint_prim.set_local_pose(translation=self.local_pose_button) | |
| pass | |
| def on_update(self, current_time: float, delta_time: float): | |
| if delta_time <= 0: | |
| return | |
| self.active_art() | |
| self.apply_behavior() | |
| def active_art(self): | |
| # Find Root | |
| self.art = self.dc.get_articulation(root_articulation_prim) | |
| if self.art == _dynamic_control.INVALID_HANDLE: | |
| print('the prim is not an articulation') | |
| self.dc.wake_up_articulation(self.art) | |
| # Find joint | |
| self.joint_handles = [] | |
| for joint_name in joint_names: | |
| self.joint_handles.append( self.dc.find_articulation_dof(self.art, joint_name)) | |
| def apply_behavior(self): | |
| self.light_control() | |
| def light_control(self): | |
| try: | |
| # 获取当前开关状态 | |
| self.Joint1_state = self.dc.get_dof_state(self.joint_handles[0], _dynamic_control.STATE_ALL).pos | |
| current_visibility = light_scopes[0].get_visibility() | |
| # 定义开关阈值和状态 | |
| SWITCH_ON_THRESHOLD = math.radians(11) | |
| SWITCH_ON_POSITION = math.radians(13) | |
| SWITCH_OFF_POSITION = math.radians(0) | |
| # 判断开关状态并控制灯光 | |
| if self.Joint1_state < SWITCH_ON_THRESHOLD: | |
| # 开关打开状态 | |
| if not current_visibility: | |
| # 如果灯是关闭的,则打开所有灯 | |
| for light in light_scopes: | |
| light.set_visibility(True) | |
| # 设置开关到打开位置 | |
| self.dc.set_dof_position_target(self.joint_handles[0], SWITCH_OFF_POSITION) | |
| else: | |
| # 开关关闭状态 | |
| if current_visibility: | |
| # 如果灯是打开的,则关闭所有灯 | |
| for light in light_scopes: | |
| light.set_visibility(False) | |
| # 设置开关到关闭位置 | |
| self.dc.set_dof_position_target(self.joint_handles[0], SWITCH_ON_POSITION) | |
| except Exception as e: | |
| carb.log_error(f"Error in light control: {str(e)}") | |
| # 发生错误时确保灯光关闭 | |
| for light in light_scopes: | |
| light.set_visibility(False) | |