How can I create materials programmatically code with hydra?

Hi,

How can I create materials programmatically with Hydra 2?

I have been looking into HdMaterialNetwork2, and HdMaterialNode2, etc, but I am not sure whether I am on the correct path.

Are there any resources, you could suggest I can take a look?

Hi,

If you want an example of a very simple material like a UsdPreviewSurface with a diffuse color only you can have a look at Maya Hydra :

A more complex material creation can be seen in the translation of Maya OpenPBR materials into UsdPreviewSurface :

Regards,
David

Thank you very much for your answer. It helped me identify some issues in material network to hydra data sources conversion.

But unfortunately, I still couldn’t make it work. I am still getting a gray cube.
Cube without material

I already enabled a number of TF_DEBUG env. variables in the hopes that it will give an indication what I might be doing wrong, but no luck yet.

I am planning to go down to the rabbit hole of debugging the generated shaders and walk my way up to see why the material I think I set are skipped/ignored.

Is there a better place to start searching?

Many thanks for your time.

Hi,
I am not sure if you tried to use a hardcoded material with just a diffuse color to check that this is working ?
Also one thing, if you are using a HdTaskController at some point in your code, be sure to have enabled the scene materials :

HdxRenderTaskParams params;
params.enableLighting = true;
params.enableSceneMaterials = true;
_taskController->SetRenderParams(params);

I can suggest that you integrate the Hydra scene browser : Universal Scene Description: Hydra Scene Browser
Which will be very helfpul to debug the hydra scene, we integrated it in Maya Hydra and it has helped solving multiple issues.
Regards,
David

I finally figured it out. Thank you very much for your help.

It turns out, Hydra already has the appropriate utils to convert a HdMaterialNetworkMap to a (correct) container data source.

Basically

auto convertMaterialNetworkToDataSource(const pxr::HdMaterialNetworkMap& network_map) -> pxr::HdContainerDataSourceHandle
{
   auto material_data_source = pxr::HdUtils::ConvertHdMaterialNetworkToHdMaterialSchema(network_map);
   auto data_source = pxr::HdRetainedContainerDataSource::New(pxr::HdPrimTypeTokens->material, material_data_source);
   return data_source;
}

Full source code can be found here.
GitHub - birsoyo/HydraCube: Draw a cube with Hydra

1 Like