by thomthom » Sun Jul 19, 2009 1:09 pm
Out of Date! A new list can be found here: viewtopic.php?f=180&t=30793&p=270781#p270781 Update — 28 September 2009As of SU7.1 it appears that some observers behave better. Trying to get confirmation on this. Update — 2 October 2009Many of the MaterialObserver events seems to be working after all. At least in SU7.1. Will have to check with older versions again. Update — 19 November 2009Two events of the SelectionObserver doesn't trigger. Seems to go back to at least SU6. Update — 28 February 2010InstanceObserver.onClose does not trigger - at least not under SU7.1. --- When I started my work on my DoubleCut plugin I ended up dealing allot with Sketchup's Observers. As it turns out, it's a treacherous land. Some simply doesn't work and some are bugged. Some are so bugged they crash Sketchup. The information about the gotchas was scattered and few, so I've compiled my own list here: (SU6 + SU7) Works- AppObserver
- DefinitionsObserver
- [SU7.1+] EntityObserver - Could Bugsplat in earlier versions.
- LayersObserver - (.onCurrentLayerChanged also triggers if you rename the current layer.)
- ModelObserver
- ToolsObserver - (Might be some weird behaviour with this one. Not been able to confirm.)
- OptionsProviderObserver
- PagesObserver
- RenderingOptionsObserver
- ShadowInfoObserver
- ViewObserver
Bugged- [SU7.1+] EntitiesObserver - Could Bugsplat in earlier versions. .onContentsModified doesn't trigger.
- DefinitionObserver - Events doesn't trigger.
- InstanceObserver - Event's doesn't always trigger. Seem to be more reliable on SU6.
- MaterialsObserver - .onMaterialUndoRedo doesn't trigger. .onMaterialRemoveAll doesn't trigger correctly.
- SelectionObserver - .onSelectionAdded and .onSelectionRemoved never triggers. Instead, .onSelectionBulkChange and .onSelectionCleared always triggers.
Bugsplats!- [SU7.0-] EntitiesObserver
- [SU7.0-] EntityObserver
(Seems to be fixed in SU7.1)They cause strange bugsplats when using. I've seen this behaviour on both SU6 and SU7, both with clean installations. These observers doesn't crash SU immediately, but sneaks up on you later on. The only simple semi-reliable way to recreate the bug I know is this: - Attach an EntityObserver or EntitiesObserver to an appropriate entity in the model.
- File->New to create a new model (nothing happens)
- File->New again to create a new model - bugplat
This doesn't happen every time. Some times you need to do some more File->New - maybe draw some geometry. But once either one of these observers are used SU will at some point crash with a bugsplat. I have tried to remove the observers after creating them, but that doesn't seem to help at all. Once one of them has been used SU becomes volatile. Other notesAnother thing I learned was that doing modifications to the model on observer events can cause lots of problems. - If a script is doing something which trigger an event that modifies the model it will break the undo stack. I also seem that it can make SU unstable. You could potentially cause infinite loops.
- After consulting with Scott it seems that queueing up a list of modifications to be done to the model and executing the list on ModelObserver.onTransactionCommit is the most reliable way to do it.
Last edited by thomthom on Thu Nov 19, 2009 10:17 am, edited 12 times in total.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Matt666 » Tue Jul 21, 2009 12:37 pm
Hi Thomthom! Great post! I also had some problems with entityObserver... See here. There is a problem with entities created by ruby...
-
Matt666
-
- Posts: 830
- Joined: Wed Dec 05, 2007 8:38 am
- Location: 48.1184, -1.675
- Name: Matt
-
by thomthom » Mon Jul 27, 2009 12:25 pm
Matt: did you find a way around your problems?
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by clarryr » Sat Aug 08, 2009 6:15 pm
I am also looking at the reliability of the observer classes. So far I've found that the model passed to ModelObserver.onDeleteModel is wrong. When the model started I set model.name = "myname". On all the other methods I tested in ModelObserver I was able to see myname. Not so on onDeleteModel. Larry
-
clarryr
-
- Posts: 2
- Joined: Sat Jul 25, 2009 5:04 am
- Name: Larry Rogers
by AdamB » Sat Aug 08, 2009 9:54 pm
clarryr wrote:I am also looking at the reliability of the observer classes. So far I've found that the model passed to ModelObserver.onDeleteModel is wrong. When the model started I set model.name = "myname". On all the other methods I tested in ModelObserver I was able to see myname. Not so on onDeleteModel. Larry
Yep. My understanding is that all delete methods are called after the fact. So the referenced object is toast by the time you are invoked. Furthermore, I believe most problems caused by Observers is hanging onto stale references to Ruby objects that have been deleted. If you create null (do nothing) Observers, they are benign. Adam
-

