Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5746

Set object material in Python

$
0
0

Frustrated with how slow the material drop down is (sometimes hangs forever), I’m trying to create a quick dialog that lets me assign a material to an object with a listbox and no thumbnail previews.

I’m struggling to find the material index. I thought it would be the index of the material in the list, but the result seems to be pretty random.

I tried sc.doc.Materials.find() but it seems to always return -1. And sc.doc.RenderMaterials has no find methods.

Surprisingly, there was no rhinoscriptsyntax.GetMaterialIndex() either.

I would appreciate any pointers!

import rhinoscriptsyntax as rs
import Rhino as rc
import scriptcontext as sc
import Rhino.Geometry as rg
    
def AssignMaterialFast():
        ids = rs.GetObjects("Select object to change material", preselect=True)
        
        rmats = [rm for rm in sc.doc.RenderMaterials]
        materialNames = []
        for mat in rmats:
            materialNames.append(mat.Name)
        
        materialNames.sort()
        result = rs.ListBox(materialNames, "Select Material", "Assign Material")
        if result is None: return
        
        for i, mat in enumerate(rmats):
            if mat.Name == result:
                index = i
                break
        
        attr = rc.DocObjects.ObjectAttributes()
        attr.MaterialIndex = index
        attr.MaterialSource = rc.DocObjects.ObjectMaterialSource.MaterialFromObject
        
        for id in ids:
            sc.doc.Objects.ModifyAttributes(id, attr, True)
        sc.doc.Views.Redraw()

    if __name__ == "__main__":
        AssignMaterialFast()

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 5746

Trending Articles