# How to dynamically add materials to rhino doc from rmtl file?

**URL:** <https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282>\
**Category:** Rhino Developer\
**Tags:** windows, rhino7\
**Created:** [March 11, 2022, 8:11am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282 "2022-03-11T08:11:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 11, 2022, 8:11am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/1 "2022-03-11T08:11:38Z")

</div>

Is there an api to do this?

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/759176_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [March 14, 2022, 12:16am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/2 "2022-03-14T00:16:10Z")

</div>

Hi @duzhengjie1,

yes, look into [Rhino.Render.RenderContent](https://developer.rhino3d.com/wip/api/RhinoCommon/html/T_Rhino_Render_RenderContent.htm) to see what is possible. To load a material from disk below seems to work in Rhino 7:

```python
import Rhino
import scriptcontext

file = r"D:\Temp\TestMaterial.rmtl"
m = Rhino.Render.RenderContent.LoadFromFile(file)
if m:
    # add to document
    Rhino.Render.RenderContent.AddPersistentRenderContent(scriptcontext.doc, m)

```

Note: if a material exists with the same name, you’ll might want to precheck and decide what to do…  
\_  
c.

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 14, 2022, 5:19am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/3 "2022-03-14T05:19:22Z")

</div>

@clement Thanks very much!

---

<div class="post-metadata">

**Author:** ![nathanletwory](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathanletwory/32/482880_2.png) [@nathanletwory](https://discourse.mcneel.com/u/nathanletwory)\
**Post date:** [March 14, 2022, 7:11am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/4 "2022-03-14T07:11:18Z")

</div>

FYI if you want to create materials fully dynamic you can read through some of my samples that I wrote last year:

[https://jesterking.github.io/rhipy/](https://jesterking.github.io/rhipy/)

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 15, 2022, 12:45am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/5 "2022-03-15T00:45:39Z")

</div>

@nathanletwory Ok，thanks

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 18, 2022, 2:27am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/6 "2022-03-18T02:27:07Z")

</div>

@nathanletwory May I have a question again please?What’t the difference between materials and rendermaterials ?And can the two object transform each other?

---

<div class="post-metadata">

**Author:** ![nathanletwory](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathanletwory/32/482880_2.png) [@nathanletwory](https://discourse.mcneel.com/u/nathanletwory)\
**Post date:** [March 18, 2022, 6:04am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/7 "2022-03-18T06:04:19Z")

</div>

> [@duzhengjie1](#):
>
> What’t the difference between materials and rendermaterials

No difference, RenderMaterial is just the underlying class name of the code for materials.

> [@duzhengjie1](#):
>
> And can the two object transform each other?

What exactly are you looking for?

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 18, 2022, 6:18am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/8 "2022-03-18T06:18:54Z")

</div>

ObjectAttributes need a MaterialIndex,it gets from Materials,I have added rendermaterial,but I get nothing when find it from materials

---

<div class="post-metadata">

**Author:** ![nathanletwory](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathanletwory/32/482880_2.png) [@nathanletwory](https://discourse.mcneel.com/u/nathanletwory)\
**Post date:** [March 18, 2022, 6:38am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/9 "2022-03-18T06:38:47Z")

</div>

Ah, you should no longer use the Material class, and the Materials table, but instead use the RenderMaterials table of a document.

Setting a RenderMaterial to an object is easy by setting it to the object `RenderMaterial` property.

See [https://jesterking.github.io/rhipy/create\_specific\_rendermaterial.html](https://jesterking.github.io/rhipy/create_specific_rendermaterial.html) for a simple example.

I am in the process of writing a simple script to show you how to load RMTL files into a Rhino document.

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 18, 2022, 7:08am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/10 "2022-03-18T07:08:22Z")

</div>

But I need to add some geometries to the InstanceDefinitions,I want its geometries can render from the materials also.The InstanceDefitions only supply the api from geometries and the ObjectAttributes.What can i do for this?

---

<div class="post-metadata">

**Author:** ![nathanletwory](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathanletwory/32/482880_2.png) [@nathanletwory](https://discourse.mcneel.com/u/nathanletwory)\
**Post date:** [March 18, 2022, 7:40am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/11 "2022-03-18T07:40:24Z")

</div>

Set the `RenderMaterial` render content to the [ObjectAttributes.RenderMaterial Property](https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_DocObjects_ObjectAttributes_RenderMaterial.htm) for the geometry you want to add or modify in the instance definition.

Here a script to load all RMTL files from a folder, recursively.

```nohighlight
import Rhino as r
import Eto.Forms as ef
import System.IO as sio
import os
import os.path
import clr
import scriptcontext as sc

clr.AddReference('System.Xml')
import System.Xml as sysxml

file_dialog = ef.SelectFolderDialog()
rc = file_dialog.ShowDialog(r.UI.RhinoEtoApp.MainWindow)

if rc == ef.DialogResult.Ok:
    for root, dirs, files in os.walk(file_dialog.Directory):
        for fname in files:
            if fname.endswith(".rmtl"):
                xmldoc = sysxml.XmlDocument()
                xmldoc.LoadXml(sio.File.ReadAllText(os.path.join(root, fname)))
                content = xmldoc.SelectSingleNode('xml')
                render_content = r.Render.RenderContent.FromXml(content.InnerXml, sc.doc)
                r.Render.RenderContent.AddPersistentRenderContent(sc.doc, render_content)

```

Addendum: the last line in the script shouldn’t be necessary. I have logged [RH-67920](https://mcneel.myjetbrains.com/youtrack/issue/RH-67920) _RenderContent.FromXml(str, RhinoDoc) incorrect behavior_ to track that.

---

<div class="post-metadata">

**Author:** ![duzhengjie1](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/duzhengjie1/32/365841_2.png) [@duzhengjie1](https://discourse.mcneel.com/u/duzhengjie1)\
**Post date:** [March 18, 2022, 8:59am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/12 "2022-03-18T08:59:37Z")

</div>

Ok,thanks very much!

---

<div class="post-metadata">

**Author:** ![JDev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jdev/32/448332_2.png) [@JDev](https://discourse.mcneel.com/u/JDev)\
**Post date:** [April 25, 2023, 12:40am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/13 "2023-04-25T00:40:33Z")

</div>

What is the process for c#?

```auto
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(File.ReadAllText(rmtlFilePath));
            XmlNode content = xmldoc.SelectSingleNode("xml");
            RenderContent renderContent = RenderContent.FromXml(content.InnerXml, doc);
            RenderContent.AddPersistentRenderContent(doc, renderContent);

```

That last line is deprecated, but not sure how to get a **RenderMaterial** from **RenderContent**? For:

```auto
doc.RenderMaterials.Add(RenderMaterial material)

```

---

<div class="post-metadata">

**Author:** ![nathanletwory](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathanletwory/32/482880_2.png) [@nathanletwory](https://discourse.mcneel.com/u/nathanletwory)\
**Post date:** [April 25, 2023, 4:10am UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/14 "2023-04-25T04:10:10Z")

</div>

```csharp
if(renderContent is RenderMaterial rm) {
    doc.RenderMaterials.Add(rm);
}

```

---

<div class="post-metadata">

**Author:** ![JDev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/jdev/32/448332_2.png) [@JDev](https://discourse.mcneel.com/u/JDev)\
**Post date:** [April 25, 2023, 12:51pm UTC](https://discourse.mcneel.com/t/how-to-dynamically-add-materials-to-rhino-doc-from-rmtl-file/139282/15 "2023-04-25T12:51:16Z")

</div>

Yeah I should have waited a few seconds, I didn’t realize RenderContent was the base class. Thank you for the fast response.