AdamB
- LightUp Support

-
- Posts: 936
- Joined: Wed Dec 12, 2007 10:49 am
- Location: Brighton, UK
- Name: AdamB
- Operating system: Mac
- SketchUp version: 2014
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by clarryr » Sun Aug 09, 2009 12:37 am
Yep. My understanding is that all delete methods are called after the fact. So the referenced object is toast by the time you are invoked.
Furthermore, I believe most problems caused by Observers is hanging onto stale references to Ruby objects that have been deleted. If you create null (do nothing) Observers, they are benign.
Adam
That's pretty much what I guessed. Unfortunately, the model reference passed in is not nil. It doesn't seem to reference anything but it doesn't compare equal to nil and it's not the active_model either. Ruby seems to be pretty good about detecting and reporting method calls on nil but I can see where having this mystery model reference might cause problems. Bottom line seems to be to clean up in onSaveModel and ignore onDeleteModel. I was trying to be a good citizen and remove all of my observers when the model was deleted but Sketchup doesn't seem to care or has already removed them. One thing I can do when onDeleteModel is called is I do know 'self', so if I need to I can track all of my other observers as instance variables of my model observer. Larry
-
clarryr
-
- Posts: 2
- Joined: Sat Jul 25, 2009 5:04 am
- Name: Larry Rogers
by Malkomitch » Fri Aug 21, 2009 9:47 am
EntitiesObserver works well if you don't erase any entity inside the observer. Otherwise, it makes Sketchup crash in an ugly way.
I'm not sure about EntityObserver but i think that's pretty much the same behavior.
Thank you for the report
-
Malkomitch
-
- Posts: 13
- Joined: Fri Mar 13, 2009 5:49 pm
by thomthom » Fri Aug 21, 2009 9:52 am
Malkomitch wrote:EntitiesObserver works well if you don't erase any entity inside the observer. Otherwise, it makes Sketchup crash in an ugly way.
I'm not sure about EntityObserver but i think that's pretty much the same behavior.
Thank you for the report
In my experience I found adding the observers to cause a delayed crash no matter what - even if I never modified the model. Just attaching it.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Malkomitch » Fri Aug 21, 2009 2:39 pm
Do you call the same observer several times?
for example, if an AppObserver.onNewModel of your own call a new EntityObserver each time you perform a File>New, you'll stack the EntityObservers without removing the old one. Every methods trigger n times and it may cause unwanted issues.
try to track your observers by storing them in a global variable, for example. And if you find some duplicates, use Sketchup.active_model.entities.remove_observer before calling a new one
In my scripts, if i use observers, i'm used to call a little function "refreshObservers" of my own, triggered by AppObserver.onNewModel or AppObserver.onOpenModel, that flushes all the old observers and create new ones.
I'm using an EntitiesObserver every day. This observer is designed to build and update a catalogue of everything drawn in or removed from sketchup. With onElementAdded and onElementRemoved, I never experienced any crash, excepted when I try to remove an entity inside the EntitiesObserver methods. It breaks the elementary rule: don't touch the collection you're watching in
-
Malkomitch
-
- Posts: 13
- Joined: Fri Mar 13, 2009 5:49 pm
by thomthom » Fri Aug 21, 2009 2:52 pm
hmm... will have to look deeper into it again. but I mean to remember that even when I removed the observers I still got crashes... hm..
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by AdamB » Sun Aug 23, 2009 7:14 pm
Malkomitch wrote:try to track your observers by storing them in a global variable, for example. And if you find some duplicates, use Sketchup.active_model.entities.remove_observer before calling a new one
Very good advice. Also remove_observer will accept without error an observer that is currently not attached - I always use the cliche of "remove followed by add" to ensure you have just 1 observer.
-

