sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Reusing menus - what is going on?

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 6 Posters 551 Views 6 Watching
    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.
    • thomthomT Offline
      thomthom
      last edited by

      I'm baffled - what's going on with menu items which some times can be reused and some times not?

      Have a look at this small snippet:

      http://sketchucation.com/forums/viewtopic.php?t=28184

      <span class="syntaxdefault"><br />require </span><span class="syntaxstring">'sketchup.rb'<br /></span><span class="syntaxdefault">unless file_loaded</span><span class="syntaxkeyword">?(</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">basename</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">__FILE__</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  plugins_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Plugins'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  $tt_menu  </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> plugins_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"ThomThom's Plugins"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  $tt_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Website'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">openURL</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'http://www.thomthom.net/su'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">  $tt_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'@ Sketchucation'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">openURL</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'http://www.thomthom.net/scf'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">  $tt_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_separator<br />end<br />file_loaded</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">basename</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">__FILE__</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault"></span>
      

      In TT_Lib2 I have this code:

      <span class="syntaxdefault"><br />module TT<br />  def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> name </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$tt_menu</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> $tt_menu </span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> name </span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">  end<br />end<br /></span>
      

      Most my plugins call TT.menu instead of UI.menu so when I add that first snippet to my Plugins folder then all the menus for my plugins end up in a single submenu under Plugins.

      menu.png

      However, as you can see in the screenshot, when I try to add menus via the Ruby console nothing appear. Neither does it help if I put the snippet in a file (m2.rb) and load it.

      What gives? Why can menus some times be added, and some times not?

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

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        This however works:

        m1.rb

        <span class="syntaxdefault"><br />plugins_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Plugins'</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">$xx_menu </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> plugins_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_submenu</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"ThomThom's Plugins"</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">$xx_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Website'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">openURL</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'http://www.thomthom.net/su'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">$xx_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'@ Sketchucation'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> UI</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">openURL</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'http://www.thomthom.net/scf'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">$xx_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_separator<br /><br />load </span><span class="syntaxstring">'c;/m2.rb'<br /></span><span class="syntaxdefault">load </span><span class="syntaxstring">'c;/m3.rb'<br />&nbsp;</span><span class="syntaxdefault"></span>
        

        m2.rb

        <span class="syntaxdefault"><br />$xx_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'foo2'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'arrg2'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"></span>
        

        m3.rb

        <span class="syntaxdefault"><br />$xx_menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">add_item</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'foo3'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'arrg3'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault"></span>
        

        If I then load load 'c:/m1.rb' from the Console, menus are added!

        But trying to reuse after that $xx_menu won't work.

        So it appear that it only work if it's all done within one action. Which is why my menu plugin works when SketchUp starts up, because the all the plugins are loaded within the same "loop" (??). ... This is awkward and annoying.

        I can probably make it work, what I want to do, but it'll hard to debug as I need to actually restart SketchUp every time...

        😞

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

        1 Reply Last reply Reply Quote 0
        • Chris FullmerC Offline
          Chris Fullmer
          last edited by

          This isn't an official answer, just from what I've seen playing around, but yeah, you've got it all sorted out. They can be added together when SU starts up, but after it is started, those sub menus are no longer accessible.

          I didn't know you could add multiple into a new submenu after startup, that is interesting. Not too surprising I guess, but still new to me.

          Lately you've been tan, suspicious for the winter.
          All my Plugins I've written

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            Also, you can't from the console:

            m = UI.menu('Plugins')
            m.add_item(...){}

            But you can chain it:

            UI.menu('Plugins').add_item(..){}

            Sent from my LT25i using Tapatalk 2

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

            1 Reply Last reply Reply Quote 0
            • A Offline
              Aerilius
              last edited by

              I think toolbars behave similar, don't they?
              When is the UI (submenus or toolbars) "locked up", when it's first displayed or earlier?

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @aerilius said:

                I think toolbars behave similar, don't they?
                When is the UI (submenus or toolbars) "locked up", when it's first displayed or earlier?

                Good questions - I thought it was when it was displayed. But I might be wrong...

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

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  Within the docs for the C++ SDK, is explained why the C++ references may not be persistent.

                  Basically at the beginning of every SDK call, the coder must get a handle to the SketchUp application.
                  From that reference, you go on to get handles to child objects of the app, such as the App's window object, menubar object, etc.
                  This application handle (reference) gets discarded at the end of the called function.

                  I think it is a bit weird, as the application handle (and it's app window, main menubar, etc.) should not change during the session.. and constant references could be created for them. (And I have done so using C and Win32API.so.)

                  I would think it should also be possible also in C++, because the API does create constant references to some other things.

                  It is annoying each time you call UI.menu("MenuName") a new and different reference is created on the Ruby-side.

                  What I wish for.. is the UI.menu method to accept menu pathname (sometimes called an xpath,) .. for example:
                  UI.menu('/') # the root menubar ref
                  UI.menu('/Draw') # top level 'Draw' menu
                  UI.menu('/Draw/Guide') # the 'Guide' submenu of the 'Draw' topmenu

                  So let's say I want to create a toplevel "Ruby" menu (and I've wanted to for SO LONG!)
                  I would do something like this:
                  RUBY_MENU = UI.menu('/').add_submenu('Ruby') verb = RUBY_MENU.add_item('$VERBOSE') { $VERBOSE = !$VERBOSE } RUBY_MENU.add_validation_proc(verb) { $VERBOSE ? MF_CHECKED : MF_UNCHECKED }

                  Of course we need persistent Ruby-side references to UI objects.

                  💭

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    +1

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

                    1 Reply Last reply Reply Quote 0
                    • fredo6F Offline
                      fredo6
                      last edited by

                      I am also facing this problem in the situation where a plugin is freshly enabled

                      • either from the Preferences dialog box
                      • or via the installers (Add Extension, EWH, Plugin Store).

                      I put the menus of my scripts under Tools > Fredo6 Collection. I keep the handle of this top menu.

                      However, if the extension of a plugin is enabled, the handle to this menu is no longer 'valid' for methods such as add_item or add_submenu. It does not really crash or even report an error, but nothing appear under the menu.

                      Basically, this means that when a user will enable or install a plugin 'on the fly', I cannot make it load in the current SU Session. Instead, I'll have to display a message "will load next time you start Sketchup".

                      Note that the problem exists when you group your Plugins under a single menu entry, usually the author name, which is precisely the recommended practice.

                      Reading the present thread, I don't think there is anything to do to circumvent this problem.

                      Fredo

                      1 Reply Last reply Reply Quote 0
                      • Chris FullmerC Offline
                        Chris Fullmer
                        last edited by

                        Yeah, no work arounds Fredo. The user will have to restart SU.

                        Lately you've been tan, suspicious for the winter.
                        All my Plugins I've written

                        1 Reply Last reply Reply Quote 0
                        • fredo6F Offline
                          fredo6
                          last edited by

                          @chris fullmer said:

                          Yeah, no work arounds Fredo. The user will have to restart SU.

                          Chris,

                          Then we need to show a message stating this when the plugin is loaded / reloaded from SU extension manager, EWH or Plugin Store.

                          The toolbar works however, but the fact that the menu are missing may be misleading

                          Fredo

                          1 Reply Last reply Reply Quote 0
                          • Chris FullmerC Offline
                            Chris Fullmer
                            last edited by

                            A message would be nice. That is something each developer will have to put into their own scripts for now.

                            Lately you've been tan, suspicious for the winter.
                            All my Plugins I've written

                            1 Reply Last reply Reply Quote 0
                            • thomthomT Offline
                              thomthom
                              last edited by

                              Would it be possible to change the menu API to give persistent references to menus?

                              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
                                tomot
                                last edited by

                                @thomthom said:

                                So it appear that it only work if it's all done within one action. Which is why my menu plugin works when SketchUp starts up, because the all the plugins are loaded within the same "loop" (??). ... This is awkward and annoying.

                                I can probably make it work, what I want to do, but it'll hard to debug as I need to actually restart SketchUp every time...

                                😞

                                http://sketchucation.com/forums/viewtopic.php?f=180&t=52489&start=30

                                Yes its a great way to waste more time!
                                Its enough to make one take up quantum computing instead 🤓

                                [my plugins](http://thingsvirtual.blogspot.ca/)
                                tomot

                                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