Setting material doesnt work unless I set the material through the UI first

I have a simple line of code that I use to set the material for my concrete wall:

myWall.material = Sketchup.active_model.materials[“ConcreteWall_01_18”]

I went through what I think is the normal import process to import this material. The material looks good and I use it all the time. The problem is, I have to use the material once through the UI (using the paint bucket) before my line of code works. Otherwise myWall is just white. But after using the UI, that line of code works forever in that model.
Once I create a new model, I have to use the UI again.

How can I get this line of code to work without having to use the paint bucket first?

Your code is calling a material [by name] from within the model’s materials collection.
So if it’s not loaded into the model it’s a ‘blank’ material…

One way is to ship the materials with your RBZ, so in the subfolder related to the extension make a subfolder called ‘Materials’ and copy the SKM files there…
The extensions main code can find the subfolder by path, then add ‘/Materials’ to path that folder - let’s call its reference ‘my_materials’.
Now run a test on the existence of the material… e.g.

mat_name = 'ConcreteWall_01_1'
mats = Sketchup.active_model.materials
full_path_to_skm = File join( my_materials, mat_name+'.skm')
unless mat = mats['ConcreteWall_01_1']
  mat = mats.load(full_path_to_skm)
### now 'mat' can be assigned to the wall etc...
myWall.material = mat
1 Like

Thanks! It took me a while to get back to this. But it works perfectly. And I learned a little about ruby. I have a bit of code reworking to do in tons of places in other projects where I can use the File.join call instead of doing it myself and checking for the file separators. Its the little things in life, right!?
Thanks again!