Scripting a material application

I am trying to produce a Ruby script that generates a model where the model includes materials applied to surfaces. It works for basic colors, but not for materials such as Metals or Concrete. Can someone give me the basic approach to do this, or the magic key for where to find things in the API ? Roughly my code issue is represented by this snippet:

model    = Sketchup.active_model 
entities = model.entities 
pts = [] 
face   = entities.add_face( pts )

# this works fine
face.material = "Red" 

# But all these failed
face.material = Sketchup::Materials::Concrete_Form_4x8 
face.material = "Concrete_Form_4x8" 
face.material = "[Concrete_Form_4x8]"
face.material = model.materials["Concrete_Form_4x8"] 

# The last 2 above run without error if i previously
# run the following code. But the resultant texture is 
# just black, which is different than if i make the same
# selection in the GUI and paint the surface.

texture_file = Sketchup.find_support_file(
              "Materials/Asphalt and Concrete"  )
puts "file= #{texture_file} " 
material = model.materials.add('Concrete_Form_4x8')
material.texture = texture_file

I edited your post to wrap up the code as Preformatted Text:

The code gets mangled if you don’t do this.

I looks like you are trying to reference materials that doesn’t exist in the model.
Materials.add let you define new materials - where you can set up the texture.

Though, are you trying to reference materials in the shipped SketchUp materials library? If so - then there is currently no API methods for that.

@sam - there looks to be a bug here.
I tried to quote a piece of the OP’s code - When I did so it didn’t preserve the fact the text was wrapped in a code block. So I manually added back-ticks to mark it up - but then the quote block broke.

Thanks for the answer (and sorry for not recognizing the pre-formated text icon when i originally posted).
Yes, i am trying to reference materials shipped with Sketchup. So now i know i’m hunting for something which is not there.

I assume i should be able to manually export individual shipped materials to jpg files
(TextureWriter), and then use the example code i included to define my own new materials to add to my model ? More than a little tedious, but a way to get there ?

Confirmed, there is a bug here with the hoisting logic. Rich formatting in quotes is a very hard problem.

The origin of the issue is that you quote something with formatting and then we strip it, so you manually try to restore it.

cc @codinghorror

If you want to use the shipped materials you could try to read them yourself. As of SU2014 we ship with the Ruby Standard Library and gems will work (not all - if they need compiling under Windows they’ll fail to install). The SKM files that are the materials on disk are simply renamed zip files. Inside there is the texture and an XML document defining the properties. You could use a zip file reading gem and load the materials yourself.

You can get the location of the material by using Sketchup.find_support_file("Materials").

It’s not trivial, but not hard either. Unfortunately it’s the way to go right now since the API is lacking this feature.

Yea, normally I don’t mind about the stripped formatting, but for code blocks it’s needed to prevent code from being mangled. I also noticed that lists suffer as well - being mangled into hard to read text chunks.

A ‘trick’ would be to make a SKP, kept with your Plugin in its subfolder.
In that SKP make a large array of squares, paint each with the desired material from the Library, save the SKP.
A once only effort.
Now you can load that SKP into the model as a component, when you need the materials [no need to place an instance] - a bit of overkill.
You can easily get the path to it from the _FILE_ of the RB…
All of the Materials in component are then available through model.materials[‘material_name’]
You could get my oldie SKM_Tools and use that to load an individual SKM material into your model.
It replaces the missing API methods… SketchUp Plugins | PluginStore | SketchUcation
[Plugin][Code]SKMtools,Material/ImageTools • sketchUcation • 1
I need to update that tool to utilize the ZIP methods of Ruby2 - currently it uses JAR mini executables [Java] to process aspects of the files - SKMs are just renamed ZIPs - so you can extract parts or make them as needed…

1 Like