How to turn on/off the light or object in Omniverse Creat or sim

Hello, I am only a beginner in Omniverse.
I am trying to turn on and off the light which I made for 3d object when I press the particular key on Keyboard for example space bar.
I mean that I’d like to disable or setactive(false) the 3d object in Omniverse.
I don’t know which Apps I would code by using python for the system between Omniverse creat and Omniverse sim.
Or is there any other way to turn the object on and off with coding?
please help me

Hi @gudwnzm

The following code shows how to subscribe to a keyboard event and use the prim’s attributes to change its behaviour. You can extrapolate this example for any attribute…

import carb
import omni
from pxr import Usd, UsdGeom
import omni.usd

# get prim and its attribute ("intensity")
stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/SphereLight")
attribute = prim.GetAttribute("intensity")

def keyboard_event(event, *args, **kwargs):
    if event.type == carb.input.KeyboardEventType.KEY_PRESS:
        if event.input == carb.input.KeyboardInput.SPACE:
            attribute.Set(0 if attribute.Get() else 3000000)  # change attribute
 
# subscribe to keyboard event
appwindow = omni.appwindow.get_default_app_window()
input = carb.input.acquire_input_interface()
input.subscribe_to_keyboard_events(appwindow.get_keyboard(), keyboard_event)

Here is a short video showing how to run it from Script Editor, however you can also run it from a Standalone Application


a

Hello @toni.sm
I’d like to say thank you for your help.
That’s really what I want to do in omniverse.
Many thanks for your support.
The coding in omniverse is possible in only Omniverse sim yet??
Is there any other way to code in omniverse creat?

I have one more question…
If I write python code for omniverse sim in VScode, how would I apply it to objects? component? in omniverse sim
I mean that how could I execute or connect the python code in omniverse sim??

Hi @gudwnzm

You can program for any omniverse App as long as you use generic code, for example from the Omniverse Kit API as used in the code above. Now, if you want to use robotic specific extensions for simulation, you are restricted to Isaac Sim.

Regarding how to program, there are different ways such as Omniverse extensions, apps, or microservices. I recommend you take a look at Omniverse Code

Hello @toni.sm
I really appreciate your kindness.
I will take a look at Omniverse Code!
Lastly… I have the last question…
The last question is that I have many SphereLights.
So I created the Xform as a parent which has many SphereLights like a bundle.
When I press the space bar on keyboard, the visibility of Xform is on and off…
I want to turn it on and off with only one xform which has many SphereLights.
I hope you understand what I’m saying.
please help me one last time…

For example…

import carb
import omni
from pxr import Usd, UsdGeom
import omni.usd

# get prim and its attribute ("intensity")
stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/World/lamp/Looks/lamp_bundle")
attribute = prim.GetAttribute("visibility")

def keyboard_event(event, *args, **kwargs):
    if event.type == carb.input.KeyboardEventType.KEY_PRESS:
        if event.input == carb.input.KeyboardInput.SPACE:
            attribute.Set(inherited if attribute.Get() else invisible)  # change attribute
 
# subscribe to keyboard event
appwindow = omni.appwindow.get_default_app_window()
input = carb.input.acquire_input_interface()
input.subscribe_to_keyboard_events(appwindow.get_keyboard(), keyboard_event)

Hi @gudwnzm

The visibility attribute type is str

Then if you want to toggle it you can try something like this

attribute.Set("inherited" if attribute.Get() == "invisible" else "invisible")

Hi @toni.sm
Thank you for your response and your kind words.
It really works well… Thank you
where could I refer to those API like attribute.Set, Get??
I have tried to find those API which means properties of Get or Set In the link below but I could not make it…
Working with USD Python Libraries | NVIDIA Developer, Universal Scene Description: UsdGeomXform Class Reference, pxr package — Omniverse Kit 102.1.2+release.46667.2027b54e.teamcity documentation

I’d like to use various keyboard_events not only for space bar like enter, WASD and number 1 etc…
So I need to refer to API of keyboard_events as well…

Thank you so much for your help.

Omniverse Kit Programming: Kit Programming Manual — Omniverse Kit 102.1.2+release.46667.2027b54e.teamcity documentation

hi @toni.sm
I really appreciate all your kindness.
It works very well now.
I will take a look at Omniverse Code hardly following your advice.

Glad to help 😁.

By the way, very nice job with the lights

Hi @toni.sm
I’m sorry, but I have one last question.
How would I run my python code which wrote from VScode in Omniverse isaac-sim?
I usually code and run it from Script Editor in Omniverse isaac-sim
However, If I run it once from Script Editor and I want to edit some code, I should starts Omniverse isaac-sim again.
Nothing has changed…
So I’d like to edit code my heart’s content.
How could I apply my code which coded from VScode to Omniverse isaac-sim?
Even though I have take a look at Omniverse Code, I could not find about that…

Hi, @gudwnzm

You can write your code in a user extension (and optionally enable its autoload) or program a standalone application…

The files are good starting point:

extension

  • PATH_TO_ISAAC_SIM/kit/exts/omni.example.hello

