Observers that persist between Sketchup sessions?

Yes this is how it must be done.

See the API docs for Sketchup::AppObserver

You create 1 master AppObserver subclass instance (inside your plugin’ module namespace,) and attach it to the application using Sketchup::add_observer().

ADD (2022-02-14): Since a module is an instance of class Module and an extension will only ever need 1 AppObserver instance, the extension submodule itself can act as the singleton AppObserver object for the extension. This simplifies code. See example link below.

Within the AppObserver callbacks onNewModel() and onOpenModel() you attach a new instance of your plugin’s custom Sketchup::ModelObserver subclass, passing in the model instance reference as an argument with the ::new() constructor.

Within your model observer’s initialize(model) method you can attach new instances of your custom EntitiesObserver subclass to whatever entities collection object that exist at that time. (Which will always be at least the model level entities, even be it empty.)
You can also at this time attach other observer subclass instances to other collection objects that belong to the model, such as the Definitions, Materials, Pages, Selection, and Styles collections.

You could also attach a new Sketchup::EntityObserver subclass instance, to specific drawing objects, by iterating Entities collections, and checking for some property or perhaps your plugin’s custom AttributeDictionary (use “AuthorNamespace_PluginName” as the dictionary name,…) with a certain attribute set to some predetermined value.


ADD (2022-02-14): I’ve since posted an example here:

2 Likes