Get all materials in scene (Python)

Hi all,

I want to list all materials in the scene, so I can select a material from a list and assign it to a selected object.

I tried the following code, to no avail:

import import rhinoscriptsyntax as rs

def getSceneMaterials():
    rdk = rs.GetPlugInObject('Renderer Development Kit')

    #get list of all RDK data ids in the document
    rdkALL = rdk.FactoryList()

    #filtered materials ID list
    matIDList = rdk.ContentList('material')

    #get materials names
    matNames = []
    for i in range(len(matIDList)):
        matNames.append(rdk.ContentInstanceName(matIDList[i]))

    for i in range(len(matNames)):
        print(matNames[i])

Hoping this question won’t be buried … :grimacing:

Are you talking about these?
image

you can get to those by the following:

import scriptcontext as sc
mats = sc.doc.Materials

documentation here:
https://apidocs.co/apps/rhinocommon/6.14.19118/T_Rhino_DocObjects_Tables_MaterialTable.htm

Hi Will,

Thanks for your reply!
Alas, the code shows only the used materials:

materials = scriptcontext.doc.Materials
for material in materials:
    print(material)

Returns
Material: neutral (0)

I mean all materials in the scene, even if unused:

Untitled

In the documentation I cannot find a way to show all…

Kind regards,
Dick

sc.doc.RenderMaterials seems to be the correct table you’re looking for

1 Like

Indeed the RenderMaterials table is the one you want.

Just 19 hours ago I posted a snippet where you can see how I loop over all materials:

In this specific script I pick with some extra conditions, but the simplest form to create a list with all materials is:

import scriptcontext as sc
rmats = [rm for rm in sc.doc.RenderMaterials]

My bad. Indeed it’s correct. I couldn’t find the methods right away.
Thanks guys!

When getting all materials present in the document, I’m missing material “neutral”:

What is happening?

Could you share the 3dm with these materials?

Here you go.

conversion.3dm (150.9 KB)

There is definitely something weird going on, need to investigate more.

Hallo @dick.lelyveld,

It turns out that the material is an old-style material. You need to replace it with a new material in v6.

2 Likes

Thanks you very much Nathan! I am much oliged.
Indeed I updated Rhino last week…

1 Like