GHPython Get BrepFace Material

Hi, I am wondering if anyone knows how to get the Material information for a BrepFace in GHPython? If I have a scene like this:

with sub-object materials applied to a single Brep, can I access the Material information somehow? I tried using GetMaterial as suggested here link, but I don’t think I understand how to get a ‘RhinoObject’ from the BrepFace in GH?

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

sc.doc = Rhino.RhinoDoc.ActiveDoc

for guid in _guids:
    brep = rs.coercebrep(guid)
    for face in brep.Faces:
        compo_index = Rhino.Geometry.ComponentIndex(
                Rhino.Geometry.ComponentIndexType.BrepFace,
                face.FaceIndex
            )
        
        # This doesn't work....
        # 'face' needs to be a 'RhinoObject' ? 
        Rhino.DocObjects.RhinoObject.GetMaterial(face, compo_index) #<--

sc.doc = ghdoc

If anyone has any thoughts I would appreciate it!
thanks so much,
@ed.p.may

Perhaps the answer is in here?

– Dale

Thanks @dale !
That is very helpful.

I am able to get the Materials that way, sort of. If I do something like this:

import Rhino
import scriptcontext as sc
import rhinoscriptsyntax as rs

for i, guid in enumerate(_guids):
    print '- '*10, 'Looking at Brep {}'.format(i), '- '*10
    print 'Using Guid: "{}"'.format(guid)
    
    brep_obj = Rhino.RhinoDoc.ActiveDoc.Objects.FindId(guid)
    print 'I found a "{}" with name="{}"'.format(type(brep_obj), brep_obj.Name)
    
    brep_obj_mat_index = brep_obj.Attributes.MaterialIndex
    print 'The BrepObject has a MaterialIndex of: {}'.format(brep_obj_mat_index)
    
    mat = Rhino.RhinoDoc.ActiveDoc.Materials[brep_obj_mat_index]
    print 'and a Material of: {}'.format(mat)
    
    # This part can't be right? 
    for k in range(4):
        mat_guid = mat.MaterialChannelIdFromIndex(k)
        print '  The MaterialChannel: {} has a guid of: {}'.format(k, mat_guid)
        
        child_mat = Rhino.RhinoDoc.ActiveDoc.Materials.FindId(mat_guid)
        print '    which yields a Material: {}'.format(child_mat)

with a scene with two objects, each of which has sub-object materials assigned:

it will give me those sub-object Materials, eventually:

but the part I am still unsure about is how to find the right Channel Index on the Material to get? As you can see in the above, here I’m just walking through 0-4 and testing those Index’s on the material… There must be some way to see which Channels are used on a specific Material, no?

If I try and get the BrepFace’s MaterialChannelIndex, it always only yields 0 or 1 in this case…

, so that can’t be used within the mat.MaterialChannelIdFromIndex() function above?

note sure where I’m going wrong here, but if you have an idea I appreciate it!

thanks,
-E
@ed.p.may

I think I found a solution now, using GetMaterial

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino

sc.doc = Rhino.RhinoDoc.ActiveDoc

for guid in _guids:
    brep = rs.coercebrep(guid)
    for face in brep.Faces:
        compo_index = Rhino.Geometry.ComponentIndex(
                Rhino.Geometry.ComponentIndexType.BrepFace,
                face.FaceIndex
            )
        brep_obj = Rhino.RhinoDoc.ActiveDoc.Objects.FindId(guid)
        print Rhino.DocObjects.RhinoObject.GetMaterial(brep_obj, compo_index).Name

sc.doc = ghdoc

seems to give me what I’m after here.