How can change rendermaterial category?

I did not find an API in RDK that can modify render materials category. I tried replacing one render material with another, as if RenderContent.Repleace was not working.

Can’t anyone help me? Waiting anxiously.

Hi @401247906,

if you have for example a render material (rm) which is not of type “Plastic” and want it to change to Plastic, you can do this:

if rm.TypeName != "Plastic":

    # delete children of this render material 
    context = Rhino.Render.RenderContent.ChangeContexts.Program
    rm.DeleteAllChildren(context)

    # change to plastic
    m_type = Rhino.Render.ContentUuids.PlasticMaterialType        
    rm = Rhino.Render.Utilities.ChangeContentType(rm, m_type, False)

_
c.

I would not delete the children but just the ChangeContentType with the last argument set to True. If you do delete child content explicitly you will not get good conversion if you go between material types that do support say textures in channels.

If a certain child content isn’t supported in the target material it will be automatically handled.

When saying False to parameter harvesting you’ll lose also color and other settings.

To change all materials to PBR materials, and keeping all original material settings where possible:

import scriptcontext as sc
import Rhino.Render

for rm in sc.doc.RenderMaterials:
    newtype_uuid = Rhino.Render.ContentUuids.PhysicallyBasedMaterialType
    Rhino.Render.Utilities.ChangeContentType(rm, newtype_uuid, True)
  • That’s a perfect solution. Thank you very much. :heart:

Hi @nathanletwory, there was a good reason for me by deleting children. I’ve found that the automatic texture conversion gave unexpected results especially in regards to mapping values. It was easier to rewrite material nodes after starting with a fresh material of the new type.

I’ve experienced a few other hick ups when using the ChangeContentType method and switching back and forth between materials with many nested child nodes, including crashes. Removing the children before switching material type prevented those.

_
c.

The way I showed would be exactly the same as what one would do in the GUI by switching the render content type, though.

It does not matter if you do this via code or the GUI, the problems are the same using complex nested materials. For example, If you go from a material of type “Custom” to “Plastic” and the custom material has all texture slots set, most of these slots do not exist in the Plastic material. To prevent that you end up with unwanted automatic conversions, i’ve found it is better to remove children. I did not want to harvest anything.

_
c.

Sure, just for OP it is important to realize that with your method nothing gets transfered, not even diffuse/base color.