AdamB
- LightUp Support

-
- Posts: 936
- Joined: Wed Dec 12, 2007 10:49 am
- Location: Brighton, UK
- Name: AdamB
- Operating system: Mac
- SketchUp version: 2014
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Jim » Thu Sep 03, 2009 4:28 am
I just noticed the AppObserver onNewModel event fires even if you hit the Cancel button on the "Save changes" dialog.
Hi
-
Jim
- Global Moderator
-
- Posts: 4678
- Joined: Mon Nov 12, 2007 10:13 pm
- Location: ohio
- Name: Jim
- Operating system: Windows
- SketchUp version: 2017
- License type: Pro
- SketchUp use: hobby
- Level of SketchUp: Intermediate
-
by fredo6 » Sun Sep 06, 2009 11:24 pm
Maybe the most efficient is to use a unique class instance for ALL observers. The observer class does not even need to be typed (i.e. a subclass of the Sketchup observer classes), as there is no check done by the methods add_observer (like for the Tool classes).
All callback methods have different names and the relevant information is always in the arguments, so that you have the context of the entities and objects you 'observe'.
This unique instance can easily be maintained at module level.
Fredo
PS: it is a little bit misleading that the API documentation always shows examples with creation of new observer class everytime it is attached to an entity, like Sketchup.active_model.entities.add_observer(MyEntitiesObserver.new)
-

fredo6
- Premium Member

