Now you can select rhino material in grasshopper

Hi all

Share a c# script I made for selecting default materials from Rhino content library. Some may find it usefull.


LoadRenderMaterial.gh (17.8 KB)

A question here. Is there a method can fetch local Rhino path?

On Windows render content is installed under %APPDATA%\McNeel\Rhinoceros\7.0\Localization\en-US for english version. Change en-US for other localizations`.

On Mac render content is installed under ~Application\ Support/McNeel/Rhinoceros/7.0/Render\ Content/

Check for all .rmtl files.

Thank you, Nathan
One more question please. Can I use .rmtl as Display.DislpayMaterial? If could, how should I do it?

Instead of trying to think about DisplayMaterial you could just use the Custom Preview component and assign through it a RenderMaterial.

Import the .rmtl into your Rhino doc, you can then use its name as input to the Custom Preview component.

I am developing a grasshopper plugin for glassware. Because only use a few material, mainly glass, metal, and no need of scenery, my client wants me to simplity workflow. So I am thinking preview DisplayMaterial in my component.

You should be able to add the render materials you need programmatically to the rhino document and use those.

You can add as few or as many as you deem necessary.

In Rhino 7 if you add a PBR material you’ll be able to use a texture to color your glass as well.

Anyway, your client doesn’t have to know how you implement it in the end, as long as it works?

I don’t know PBR material much, Is it “physical based material”? My client barely understands rendering process. He has been taught switch to raytraced mode in viewport.
I tried to write glass material as DocObjectsMaterial, but geometry become invisible in raytraced mode.

            Material material = new Material();
            material.ReflectionColor = Color.FromArgb(255, 255, 255);
            material.DiffuseColor = Color.FromArgb(255, 255, 255);
            material.AmbientColor = Color.Black;
            material.EmissionColor = Color.FromArgb(10, 10, 10);
            material.Reflectivity = 0.9;
            material.ReflectionGlossiness = 0.1;
            material.Transparency = 0.9;
            material.IndexOfRefraction = 1.0;
            material.FresnelReflections = true;
            material.FresnelIndexOfRefraction = 1.68;
            material.Shine = 30.0;
            return material;

Then creating a PBR material would be best.

You need to set this to 1.52 for glass. Raytraced (and in Rhino 7 also Rhino Render, as they are the same) the fresnel component gets automatically calculated based on the IOR.

It is a bit convoluted, but to create a PBR render material you can do something like

import Rhino 
import scriptcontext as sc

#
#print(Rhino.Render.ContentUuids.PhysicallyBasedMaterialType)

# first create an empty PBR material
pbr_rm = Rhino.Render.RenderContentType.NewContentFromTypeId(Rhino.Render.ContentUuids.PhysicallyBasedMaterialType)

# to get to a Rhino.DocObjects.PhysicallyBasedMaterial we need to simulate the
# render material first.
sim = pbr_rm.SimulatedMaterial(Rhino.Render.RenderTexture.TextureGeneration.Allow)

# from the simulated material we can get the Rhino.DocObjects.PhysicallyBasedMaterial
pbr = sim.PhysicallyBased;

# now we have an instance of a type that has all the API you need to set the PBR
# properties. For simple glass we set color to white, opacity to 0 and opacity
# IOR to 1.52
pbr.Opacity = 0.0
pbr.OpacityIOR = 1.52
pbr.BaseColor = Rhino.Display.Color4f.White

# convert it back to RenderMaterial
new_pbr = Rhino.Render.RenderMaterial.FromMaterial(pbr.Material, sc.doc)
# Set a good name
new_pbr.Name = "My Own Glass"

# Add it to the document
sc.doc.RenderMaterials.Add(new_pbr)

It is very similar how you do it in C#


I got an error. What is this message mean? sc.doc is GrasshopperDoc, but should be RhinoDoc. How to import RhinoDoc?


You see the problem get back. I still unable to add render material as DisplayMaterial.

You should be able to use Rhino.RhinoDoc.ActiveDoc.

Translate to C# and create a new RenderMaterial successful. Thank you, Nathan. But I still cannot take RenderMaterial as DocObjectsMaterial or DisplayMaterial.

    ///Rhino.Render.ContentUuids.PhysicallyBasedMaterialType

    // first create an empty PBR material
    RenderMaterial pbr_rm = (RenderMaterial) RenderContent.Create(ContentUuids.PhysicallyBasedMaterialType, RenderContent.ShowContentChooserFlags.None, Rhino.RhinoDoc.ActiveDoc);
    // to get to a Rhino.DocObjects.PhysicallyBasedMaterial we need to simulate the render material first.
    var sim = pbr_rm.SimulatedMaterial(Rhino.Render.RenderTexture.TextureGeneration.Allow);

    // from the simulated material we can get the Rhino.DocObjects.PhysicallyBasedMaterial
    var pbr = sim.PhysicallyBased;

    // now we have an instance of a type that has all the API you need to set the PBR properties. For simple glass we set color to white, opacity to 0 and opacity IOR to 1.52
    pbr.Opacity = 0.0;
    pbr.OpacityIOR = 1.52;
    pbr.BaseColor = Rhino.Display.Color4f.White;

    // convert it back to RenderMaterial
    var new_pbr = Rhino.Render.RenderMaterial.FromMaterial(pbr.Material, Rhino.RhinoDoc.ActiveDoc);
    // Set a good name
    new_pbr.Name = "My Own Glass";

    // Add it to the document
    Rhino.RhinoDoc.ActiveDoc.RenderMaterials.Add(new_pbr);

    A = new_pbr.Xml;

There is a method "ToPhyiscallyBase " under DocObject.Material class. But it seems doesn’t work.
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_DocObjects_Material_ToPhysicallyBased.htm

I got answer. I use DrawMeshShaded, which will not render geomtery in raytraced mode.

Hi Nathan
Could you give more information about PBR properities? Which effects do they control. Some are confused lke Roughness, ClearcoatRoughness, and OpacityRoughness.
https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Render_ChildSlotNames_PhysicallyBased.htm

Hey @zhuangjia777

First of all you should look at this PhysicallyBasedMaterial instead. My script shows you how to get to this type.

The channels correspond pretty much to Principled BSDF with some very minor variations. The Rhino PBR material in Raytraced and Rhino Render builds on exactly this shader node. In Cycles opacity is called transmission. Displacement is handled separately, but for Rhino PBR made part of the material. Where Cycles provides a triplet (color) for control, like subsurface scattering radius, Rhino gives just a scalar for control.

But otherwise it is the one and the same.