How can I change the color of an existing light with Python API

Hello I have two questions

  1. “How can I change the color of an existing light in an Omniverse stage?”
  2. “How can I modify the color of an existing prim object in Omniverse?”

I wish to solve these tasks using the Python API. If possible, I’m curious about the sample code along with the libraries I need to import.
If you have experience solving this issue, please share it

For changing the light:

import omni.usd
from pxr import Gf, Usd

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/CylinderLight")
attr = prim.GetAttribute("inputs:color")
attr.Set(Gf.Vec3d(0.8, 0.5, 0.5))
# Optionally you can provide time to animate color
attr.Set(Gf.Vec3d(0.8, 0.5, 0.5), Usd.TimeCode(2))

import omni.usd
from pxr import Gf, Usd, UsdGeom
stage = omni.usd.get_context().get_stage()

for prim in stage.Traverse():
prim_name = prim.GetName()
if “Sphere” in prim_name or “light” in prim_name:
print(prim_name)
xform = UsdGeom.Xform(prim)
print(xform.GetPath())

prim = stage.GetPrimAtPath(“/World/NUC12_07/Geometry/cart_12/Sphere/SphereLight”)
attr = prim.GetAttribute(“inputs:color”)
attr.Set(Gf.Vec3d(0.8, 0.5, 0.5))

"I understood the method. However, there seems to be an error with ‘light’. Could it be slightly different?

Also, I would appreciate it if you could tell me how to change the Material.

@inyong2327 you can examine the USD snippets on creating and modifying materials attributes like color:

what was the actual issue with the other script to change light color? the indentation?

Error in ‘pxrInternal_v0_20__pxrReserved__::UsdStage::_SetValueImpl’ at line 6028 in file C:\b\w\ca6c508eae419cf8\USD\pxr\usd\usd\stage.cpp : ‘Empty typeName for </World/NUC12_07/Geometry/cart_12/Sphere/SphereLight.inputs:color>’

I tried changing the color of the Light using the code you provided, but I encountered this error. It says there’s no attribute ‘.inputs:color’. Am I using it incorrectly?

and It would be helpful if you could also provide an API to reference which attributes are present in the prim object.

Thank you.

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