Reference color with number

Hi,

I read in a object and it has a type number. I want to color that object according to that type number.

First, I created a few colors :

mr1508=model.materials.add
mr1508.color='red'

mr7911372=model.materials.add
mr7911372.color="yellow"

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.

How do I do this?

Thanks in advance.

Regards,

John

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

john

1 Like

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].

1 Like

Thanks John.
mats.add(‘mat_name’) worked.

Thanks. I know the ‘materialName’ now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.