below is one example which works without using material ids or indices, just by using the material name:
# -*- coding: utf-8 -*-
import Rhino
import scriptcontext
import rhinoscriptsyntax as rs
def ApplyMaterialTest():
ids = rs.GetObjects("Select objects", 8+16+32, True, True, True)
if not ids: return
# define path to the material and its name with extension
path = "D:\\Temp\\"
name = "Red Plastic.rmtl"
# load the material, surround path+name with quotes to prevent errors
cmd = "_RenderLoadMaterialFromFile "
rc = rs.Command(cmd + chr(34) + path + name + chr(34), False)
if not rc: return
# assign loaded material name (without extension) to selected objects
cmd = "_RenderAssignMaterialToObjects "
rc = rs.Command(cmd + chr(34) + name[0:-5] + chr(34), False)
if not rc: print "Error assigning material"
if __name__=="__main__":
ApplyMaterialTest()
Note that you get an error message dialog if the material does not exists and if multiple materials are present with the same name in the material editor, _RenderAssignMaterialToObjects prints an error to the command line and does not assign it.
@Alain, many of the material related methods are still missing in python rhinoscript syntax. Could you please add these to Rhino 6:
@clement
Hi clement,
If the name to give material was duplication,there was more than one material to use this name to give material failure.
How do you do for the material of duplicating name?
can you use “_RenderRemoveUnusedMaterials” to remove the material of duplicating name before?
to prevent that a material gets loaded if it is already found, you could check all material names first and put the _RenderLoadMaterialFromFile below a condition:
# check if the material name is already present
materials = [mat.Name for mat in scriptcontext.doc.Materials]
if not name[0:-5] in materials:
# load the material, surround path+name with quotes to prevent errors
cmd = "_RenderLoadMaterialFromFile "
rc = rs.Command(cmd + chr(34) + path + name + chr(34), False)
if not rc: return
else:
print "material '{}' is loaded already".format(name[0:-5])
If you change this in the script above, the material which is loaded with the same name gets assigned. Does this help ?
def ApplyMaterialTest():
ids = rs.GetObjects(“Select objects”, 8+16+32, True, True, True)
if not ids: return
# define path to the material and its name with extension
path = "D:\\Temp\\"
name = "Red Plastic.rmtl"
# check if the material name is already present
materials = [mat.Name for mat in scriptcontext.doc.Materials]
if not name[0:-5] in materials:
# load the material, surround path+name with quotes to prevent errors
cmd = "_RenderLoadMaterialFromFile "
rc = rs.Command(cmd + chr(34) + path + name + chr(34), False)
if not rc: return
else:
print "material '{}' is loaded already".format(name[0:-5])
# assign loaded material name (without extension) to selected objects
cmd = "_RenderAssignMaterialToObjects "
rc = rs.Command(cmd + chr(34) + name[0:-5] + chr(34), False)
if not rc: print "Error assigning material"