Materials.purge_unused dosn't work

Should I do something after:

Sketchup.active_model.materials.purge_unused

because when I called above nothing happen. I can still see materials in Materials Dialog in catalog “In Model”.
Should I make some refresh?.

the command (I copy-pasted your line) just works fine for me IF I don’t have an unused material selected in the material editor. If I do have an unused material selected, it won’t purge that one but it will purge the rest.

You’ll still have the materials applied to any unpurged components…even if you deleted them from the drawing.

1 Like

good point.

There are several reason that a seemingly ‘unused’ Material is not purged by this API snippet…

  1. It is actually unused, but it is currently set to be the active material
  2. It is actually unused in any of the model’s contents, but it is used as a material inside a component definition stored in the model [also see 3. below], and that component is itself unused in the model, so you don’t see it.
  3. It seems to be unused, but it is actually used somewhere in the model or a component definition:-
    3.1. It is applied to a back-face that is normally inside an object and you can’t see it.
    3.2. It is applied directly onto a ‘container’ [a group or component instance], but it is not seen because that ‘container’ has no faces within it that are assigned the default material, or it’s only on a back-face [as explained in 3.1].
    3.3. It is applied to a hidden entity, so you never see it.
    3.4. It is applied to an entity assigned an ‘off’ layer, so you don’t see it.
    3.5. It is actually used on some visible entities, but does not display because you have ‘color by layer’ set ‘on’.
    3.6. It is applied directly to some edges [in the model or containers, or definitions], but your Style is perhaps set to show edges as ‘same’ rather than ‘by material’, so again you never see it.
    3.7 It is applied directly to an edge which is not visible because it’s hidden or smoothed as part of a surface, so again you don’t see it…
  4. It is actually used and visible, but you have two very similar looking/colored/textured materials and you mistake them.

So there are several reasons it ‘doesn’t work’, but none are bugs, they are all real reasons…

2 Likes

This one is easy to deal with:

matls = Sketchup.active_model.materials
matl  = matls.current
matls.current= nil if !matl.nil?
matls.purge_unused
matls.current= matl if !matl.nil? && matl.valid?
1 Like