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.each do |e|
		if e.is_a? (Sketchup::ComponentInstance)

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: = "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.
  3. If you don’t want to run long loops, break them into a single iteration:
    e = ents[0]
    e = ents.find{ |entity| if entity.is_a?(Sketchup::ComponentInstance) }
  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