Material observer behaviour on OS X
-
Hi all,
The behaviour of the MaterialsObserver on OS X is puzzling me: material observers are assigned to active_model.materials, so I would expect them to work on one model only. When creating multiple documents, this works as expected: a onMaterialSetCurrent observer assigned to one model will not respond to changing the active material for another model when using the paint bucket.
However, when running a Ruby script that changes the active material for Sketchup.active_model, observers from all models react to the event. So when running the script below on one model, then creating a new file and running it for that model, the messagebox will appear twice. After creating yet another model and running the script, it will appear three times and so on. Strangely, when using the paint bucket, only the observer for the current model responds.
Is this some glitch or actually expected behaviour?
As I am trying to temporarily switch off the material observer, I am thinking of storing all models and their observers in a hash, so I can just loop through them and switch them off one by one. Does that sound like a sensible solution, or is there a better way to make sure no material observers will react to setting the active material in my script?
class SU2LUX_materials_observer < Sketchup;;MaterialsObserver def onMaterialSetCurrent(materials, material) UI.messagebox ("observer reporting; " + Sketchup.active_model.to_s + " " + self.to_s) end end puts "creating observer" puts myObserver = SU2LUX_materials_observer.new puts "assigning observer" puts Sketchup.active_model.materials.add_observer(myObserver) puts "setting active material" Sketchup.active_model.materials.current = Sketchup.active_model.materials[1]
-
@pistepilvi said:
However, when running a Ruby script that changes the active material for Sketchup.active_model, observers from all models react to the event
Oh? That's certainly not intended. Though I'm not really surprised given the current state of observers.
I'll have a look at this.
-
<offtopic>
There's a Materials.current method ?? For my latest project I created a materials observer listening to active material change just so it could be assigned to a variable so I later could know what material is active. How could I have missed this? cleaning up some code
</offtopic> -
@eneroth3 said:
<offtopic>
There's a Materials.current method ?? For my latest project I created a materials observer listening to active material change just so it could be assigned to a variable so I later could know what material is active. How could I have missed this? cleaning up some code
</offtopic>Beware that the material might not be in the model yet - in older version of SketchUp it can cause crashes if you try to apply the material to entities.
Advertisement