Assign material with a macro

Hi,
I am trying to make a macro to assign a premade material to objects. I found this old thread from rhino 6 but cant get it to work?

So work flow is to click on the macro button, i then selected the objects, when i hit enter it assigns the material already created called “Completed”

Hi @stigolsson

This should work as long as you have a material called Completed

-Materials Options AssignToObjects Completed Pause Enter Enter

HTH, Jakob

I’m using this script as a macro in Rhino. It raises an error when there’s no material in the material table. Other than that it works fine.

You get to choose the material name in a popup:

image

import rhinoscriptsyntax as rs
import scriptcontext as sc
import re


ids = rs.GetObjects("Select objects to change material", preselect=True)

rmats = [rm for rm in sc.doc.RenderMaterials]
materialNames = []
for mat in rmats:
    if not bool(re.search(r"\s", mat.Name)) and len(mat.Name) <= 10:
        materialNames.append(mat.Name)

materialNames.sort()
result = rs.ListBox(materialNames, "", "Materials")

rs.SelectObjects(ids)
cmdString_n = "-_RenderAssignMaterialToObjects " + result + " " "n"
rs.Command(cmdString_n, True)
cmdString_y = "-_RenderAssignMaterialToObjects " + result + " " "y"
rs.Command(cmdString_y, True)

rs.UnselectAllObjects()

Hi, managed to post on a colleagues account :joy: This does what im after very nicely! How do link this script to a button? I tried to past it in the command section of the button editor but had no luck. Completely fresh when it comes to scripting.

Rhino - Running Scripts from Macros (rhino3d.com)

Integrating macros and scripts into your workspace [McNeel Wiki]

Save the script above as a python script and you’ll see in the links above how to setup the button…