# How can I create materials programmatically code with hydra?

**URL:** <https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405>\
**Category:** Hydra\
**Tags:** cpp, beginner\
**Created:** [April 28, 2025, 3:57pm UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405 "2025-04-28T15:57:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orhun](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@orhun](https://forum.aousd.org/u/orhun)\
**Post date:** [April 28, 2025, 3:57pm UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405/1 "2025-04-28T15:57:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dlanier](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/dlanier/32/166_2.png) [@dlanier](https://forum.aousd.org/u/dlanier)\
**Post date:** [April 29, 2025, 7:39am UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405/2 "2025-04-29T07:39:00Z")

</div>

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 :

> <https://github.com/Autodesk/maya-hydra/blob/927a5f4573901eaca1775749c1ea2021cfd3bf23/lib/mayaHydra/hydraExtensions/sceneIndex/mayaHydraSceneIndex.cpp#L749>

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

> <https://github.com/Autodesk/maya-hydra/blob/927a5f4573901eaca1775749c1ea2021cfd3bf23/lib/mayaHydra/hydraExtensions/adapters/materialNetworkConverter.cpp#L884>

Regards,  
David

---

<div class="post-metadata">

**Author:** ![orhun](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@orhun](https://forum.aousd.org/u/orhun)\
**Post date:** [May 2, 2025, 5:16pm UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405/3 "2025-05-02T17:16:47Z")

</div>

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](https://github.com/birsoyo/HydraCube?tab=readme-ov-file#cube)

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.

---

<div class="post-metadata">

**Author:** ![dlanier](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.aousd.org/dlanier/32/166_2.png) [@dlanier](https://forum.aousd.org/u/dlanier)\
**Post date:** [May 5, 2025, 9:31am UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405/4 "2025-05-05T09:31:07Z")

</div>

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 :

```auto
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](https://openusd.org/dev/api/_page __hydra__ scene__browser.html)  
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

---

<div class="post-metadata">

**Author:** ![orhun](https://avatars.discourse-cdn.com/v4/letter/o/8baadc/32.png) [@orhun](https://forum.aousd.org/u/orhun)\
**Post date:** [May 6, 2025, 2:07am UTC](https://forum.aousd.org/t/how-can-i-create-materials-programmatically-code-with-hydra/2405/5 "2025-05-06T02:07:49Z")

</div>

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

```cpp
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](https://github.com/birsoyo/HydraCube)
