When I read in a type number = “1508”, I want to use color mr1508.
I tryied:
thistype=“1508”
mr=“mr”+thistype
mygroup.material = mr
An error comes up saying Cannot find material named mr1508. I think though mr=“mr1508”, but it is not a material. I don’t want use array to reference color here, because my type number is sparse. Neither i want IF statement, too slow.
model = Sketchup.active_model
mats = model.materials
# name your reference
mat_ref = mats.add('mat_name')
# color your material using the reference
mat_ref.color = 'pink'
# to test if the name string has the same reference
mats['mat_name'] == mat_ref # returns true only if the name was unique
You are confusing a variable (mr1508) that refers to a Material object with a String (“mr”+thistype).
A Material can have a String assigned as its name property, but your code never does so. Said another way, setting the variable mr1508=model.materials.add does not somehow cause “mr1508” to be the name of the new material. You have to do that explicitly via Material#name=“mr1508” (note that “mr1508” is a String, it is not the variable named mr1508). And if you later generate the String “mr”+thistype that is just a String, it is not a Material or a reference to a Material. You have to retrieve a Material from the model’s Materials collection using model.materials[materialName].