standalone app

  • PATH_TO_ISAAC_SIM/standalone_examples/api/omni.isaac.kit/hello_world.py
  • PATH_TO_ISAAC_SIM/standalone_examples/api/omni.isaac.kit/load_stage.py

Hi, @toni.sm
It’s a different story from the topic of this article, but I’m asking because there’s no one to ask.
Is there a way to see the model created by Omniverse Create in oculus quest 2 through Omniverse CloudXR?
If you know how to do it, please tell me. please
Always Thank you!

Hi @gudwnzm

As a user, I only know this reference to CloudXR in Omniverse (Augmented Reality — Omniverse Create documentation) for “modern Android or iOS mobile device capable of running AR applications”

There is the external user extension (omni.add_on.openxr) that supports XR in any Omniverse application (Isaac Sim, Create, etc. running on Linux, at the moment). I have only tested it with a HTC Vive Pro 2 (it is the only device to which I have access) although it is designed to work with any device that supports the OpenXR standard.

Hi @toni.sm
Thank you for your response to CloudXR.
I’m so sorry to ask you again, but would I ask for a good example just like last time?
I’m still studying the USD library, but I’m a developer of Unity, so there’s still a lot of confusion. I want to control the door in the video I showed you above with a keyboard.
I want to click on keyboard P to make a toggle key that goes up and down the door. I really see and realize a lot through you.
I am collecting and organizing the materials you help and making them into learning materials. I also want to share this with people later and help people like you.
Please help me again.

Hi @gudwnzm

You can do it from different approaches, for example: editing the attributes of a prim and modifying its transformations or involving physics, through rigid bodies, joints (prismatic joint) and articulations (dynamic_control extension). While the former is simpler, the latter is more natural…

Here is a snippet that shows both approaches in a scenario with two doors: left (prim attr) and right (dynamic_control). Look inside the .usd to inspect the rigid bodies, the articulation and the joint

door.usd (4.5 KB)

import pxr
import carb
import omni
import omni.usd
from omni.isaac.dynamic_control import _dynamic_control

opened = False

# left door (prim attributes)
stage = omni.usd.get_context().get_stage()
left_door = stage.GetPrimAtPath("/World/left_door/door")

# right door (articulation)
dc = _dynamic_control.acquire_dynamic_control_interface()
ar = dc.get_articulation("/World/right_door")
dof = dc.get_articulation_dof(ar, 0)

def keyboard_event(event, *args, **kwargs):
    global opened

    if event.type == carb.input.KeyboardEventType.KEY_PRESS:
        if event.input == carb.input.KeyboardInput.P:
            dc.wake_up_articulation(ar)
            # close
            if opened:
                left_door.GetAttribute("xformOp:translate").Set(pxr.Gf.Vec3d(0,0,0))
                dc.set_dof_position_target(dof, 0)
            # open
            else:
                dc.set_dof_position_target(dof, 195)
                left_door.GetAttribute("xformOp:translate").Set(pxr.Gf.Vec3d(0,0,195))
            opened = not opened
 
# subscribe to keyboard event
appwindow = omni.appwindow.get_default_app_window()
input = carb.input.acquire_input_interface()
input.subscribe_to_keyboard_events(appwindow.get_keyboard(), keyboard_event)

Hello! @toni.sm
I’d really like to say thank you for replying me!
I have tried the example which you wrote it.
But there is a small problem…

Above picture is made by Omniverse Create
It says that NameError: name’_dynamic_control’ is not defined, ModuleNotFoundError: No module named ‘omni.isaac’
So I think dynamic_control is available on Isaac sim
Is there any way to control physics API like dynamic_control in Omniverse Create?

And I’d tried to run in Isaac sim Sim
But there is other problem…

Thank you so much for always helping me.

Hello @toni.sm

I’m sorry, but I’m sorry that you’re the only one to ask, but I have one more question. I used the extension manager to turn on the light that I coded last time at extension.py in VSCode.
It says that [Error][carb.input.python]NameError:name’attribute’is not defined in VSCode. There’s no problem when you look at the code.
If I use the same code in Scripteditor in Omniverse window, it runs, but VScode says the attribute is not defined.
Did I miss anything?

Hi @gudwnzm

Please, open a new topic if the problem is not directly related to the original topic of this post, in order to better organize the content :)

Yes, dynamic_control is an Isaac Sim extension. What about using prim methods to change the drive:linear:physics:targetPosition (Target Position) attribute’s value?


Make sure the simulation is running before calling the code


You are trying to get an attribute of a prim (that is created as a Python class variable)). This prim will be initialized when the extension is loaded by the extension manager (even if your USD scene is not currently loaded) resulting in an invalid prim (because the path does not exist). Instantiate your prim and its attribute using another logic, for example inside a button callback…

Hi! @toni.sm
I didn’t know I had to open a new topic. I’ll do that from next time. Thank you so much for always helping me.
Lastly in this topic… Would I ask what inside a button callback is?
image

Do you mean this button callback?

As you said, instead of the button callback function, I put the code on and off the light.
NameError: name ‘attribute’ is not defined error does not change.

image

Otherwise, is there a way to use the code you created, that is, create as a Python class variable, in the extension manager?