• Login
sketchucation logo sketchucation
  • Login
πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

How observers work ?

Scheduled Pinned Locked Moved Developers' Forum
18 Posts 5 Posters 1.9k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    pvbuero
    last edited by 11 May 2010, 06:10

    Hello,

    I'm new to ruby and I try to make a little script that creates a bunch of scenes (pages) of a model that shows different shading situations of my sketchup-model during a day.
    So far everything worked just fine but now I'd like to have a text object which shows the actual time for each scene and that text should be changed when the scene changes.(manually and automatically when creating an animation) I know something like events from VBA (visual basic) where I've a little programming experience and I figured out that in ruby it might be done with observers ?. But unfortunately I was'n t able to find a tutorial on how those observers are being used.
    I tried this:

    @unknownuser said:

    This is an example of an observer that watches the selection for

     # changes.
     class MyShadowInfoObserver < Sketchup::ShadowInfoObserver
       def onShadowInfoChanged(shadow_info, type)
         UI.messagebox("onShadowInfoChanged: " + type.to_s)
       end
     end
    
     # Attach the observer.
     Sketchup.active_model.shadow_info.add_observer(MyShadowInfoObserver.new)
    

    in my script and hoped the messagebox would be fired when I changed the shading settings, but nothing happened not even an error message...
    So I would be greatful for any helping hint...

    MD

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 11 May 2010, 07:46

      Maybe Sketchup.active_model isn't ready when your script is loaded?
      As a test, add Sketchup.active_model.shadow_info.add_observer(MyShadowInfoObserver.new) to a menu and activate it from there. See if that works.

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • T Offline
        TIG Moderator
        last edited by 11 May 2010, 08:44

        If you have $5 this is a good tool that does what you want... http://www.smustard.com/script/Time

        TIG

        1 Reply Last reply Reply Quote 0
        • P Offline
          pvbuero
          last edited by 14 May 2010, 11:56

          @tig said:

          If you have $5 this is a good tool that does what you want... http://www.smustard.com/script/Time

          Thank's a lot. I already found the time.rb script and bought it. It works quite good, but nevertheless I'm still interested how those observerclasses work and I still wasn't able to figure out how to use them. The time script - by the way - does not use an observer to trigger a new note to display the shadow_info["ShadowTime"]. It just uses a timer which frequently retrieves the ShodowTime an draws it on the screen.
          My goal is to get a trigger whenever the shadowtime changes and that trigger should start a little method to display the new time on the screen. It should work when you change the time manually and it should also work when you start an animation which changes the shadowtime automatically... Is that possible ?

          I managed to define the class, I managed to add the observerclass without an errormessage but then nothing happens 😞 ...
          Therefore my question is: Are there any examples on the web on how those classes are being used ?

          Thanks a lot for your help
          MD

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by 17 May 2010, 15:03

            Your code looks good to me, so I am not sure why it didn't work.

            This works for me:

            class SO
                def onShadowInfoChanged(info, type)
                    puts "changed;#{info.inspect};#{type.inspect}"
                end
            end
            
            Sketchup.active_model.shadow_info.add_observer(SO.new)
            
            

            Hi

            1 Reply Last reply Reply Quote 0
            • T Offline
              thomthom
              last edited by 17 May 2010, 19:18

              @dan rathbun said:

              (1) Jim did not subclass from Sketchup::ShadowInfoObserver

              No need. Su doesn't care. Just implement the methods required.
              I've made observer classes that uses methods for multiple observers.

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 17 May 2010, 19:21

                @jim said:

                This works for me:

                class SO
                >     def onShadowInfoChanged(info, type)
                >         puts "changed;#{info.inspect};#{type.inspect}"
                >     end
                > end
                > 
                > Sketchup.active_model.shadow_info.add_observer(SO.new)
                > 
                

                I assume you mean when the user changes control settings in the Shadow Settings inspector dialog...
                Does it also work (ie, the callback gets called,) when a Ruby call directly modifies ShadowInfo['ShadowTime'] ??

                @jim said:

                Your code looks good to me, so I am not sure why it didn't work.
                I see two (2) issues:
                (1) Jim did not subclass from Sketchup::ShadowInfoObserver
                (2) Neither version creates a 'code controlled' reference (variable,) to the observer instance object. How can you be sure Ruby will not garbage collect the instance, if there is no reference pointing to it? (Meaning that you are relying on Ruby to maintain an anonymous reference to the object.)
                Would this be more stable?

                class SO < Sketchup;;ShadowInfoObserver
                    def onShadowInfoChanged(info, type)
                        puts "changed;#{info.inspect};#{type.inspect}"
                    end
                end
                ShadowInfoWatcher = SO.new
                Sketchup.active_model.shadow_info.add_observer(ShadowInfoWatcher)
                

                The other major issue is that ShadowInfo['ShadowTime'] returns an incorrect time (I posted a bug report on this.) It displays correctly in the dialog (on the time slider, but ShadowInfo['ShadowTime'] seems to return a time which is the "SliderTime" minus the user's OS system (time zone,) difference to GMT (taking Daylight Savings Time into account,) regardless of where the model's geolocation is. The same error occurs whether the model is located in Boulder, Colorado or Melbourne, Florida (My local location.) Since my computer is located at GMT-5 hours (or GMT-4 hours during daylight savings time,) the time returned by ShadowInfo['ShadowTime'] is currently (on DST,) always 4 hours less than the "SliderTime" in the Shadow Settings inspector dialog.

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 17 May 2010, 19:28

                  @thomthom said:

                  @dan rathbun said:

                  (1) Jim did not subclass from Sketchup::ShadowInfoObserver

                  No need. Su doesn't care. Just implement the methods required.

                  I knew that Sketchup does't (currently,) check the observer's superclass.

                  But isn't it a matter of good Ruby programming practice?

                  And, what if (in the future,) Google implements some built-in methods, constants, etc. in the superclass that will need to be inherited by your custom observer's subclass(es)? You'd have to go and edit and re-release all your plugins that use observer's.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    thomthom
                    last edited by 17 May 2010, 19:40

                    @dan rathbun said:

                    But isn't it a matter of good Ruby programming practice?

                    Probably is. But combining several observers into one class sure is handy at times. πŸ˜‰

                    Thomas Thomassen β€” SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • P Offline
                      pvbuero
                      last edited by 17 May 2010, 19:55

                      @unknownuser said:

                      This works for me:

                      This works for me too. Perfect. Thanks a lot.

                      I thought I was doing something basically wrong because the code above was't "my code". It was from the API documentation... strange πŸ˜’

                      1 Reply Last reply Reply Quote 0
                      • P Offline
                        pvbuero
                        last edited by 25 May 2010, 06:27

                        I'm still having trouble with this observer.
                        Now the event is fired whenever I change the shadowinfo settings and I see a messagebox (see below). That's fine so far.
                        Observer.png

                        But I'm not able to overwrite this messagebox-method with my own method.
                        I wrote the following:

                        class SO < Sketchup;;ShadowInfoObserver
                            def onShadowInfoChanged(info, type)
                                UI.messagebox("test the observer")
                            end
                        end
                        

                        ...but still the same message as shown above appears..
                        What have I done wrong ?

                        Thanks for your help...
                        MD

                        1 Reply Last reply Reply Quote 0
                        • D Offline
                          Dan Rathbun
                          last edited by 25 May 2010, 17:38

                          @pvbuero said:

                          I'm still having trouble with this observer.
                          ...but still the same message as shown above appears..
                          What have I done wrong ?

                          Jim's version is still being loaded, probably AFTER your version, so your's gets redefined by his version.

                          Also it's best to restart Sketchup for testing. (Otherwise you have to detatch the old observer instance, redefine the variable to point to a new instance of your new observer class, and attach the new observer instance.)

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • P Offline
                            pvbuero
                            last edited by 26 May 2010, 07:44

                            @dan rathbun said:

                            Jim's version is still being loaded, probably AFTER your version, so your's gets redefined by his version.
                            Also it's best to restart Sketchup for testing. (Otherwise you have to detatch the old observer instance, redefine the variable to point to a new instance of your new observer class, and attach the new observer instance.)

                            Hello Dan,

                            I have restarted sketchup and I've no idea where Jim's version should come from ? I deleted it and replaced it by my code...

                            Thanks MD

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              thomthom
                              last edited by 26 May 2010, 08:02

                              Have you tried giving it a unique name?
                              Or even better wrap your code into a unique module to avoid namespace collision.

                              Thomas Thomassen β€” SketchUp Monkey & Coding addict
                              List of my plugins and link to the CookieWare fund

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                Dan Rathbun
                                last edited by 26 May 2010, 21:24

                                @pvbuero said:

                                I have restarted sketchup and I've no idea where Jim's version should come from ? I deleted it and replaced it by my code...

                                It has to come froma script file in the Plugins folder.

                                if you write a script that opens the Console first thing (name it !!!first.rb or something with exclamtion points so it gets loaded first,) and then in that set
                                $VERBOSE=true
                                you will see warnings issued to the Console whenever a method is overwritten.

                                The problems you have are because you have too many things in the Plugins folder.
                                This is why ThomThom suggested MOST scripts should be in subfolders (and loaded with a loader script.)

                                The easiest way to test is to rename the Plugins folder to something else "_Plugins"
                                and make a new Plugins folder.
                                Put the script under test in the new folder and load Sketchup. Test it.
                                If it works then there is a problem script in the "real" Plugins folder.

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • P Offline
                                  pvbuero
                                  last edited by 27 May 2010, 10:15

                                  Thanks a lot. You're answers are a great help for me as an absolute beginner... πŸ’­
                                  ... I'll try your suggestions this evening and give you a feedback.

                                  1 Reply Last reply Reply Quote 0
                                  • P Offline
                                    pvbuero
                                    last edited by 29 May 2010, 06:13

                                    Hello,

                                    thanks to Dans suggestion πŸ‘ I solved the problem. There was a second testing script which obviously overwrote the script I was working on. I deleted it and everything worked as I intended.

                                    Thanks to all of you for your help.
                                    MD

                                    1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      thomthom
                                      last edited by 31 May 2010, 15:48

                                      @dan rathbun said:

                                      @thomthom said:

                                      @dan rathbun said:

                                      (1) Jim did not subclass from Sketchup::ShadowInfoObserver

                                      No need. Su doesn't care. Just implement the methods required.

                                      I knew that Sketchup does't (currently,) check the observer's superclass.

                                      But isn't it a matter of good Ruby programming practice?

                                      And, what if (in the future,) Google implements some built-in methods, constants, etc. in the superclass that will need to be inherited by your custom observer's subclass(es)? You'd have to go and edit and re-release all your plugins that use observer's.

                                      Kind of seem to be by design: http://forums.sketchucation.com/viewtopic.php?f=180&t=17047&start=150#p171636

                                      Thomas Thomassen β€” SketchUp Monkey & Coding addict
                                      List of my plugins and link to the CookieWare fund

                                      1 Reply Last reply Reply Quote 0
                                      • 1 / 1
                                      • First post
                                        Last post
                                      Buy SketchPlus
                                      Buy SUbD
                                      Buy WrapR
                                      Buy eBook
                                      Buy Modelur
                                      Buy Vertex Tools
                                      Buy SketchCuisine
                                      Buy FormFonts

                                      Advertisement