How to set the material name of sub components but not the material color applied on the components

I am doing code like this…

def material_name comp
	ents=comp[0].definition.entities
	ents.each do |e|
		if e.is_a? (Sketchup::ComponentInstance)
			puts e.material.map{|x| x.name}
		end
	end
end

material_name Sketchup.active_model.selection

You need to write the code inside of triple backticks for it to be readable.

```
Code goes here…
```

To change the name of an instance’s material’s nam you can do something like this:

instance.material.name = "New Name"
1 Like

thank you

When a method definition (def) does not work, try to decompose it and run it in tiny small steps:

  1. For the method arguments declare a variable and assign a value for testing:
    comp = Sketchup.active_model.selection
  2. Copy and paste line by line from the method body into the Ruby console and run it.
    ents=comp[0].definition.entities
  3. If you don’t want to run long loops, break them into a single iteration:
    e = ents[0]
    or
    e = ents.find{ |entity| if entity.is_a?(Sketchup::ComponentInstance) }
    then
    puts e.material.map{|x| x.name}
  4. Now you have the line that gives the error and you can try alternative ways of formulating it. Or inspect it method for method:
    puts e.material