How to read PBR material data in GH in Python based on a GUID

Hi there,

I am searching for a way in Grasshopper to read out PBR material data like base color, roughness, metalness, ior, opacity (if possible also values like sheen and clearcoat and similar from the Rhino detailed PBM material settings) by a given GUID. Best would be a Python solution but an existing component that does it would be better than nothing.

I know from Human the component “Material Table” but this is very limited and also based on Specular/Glossiness instead of Metallic/Roughness - so unfortunately it does not help. But from the “basic idea” this is similar to what I would need:

Again - best would be a little sample code in Python - but any idea welcome.

Thanks in advance

Hi Ore!
Tricky stuff with Rhino.Render.Variant !
… from this thread Create material, apply to layer and export to rmtl file - #13 by andy (thanks to @andy for the conversion step)
I’ve put up a small working example:


c# RBM rendermaterial parameters.gh (7.1 KB)

  private void RunScript(Guid id, string paramName, object XD, ref object val)
  {
    RhinoDoc doc = this.RhinoDocument;
    Rhino.Render.RenderMaterial mat = doc.Objects.FindId(id).GetRenderMaterial(true);

    var v = mat.GetParameter(paramName) as IConvertible;
    val = Convert.ToDouble(v);
  }

To “explore” what are the correct names of the parameters do as @nathanletwory say here Create material, apply to layer and export to rmtl file - #20 by nathanletwory … export your material as .rmtl and open it with a text editor.

1 Like

One can actually iterate over the entries in the <parameters>since each RenderMaterial exposes its structure as XML.

See my inspect render parameters by code for an example.

1 Like

Hi @nathanletwory thanks for pointing to your script. Unfortunately I could not get it to work. I took the raw file from Github and used it in GHPython (I use Rhino 7 SR14). When trying to run it I get an error message:

Runtime error (MissingMemberException): 'GrasshopperDocument' object has no attribute 'RenderMaterials' Traceback: line 7, in script

Where line 7 is:
rms = list(sc.doc.RenderMaterials)

Not sure what I’m doing wrong. Maybe the context should be Rhino instead of GH? I dont’t know.

Ah, yes, I don’t know how much you can access from Python in GhPython. You’ll need access to the full Rhino document. But you should be able to do something similar in C# based on my script, essentially as what @maje90.

The example script is indeed for regular Rhino Python.

Thank you very much @maje90 for this - it works great and is (almost) exactly what I want (I would have preferred a GHPython script). Fortunately the c# script you provided can easily be modified (even with my very limited c# knowledge) to extract the parameters I need.

So thanks again - your answer (together with @nathanletwory suggestion to export .rmtl) is the solution.

Maybe this is of value? This can be done from a GHPython component. I’ll leave the xml parsing part to you, but this will get the gh xml file of the materials in the active Rhino doc. In this example, I only had one PBR material in the active document called “Fiber”.

import Rhino
import Grasshopper as GH
import ghpythonlib.treehelpers as th

DisplayMaterial = []
MaterialNames = []

for mat in Rhino.RhinoDoc.ActiveDoc.RenderMaterials:
    DisplayMaterial.append([GH.Kernel.Types.GH_Material(mat.Xml)])
    MaterialNames.append([mat.Name])
    xmlOut = mat.Xml  # this is the gh xml material file you would need to parse
    
DisplayMat = th.list_to_tree(DisplayMaterial)
MatNames = th.list_to_tree(MaterialNames)


GHPython_xmlMaterial.gh (41.1 KB)
GHPython_xmlMaterial.3dm (794.6 KB)

2 Likes

Thanks for this info - it uses some interesting tricks I didn’t know before. So for me this is of value for sure.

Regarding the initial problem I have based my solution on the c# script provided by Riccoardo - with some modifications. It now does everything I need.

Kind regards

1 Like