-
- Posts: 4970
- Joined: Mon Nov 12, 2007 9:07 pm
- Location: France
- Name: Fredo6
- Operating system: Windows
- License type: Pro
- SketchUp use: interior design
- Level of SketchUp: Intermediate
by thomthom » Tue Sep 08, 2009 6:23 pm
Fredo6 wrote:PS: it is a little bit misleading that the API documentation always shows examples with creation of new observer class everytime it is attached to an entity, like Sketchup.active_model.entities.add_observer(MyEntitiesObserver.new) 
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Jim » Mon Sep 28, 2009 9:06 am
I don't know if this is "right" or not, but here is my approach. I created a global $jf_observers = {} Then, create a single instance of the observers I need: - Code: Select all
$jf_observers[:layers] = JF::LayersObserver.new When I need an observer, I register a block of code: - Code: Select all
$jf_observers[:layers].register(:onLayerAdded) { |layers, layer| do_something(layer) }
At this point, the observer is attached if it isn't attached (an AppObserver is also created and attached because it needs to re-attach any other observers onNewModel and onOpenModel.) But, there is only a single instance of each observer in existence. There are a handful of entity-specific observers that do not fall under the AppObserver control. But all of the model-level observers can. Using this method, any developer can use an observer simply by registering a block of code to be executed on an event. .register returns an id so you can later .unregister(id) the event, too. The observers detach themselves when there are no more events in their queues. I don't really ever see a reason to detach the AppObserver - it can stay attach forever. I am in the process of coding a "suit" of observers that behave in a similar fashion, but I am not really far enough to know if it is a viable or stable strategy; although I think it is a solid approach.
Hi
-
Jim
- Global Moderator
-
- Posts: 4678
- Joined: Mon Nov 12, 2007 10:13 pm
- Location: ohio
- Name: Jim
- Operating system: Windows
- SketchUp version: 2017
- License type: Pro
- SketchUp use: hobby
- Level of SketchUp: Intermediate
-
by RickW » Fri Oct 02, 2009 10:03 am
For my part, I created the SmustardAppObserver that allows plugins to add calls to the Observer instance. When an event is triggered, the observer will parse the list of calls.
-
RickW
-
- Posts: 771
- Joined: Fri Nov 16, 2007 6:38 am
- Location: Wichita, KS
- Name: RickW
-
by thomthom » Thu Nov 19, 2009 10:18 am
Updated to reflect finding of bugged events in SelectionObserver.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Thu Nov 19, 2009 10:32 am
There also seems to be some oddness with DefinitionsObserver. Event's unexpectedly triggering. At least in SU7.1. SU6:- Group/Component Creation: onComponentPropertiesChanged
- Paste: onComponentPropertiesChanged
SU7:- Group/Component Creation: onComponentPropertiesChanged
- Paste:
- Before you place the component: onComponentRemoved and onComponentPropertiesChanged
- After: onComponentRemoved
- Ctrl+Move: onComponentRemoved
Not sure if the onComponentRemoved started triggering in SU7.0 or 7.1.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Sun Feb 28, 2010 1:52 pm
InstanceObserver.onClose does not trigger - at least not under SU7.1.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by kwalkerman » Wed May 19, 2010 6:32 pm
EntityObserver - (and possibly others)
it seems that OnEraseEntity is activated after onChangeEntity, which means that if the entity is erased, it can cause bugs for whatever you are trying to do with OnChangeEntity. If OnEraseEntity were activated first, you could create a simple boolean value:
def initialize @still_here = true end
def OnEraseEntity(entity) # whatever you want to do... @still_here = false end
def OnChangeEntity(entity) if (still_here) # whatever you want to do... end end
This is a problem because the entity seems to be erased before OnChangeEntity is called, which means that doing something to the entity gives errors, but your observer doesn't know it until it gets through OnChangeEntity to OnEraseEntity.
-- Karen
-
kwalkerman
-
- Posts: 148
- Joined: Mon Feb 08, 2010 9:48 pm
- Name: kwalkerman
by thomthom » Wed May 19, 2010 6:52 pm
kwalkerman wrote:This is a problem because the entity seems to be erased before OnChangeEntity is called, which means that doing something to the entity gives errors, but your observer doesn't know it until it gets through OnChangeEntity to OnEraseEntity.
Yea - the Entity and Entities observers aren't easy to deal with. 
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Jernej Vidmar » Fri Sep 03, 2010 1:19 pm
Hi guys, seems like we have found new observer problem (MacOSX + SketchUp8). When InstanceObserver is attached to the Group and SketchUp is then exited, bugsplat window appears. If the model is saved before exiting SketchUp, no bugsplat window appears. - Code: Select all
class TestObserver < Sketchup::InstanceObserver def onOpen(entity) p 'on called' end
def onClose(entity) p 'onClose called' end end # Select a Skethcup Group and call this method # so the observer will be attached to the Group def attach_observer group = Sketchup.active_model.selection[0] group.add_observer(TestObserver.new) end
Can anyone please confirm that? It seems to be MAC OS X + SketchUp 8 specific problem, Windows version works OK, and SU 7 on Mac OS X too. Cheers, N78
-
Jernej Vidmar
- Modelur
-
- Posts: 36
- Joined: Thu Dec 20, 2007 6:08 pm
- Name: Jernej Vidmar
-
by thomthom » Fri Sep 03, 2010 2:15 pm
I've not gotten around to test the InstanceObserver - but I'll see if I can test it this weekend.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by AdamB » Fri Sep 03, 2010 6:01 pm
Yes, I can confirm its a repeatable bug on Mac OSX SketchUp 8.
I've logged a very grumpy bug report with Google.
Useful to know you can stop the crash by saving before exiting, but I am disappointed a bug like this could be missed. I know software has bugs in it etc, but this seems like any basic regression testing / smoke testing would flush this one out.
-

AdamB
- LightUp Support

