Create UsdShade prims and Nodegraph nodes with Python

   374   2   0
User Avatar
Member
235 posts
Joined: Nov. 2013
Offline
Is there a way to create Shader prims (using the USD python API) that have the necessary information for the Edit Material Lop to create the corresponding nodegraph nodes?

My use case is this:
1. Exporting a usd asset with materials from Unreal (which due to UE's USD implementation makes the materials import as UsdPreviewMaterials).
2. Using a python lop to build a proper MaterialX material within the existing material network.
3. Being able to append an Edit Material LOP to check everything is wired up correctly.

I do not wish to programmatically append a material edit lop and load the network for what will eventually be hundreds of assets, I'm hoping to achieve this using the USD python API or whatever sorcery the Edit Material Lop uses.

Example below has a basic USD MaterialX Builder manually created inside a material library ('materiallibrary6').
Then you can see the python lop attempting to make a basic material. The Scene Graph prims appear correctly as far as I can see.
Third pic is when I load the programmatically created material into an Edit Material Lop. The contents are empty, although the material container does get created as a subnet.





from pxr import Usd, UsdShade, Sdf

# Get the current USD stage from the LOP node context
node = hou.pwd()
stage = node.editableStage()

# Define the path for the material
material_path = Sdf.Path("/materials/my_custom_material")

# Create the Material Prim
material_prim = UsdShade.Material.Define(stage, material_path)
material_primpath = stage.GetPrimAtPath(material_path)
material_primpath.ApplyAPI("MaterialXConfigAPI") #Apply correct API Schema to Material container

# Create a Shader Prim inside the material
shader_path = material_path.AppendPath("mtlx_standard_surface")
shader_prim = UsdShade.Shader.Define(stage, shader_path)
shader_prim.CreateIdAttr("ND_standard_surface_surfaceshader")
shader_primpath = stage.GetPrimAtPath(shader_path)
shader_primpath.ApplyAPI("NodeGraphNodeAPI") #Apply correct API Schema to Shader
Edited by Hamilton Meathouse - Nov. 14, 2026 19:39:57

Attachments:
manual_mtlx.jpg (276.4 KB)
mtlx_prims_py.jpg (300.7 KB)
edit_material.jpg (214.4 KB)

User Avatar
Staff
1477 posts
Joined: July 2005
Offline
You may need to connect the shader to the material's terminal output:

shader_out = shader_prim.CreateOutput("out", Sdf.ValueTypeNames.Token)
material_out = material_prim.CreateSurfaceOutput("mtlx")
material_out.ConnectToSource(shader_out)
User Avatar
Member
235 posts
Joined: Nov. 2013
Offline
rafal
You may need to connect the shader to the material's terminal output:

shader_out = shader_prim.CreateOutput("out", Sdf.ValueTypeNames.Token)
material_out = material_prim.CreateSurfaceOutput("mtlx")
material_out.ConnectToSource(shader_out)

Ah yep, works with this now, thanks Rafal!

I'm slowly getting there with this tool, its whack-a-mole with all the edge cases and interesting naming coming from UE...
  • Quick Links