List all object names assigned to a certain material?

Is it possible to get a vertical list (one object per line) with all object names assigned to a certain material? With ability to see the layer and object colour as RGB code?
For example, if I select material “Steel 01” and right-click on it, Rhino will let me open a pop-up list whose content could be copied in the clipboard or saved as a text file. It will look like this:

Material: Steel 01
Object name: Tube 1, Layer: Chassis, RGB 128, 128, 128
Object name: Tube 1, Layer: Chassis, RGB 128, 128, 128
Object name: Tube 1, Layer: Chassis, RGB 128, 128, 128
Object name: Tube 2, Layer: Bumper sub-frame, RGB 90, 90, 90

etc…

I have cobbled together a python script that let’s you select an object, and then it will make a list of all objects that have the same material and copy the data to the clipboard. The meat of the code comes from two posts I found in the forums (because, MAN, this was complicated).

[Edit]
I assumed that by “object color” you meant the layer color.

[Edit]
It looks like it only lets you select breps and meshes…so let me know if you need other types of objects and I’ll see if we can add those to the filter.

#! python 2

import rhinoscriptsyntax as rs
import scriptcontext
import Rhino

# select an object and get its RenderMaterial
# from user Nathan Letwory at https://discourse.mcneel.com/t/rhinocommon-unable-to-get-rendermaterial-of-object/184978/7
objTypesFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Mesh
rc, objRef = Rhino.Input.RhinoGet.GetOneObject( "Select object", False, objTypesFilter )

selected_obj = objRef.Object()
mtl = selected_obj.RenderMaterial


# get a list of all objects that have that material
# from user Clement at https://discourse.mcneel.com/t/select-by-material-name-with-python-and-or-rhinocommon/173074/2
settings = Rhino.DocObjects.ObjectEnumeratorSettings()
settings.NormalObjects = True
objs = scriptcontext.doc.Objects.GetObjectList(settings)

rc = filter(lambda i: i.RenderMaterial and i.RenderMaterial.Name==mtl.Name, objs)

# produce the output
text = "Material Name: " + mtl.Name + "\n"
for ob in rc:
    layer_name = rs.ObjectLayer(ob)
    name = ""
    if ob.Name:
        name = ob.Name
    else:
        name = "[unnamed]"
    text += "Object Name: " + name + ", Layer: " + layer_name + ", " + str(rs.LayerColor(layer_name)) + "\n"

# Alert user that the task is done
rs.ClipboardText(text)

rs.MessageBox("Object list has been copied to clipboard")

ListObjectsByMaterial.py (1.3 KB)

[Edit]
I modified the script to produce the list in a CSV format so you can open or import it into a spreadsheet. (I also cleaned up the color section a bit). Just paste the text into your favorite text editor and save it with a .csv extension. Then open/import it into your favorite spreadsheet program.

ListObjectsByMaterial (CSV).py (1.6 KB)

2024-08-10_13-00-57