Can you run a script using Kit to Edit a usd file without loading a gui like create or code

so lets say i have a script called Make_A_Pointless_Cube.py
and that script contains the code

import sys
import omni.usd
import omni.kit.commands
from pxr import Usd, UsdGeom, Sdf,UsdShade

#----------------------------------------------------------------------
def Make_Pointless_Cube(fp) -> None:
	"""  """
	stage   = omni.usd.get_context().open_stage(fp) 
	df_prim:Usd.Prim = stage.GetDefaultPrim()
	omni.kit.commands.execute('CreateMeshPrimWithDefaultXform',
					prim_type='Cube',
					prim_path=df_prim.GetPath().AppendChild("Pointless_Cube").pathString)
        omni.usd.get_context().save_stage()

if __name__ == "__main__":
        fp = sys.argv[1]
	Make_Pointless_Cube(fp)

and every time i finish a project for a client i deliver 20 to 30 usd files.
but the client has requested that they would like a pointless cube in all the finale deliveries.
but not in the working files.

now i have a script that scans a directory finds all the usd files and runs the
Make_A_Pointless_Cube.py file on them.

i have tried
kit.exe --exec "C:\code_stuff\scripts\do_pointless_stuff.py"
kit.exe --exec "C:\code_stuff\scripts\Make_A_Pointless_Cube.py" A_Usd_file.usd
but when ever i run it one of 3 things happens

[Error] [omni.kit.app.plugin] [py stderr]: ImportError: DLL load failed: The specified module could not be found.

[Error] [omni.kit.app.plugin] [py stderr]: ModuleNotFoundError: No module named ‘omni.usd’

or it just crashes.

the code is much more complex than that but i wanted to keep the concept simple
im also looking to see if this could be executed from within Maya after a usd export

Hey Mayaenite! You might be getting those errors because those modules are not loaded with Kit. You can enable them via the command you input or run the command with an Omniverse Application. If you are using Create head into the root folder with the bat file. Below you can see how to find the path where the bat file lives.
../../_images/Replicator_find_code.gif

Then you can run the command .\omni.create.bat --exec "C:\code_stuff\scripts\do_pointless_stuff.py"

I also noticed in the simple script you had that there was a couple of errors so I have below a modified version from what you provided that should work. Let me know how it goes!

import sys
import omni.usd
import omni.kit.commands
from pxr import Usd, UsdGeom, Sdf,UsdShade

#----------------------------------------------------------------------
def Make_Pointless_Cube(fp) -> None:
	"""  """
	context = omni.usd.get_context()
	context.open_stage(fp)
	stage = context.get_stage() 
	df_prim:Usd.Prim = stage.GetDefaultPrim()
	omni.kit.commands.execute('CreateMeshPrimWithDefaultXform',
		prim_type='Cube',
		prim_path=df_prim.GetPath().AppendChild("Pointless_Cube").pathString)
	omni.usd.get_context().save_stage()

if __name__ == "__main__":
	fp = "C:/Assets/test.usd"
	Make_Pointless_Cube(fp)
1 Like

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