-
- Posts: 936
- Joined: Wed Dec 12, 2007 10:49 am
- Location: Brighton, UK
- Name: AdamB
- Operating system: Mac
- SketchUp version: 2014
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by spring.freediver » Wed Sep 29, 2010 9:12 pm
Does the View.remove_observer method work in SU7.1?
I have a tool that needs to turn a ViewObserver on and off.
In tool methods that receive a view argument: I use "@observer = view.add_observer(MyViewObserver.new)" to turn it on; and "view.remove_observer(@observer)" to turn it off.
view.remove_observer returns false, and the observer is not removed.
I tried changing @observer to a class variable (@@observer), and it still did not work.
Any ideas?
-
spring.freediver
-
- Posts: 50
- Joined: Wed Mar 04, 2009 7:54 pm
- Location: USA
- Name: spring.freediver
- Operating system: Windows
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
-
by thomthom » Wed Sep 29, 2010 9:33 pm
hm... I have not tried to remove observers from view objects... btw - I am compiling a new observer list: viewtopic.php?f=180&t=30793If you find new information not included in the list - can you please let me know so I can update the list?
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Wed Sep 29, 2010 9:36 pm
spring.freediver wrote:Does the View.remove_observer method work in SU7.1?
I have a tool that needs to turn a ViewObserver on and off.
In tool methods that receive a view argument: I use "@observer = view.add_observer(MyViewObserver.new)" to turn it on; and "view.remove_observer(@observer)" to turn it off.
view.remove_observer returns false, and the observer is not removed.
I tried changing @observer to a class variable (@@observer), and it still did not work.
Any ideas?
Hang on... aren't you suppose to do it like this: @observer = MyViewObserver.new view.add_observer(@observer)... view.remove_observer(@observer) 
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Wed Sep 29, 2010 9:37 pm
Yea - pretty sure so - because .add_observer also returns true/false . You need to keep a reference to the actual observer instance.
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by AdamB » Fri Oct 15, 2010 9:38 am
AdamB wrote:Yes, I can confirm its a repeatable bug on Mac OSX SketchUp 8.
I've logged a very grumpy bug report with Google.
Useful to know you can stop the crash by saving before exiting, but I am disappointed a bug like this could be missed. I know software has bugs in it etc, but this seems like any basic regression testing / smoke testing would flush this one out.
After I'm done with a Tool, I always done .pop_tool rather than .select_tool(nil) because it seems less presumptuous. ie restore what the user was doing before rather than cancel their previous selection. However, Gaieus found out a problem/conflict with Jim Toolbar Organizer - long story short, I've switched to doing select_tool(nil) to avoid some weird race-condition with menu validation procs. But it seems to have cured the crash on exit of SU8 on Mac when using Observers as well... Adam
-

AdamB
- LightUp Support

-
- Posts: 936
- Joined: Wed Dec 12, 2007 10:49 am
- Location: Brighton, UK
- Name: AdamB
- Operating system: Mac
- SketchUp version: 2014
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by thomthom » Fri Oct 15, 2010 9:51 am
AdamB wrote:After I'm done with a Tool, I always done .pop_tool rather than .select_tool(nil) because it seems less presumptuous. ie restore what the user was doing before rather than cancel their previous selection.
However, Gaieus found out a problem/conflict with Jim Toolbar Organizer - long story short, I've switched to doing select_tool(nil) to avoid some weird race-condition with menu validation procs.
But it seems to have cured the crash on exit of SU8 on Mac when using Observers as well...
what? damn! I rely on this feature for a plugin I'm making. what kind of race condition? you got a small example? windows, osx?
-

thomthom
- PluginStore Author

-
- Posts: 19457
- Joined: Tue Nov 13, 2007 12:47 pm
- Location: Trondheim, Norway
- Name: Thomas Thomassen
- Operating system: Windows
- SketchUp version: 2019
- License type: Pro
- SketchUp use: other
- Level of SketchUp: Advanced
-
by Ad Machine » 5 minutes ago
-
Ad Machine
- Robot
-
- Posts: 2012
-
Return to Developers' Forum
|