How to recover the group on which the material is applied

Hello, i work on material, i create material by script, then i apply this material on a componentSU_Component_Material
I want to retrieve the component (face or group) on which material is applied, in script. After applying the material, the component is not selected.

Perhaps you could say more about your ultimate goal? I can imagine several ways to go about it, but the best choice will depend on what you are trying to accomplish.

One point of confusion for me is how you apply the material to the component. Is it done manually using the Paint Bucket Tool? If so, that means your first script to create the material completes, you do the painting, and then a second script does the rest. But if the painting is also done via Ruby, I don’t understand why you need to retrieve the component since you had to have a reference to it to paint it.

Why do you want the component not selected at the end? That may be leading you to avoid selecting it as part of the workflow whereas doing the selection could simplify things and it is easy to clear the selection via Ruby.

My final goal is to apply materials to objects in my scene and attach a tag to these objects that will have the name of the initial material. The tag will be used to display/hide some elements of the scene.

Yes i apply the material manually with the Paint Bucket Tool.

When I say that the component is not selected at the end, i mean after painting component not in

model.Sketchup.active_model.selection

Assume that matl is the reference to the one you used to paint with …

model = matl.model
ents = model.active_entities
painted_group = ents.grep(Sketchup::Group).find {|grp| grp.material == matl }
if painted_group
  # do something with valid group
else # painted_group is nil
  # do something else as group was not found
end

When i try

I got this error

You told us:

Then Dan is referring to it:

Therefore you have to assign a material created by your script to that variable (matl ) before you execute the code snippet…

1 Like

Sorry my bad i dont see this

You can also just use …

model = Sketchup.active_model