Saving USD scene to PNG file using Python

Hi, I had a quick question. I am currently using Omniverse as a means of rendering USD scenes. I am new to Omniverse and am unsure how to export a scene as an image. I would like to do this programmatically since I will be updating the scenes many times (and by result the usd file) and would like to stick together each of these frames to make a video. Does anyone have any experience with this? I am also open to using other renderers that support USD. The main thing is that I would like to generate image files for each frame using Python.

@abhishekjoshinj i am just an OV user passing by, see if this thread points you in the right direction:

here is the viewport API - Overview — omni.kit.viewport.docs 104.0.3 documentation and a capture section in the left-hand menu as well, should you want to take a look.

I am currently creating the USD file using the pxr library. Is there a function in omni.kit that allows me to load the USD file to be my current scene?

Also, I don’t know if this is possible, but is it possible to import omni.kit using a text editor such as VS Code?

regarding file opening/closing, you can probably look into omni.kit.window.file page:
https://docs.omniverse.nvidia.com/kit/docs/kit-sdk/104.0/source/extensions/omni.kit.window.file/docs/index.html#

and regarding VSCoding, see if this is what you are looking for -

here are a few more pages of doc and code snippets for your benefit as you get your feet wet -
https://docs.omniverse.nvidia.com/dev-guide/latest/programmer_ref.html

I was able to get an Omniverse extension set up but now want to pip install from a particular directory of mines. How might I go about doing this?

This is not so easy. You can save a PNG file using CaptureExtension which gives you lots of options to control the image size and rendering quality (it is what MovieCapture uses):

import omni.kit
capture_extension = omni.kit.capture.viewport.CaptureExtension.get_instance()
capture_extension.options._output_folder = r'C:\Temp'
capture_extension.options._file_type = '.png'
capture_extension.start()

And you can load a USD file to the viewport with:

import omni.usd
omni.usd.get_context().open_stage(r'C:\Temp\sample.usd')

However if you try and put this in a loop with a list of USD files then it won’t work because the loading and rendering are asynchronous and I haven’t yet found a way to force the update of the viewport before trying to save the output.

Thanks so much for this! Where can I find the options/API for CaptureExtension?

@william.sellers that’s good to know, thanks for sharing, William! 👍

@abhishekjoshinj there’s also viewport utility. feel free to take a look:

python API for it can be found here - omni.kit.viewport.utility — Omniverse Kit 1.0.16 documentation

You can find the python source in the ov folder and you can get to it easily through the Extensions Window (Kit Image and Video Capture). If you look at capture_options.py you can see all the settings. What’s nice about it compared to the other options is that all the settings in the viewport are saved before the capture is run and then restored afterwards which means you can capture at one resolution and display at another (for example). The down side is that there is no video showing how to use it, and I guess hte interface might change (there is an older version that is marked as deprecated).

1 Like

In case anyone finds it useful, I’ve just written an extension that does exactly this. You give it a folder of USD files and it will produce PNG files for you. It is still a work in progress because it could be much smarter about identifying when the viewport is properly updated but it does work.

https://github.com/wol101/GaitSym2019/tree/development/omniverse/kit-exts-usd_batch_render

2 Likes

This is very helpful, thank you so much! One quick question I noticed was that when I render using path tracing, it doesn’t sample that much and instead produces an image after the first couple samples. Do you know how this can be fixed such that it hits 512/512 (or something like that)? Is this a timing thing?

No, it’s an option. You can set the number of samples in the latest version. It also handles timing better by waiting for the PNG file to be written.