Prim Create 및 색상 및 Light 추가 예제 - SmartX-Team/Omniverse GitHub Wiki
해당 예제는 MobileX Station들 Model 들 위에 현재 전원상태를 알려주는 원형 구체들 생성 및 색상이랑 라이트 설정하는 예제이다.
NUC11 시리즈는 1부터 10까지 있어서 for 문으로 돌림
from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf, PhysxSchema, UsdLux
stage = get_context().get_stage()
for i in range(1, 11):
prim_path = f"/World/NUC11_{i:02}"
new_sphere_path = str(prim_path) + '/Geometry/cart_12/Sphere'
new_spherelight_path = prim_path + '/Geometry/cart_12/Sphere/SphereLight'
prim = stage.GetPrimAtPath(prim_path)
if not stage.GetPrimAtPath(new_sphere_path):
spherePrim = UsdGeom.Sphere.Define(stage, new_sphere_path)
#attr = spherePrim.GetAttribute("inputs:color")
#attr.Set(Gf.Vec3d(0.8, 0.5, 0.5))
radius_attr = spherePrim.GetRadiusAttr()
radius_attr.Set(50.0) # 여기에서 원하는 반지름 값으로 설정
#생성된 구체를 해당 MobileX Station 위로 지정
current_pos = prim.GetAttribute('xformOp:translate').Get()
new_pos = Gf.Vec3d(current_pos[0], current_pos[1] + 100, current_pos[2])
UsdGeom.XformCommonAPI(spherePrim).SetTranslate(new_pos)
sphereLight = UsdLux.SphereLight.Define(stage, new_spherelight_path)
sphereLight.GetRadiusAttr().Set(50.0)
sphereLight.GetIntensityAttr().Set(30000.0)
print(f"Created new Sphere at: {new_sphere_path}")
else:
print(f"Sphere already exists at: {new_sphere_path}")
결과
MobileX Station들 위에 빛이 나는 원형 구체가 생성됨을 확인할 수 있다.
Station 들 색상 변경할때 예시
from pxr import Usd, UsdGeom, UsdShade, Sdf
from omni.usd import get_context
stage = get_context().get_stage()
# Define the path to the new material
new_material_path = "/World/Looks/station_white"
new_material_prim = stage.GetPrimAtPath(new_material_path)
if not new_material_prim:
print(f"Material does not exist at: {new_material_path}")
else:
for i in range(1, 21):
prim_path = f"/World/NUC12_{i:02}/Geometry/cart_12"
prim = stage.GetPrimAtPath(prim_path)
if prim:
material_binding_api = UsdShade.MaterialBindingAPI(prim)
material_binding_api.Bind(UsdShade.Material(stage.GetPrimAtPath(new_material_path)))
print(f"Changed material of {prim_path} to {new_material_path}")
else:
print(f"Prim does not exist at: {prim_path}")