In my plugin I define a few materials that I would like to protect from deletion. For example when the CleanUp plugin is used.
To do this I have created a MaterialsObserver
module RG
class MyMaterialsObserver < Sketchup::MaterialsObserver
def onMaterialRemove(materials, material)
if material.name == "area_of_interest"
$AOI_MAT = materials.add("area_of_interest")
$AOI_MAT.texture = File.join(PLUGIN_PATH, "images", "focus_area.jpg")
$AOI_MAT.texture.size = [25.m, 25.m]
end
end
end
end
At first I thought that it would have been possible to add this observer to the app with
Sketchup.add_observer(RG::MyMaterialsObserver.new)
No error was reported (add_observer
returns true
), but it was not working. After several attempts I have realized that I needed to add the MaterialsObserver
to every new or open model:
module RG
class MyAppObserver < Sketchup::AppObserver
def onNewModel(mod)
puts "onNewModel: " + mod.to_s
Sketchup.active_model.materials.add_observer(RG::MyMaterialsObserver.new)
end
def onOpenModel(mod)
puts "onOpenModel: " + mod.to_s
Sketchup.active_model.materials.add_observer(RG::MyMaterialsObserver.new)
end
end
end
Is this the correct approach? Sketchup
responds to add_observer
, but the observer is not triggered. What am I missing?
Thanks!