MaterialBindingAPI Bind Mesh to Material prim

Hello.

When referencing a ‘Mesh’ and a ‘Material’ prim onto a Stage,
how do I MaterialBindingAPI Bind the ‘Mesh’ to the ‘Material’ ?
Below is a simple example of what I currently have but the last line is wrong.
Do I first need to Get or Apply a Material API to the material_prim ?
Thanks for any suggestions.

stage = Usd.Stage.CreateNew('foo.usda')

geo_prim = stage.OverridePrim('/geo')
geo_prim.GetReferences().AddReference('geo.usda')

material_prim = stage.OverridePrim('/material')
material_prim.GetReferences().AddReference('material.usda')

geo_prim.ApplyAPI(UsdShade.MaterialBindingAPI)
UsdShade.MaterialBindingAPI(geo_prim).Bind(material_prim)

Hey there!

Good questions. The code snippet you provided is very close to working, you just need to pass Bind a UsdShade.Material object rather than a Usd.Prim.

UsdShade.MaterialBindingAPI(geo_prim).Bind(UsdShade.Material(material_prim))

I would also recommend getting and applying the MaterialBindingAPI using a single call like this:

mat_binding_api = UsdShade.MaterialBindingAPI.Apply(geo_prim)
mat_binding_api.Bind(UsdShade.Material(material_prim))

Hope this helps!

2 Likes

@jackohoeg
Thanks for your prompt response explaining I needed the UsdShade.Material object
and also kindly offering the cleaner MaterialBindingAPI single call.
Much appreciated.

1 Like