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

    Combining Ruby Scripts

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 2 Posters 1.5k Views 2 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.
    • E Offline
      ezt
      last edited by

      I am new to Sketchup and have downloaded a few Ruby scripts which are extemely useful.

      However, would it be possible to combine some of them into one script to save having to go through 2 or 3 one after the other?

      For example I use a 'select by layer' script to select components, then run the 'cutlist and materials' script which only works on selected components.

      It would be nice to be able to run just one script which executed the other 2 one after the other.

      If this is possible, how easy would it be?

      Thanks.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        You look at the end part of the script that runs the menu. Disable these with leading ###.

        Find the commands and write a new short script that has a new def that runs the commands one after the other and is called by a single menu entry...

        TIG

        1 Reply Last reply Reply Quote 0
        • E Offline
          ezt
          last edited by

          Thank you Tig.

          I have virtually no knowledge of Ruby, but I have had a go and I have a couple of questions if I may.

          First:
          I have created a small script called 'myscript' like this -

          @unknownuser said:

          #Make sure this ruby files are loaded
          require "sketchup.rb"
          require "script1.rb"
          require "script2.rb"

          def run_thescripts

          ???????????

          end

          define our file name so we will know when we load it twice

          filename="myscript.rb"

          #run this code the first time this script is loaded
          #If it is loaded again, file_loaded!(filename) will return true
          if !file_loaded?(filename)
          # get the SketchUp plugins menu
          plugins_menu = UI.menu("Plugins")
          # add a seperator and our function to the "plugins" menu
          if plugins_menu
          plugins_menu.add_separator
          plugins_menu.add_item("My Script") { run_thescripts}
          end
          # Let Ruby know we have loaded this file
          file_loaded(filename)
          end

          I am not sure if this is correct (although it seems to be the format from what I have seen in other scripts), and am not sure how to call and run the other two scripts with this. What should I put after the 'def run_scripts' part where the ????? are for two other scripts (script1.rb and script2.rb)?

          Second:

          In the two other scripts I assume that this is the part where I have to comment out the 'menu part' for them.

          @unknownuser said:

          define our file name so we will know when we load it twice

          filename="myscript.rb"

          #run this code the first time this script is loaded
          #If it is loaded again, file_loaded!(filename) will return true
          if !file_loaded?(filename)
          # get the SketchUp plugins menu
          plugins_menu = UI.menu("Plugins")
          # add a seperator and our function to the "plugins" menu
          if plugins_menu
          plugins_menu.add_separator
          plugins_menu.add_item("myscript") { run_scripts}
          end
          # Let Ruby know we have loaded this file
          file_loaded(filename)
          end

          Is it just this part I comment out

          @unknownuser said:

          get the SketchUp plugins menu

             plugins_menu = UI.menu("Plugins")
             # add a seperator and our function to the "plugins" menu
             if plugins_menu
                 plugins_menu.add_separator
                 plugins_menu.add_item("myscript") { run_scripts}
             end
          

          The whole Ruby thing seems quite useful, and I must get my head around it, but thanks for your help in the meantime.

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            I'll give you an imaginary example.
            Script1 selects everything that's on a layer.
            Script2 makes any selection 'Red'.
            Clearly the two must be compatible it's no use selecting faces if the next script wants to process edges !

            The menu item for Script1 might look something like this...

            
            if( not file_loaded?("Script1.rb") )
                UI.menu("Plugins").add_item("Select by Layer") {selectByLayer}
            end
            file_loaded("Script1.rb")
            
            

            You can leave this menu as it is, BUT if you don't want it on the menu put a # in front of the UI.menu... line. The function 'selectByLayer' will be loaded anyway. If you typed at in the Ruby Console OR called it in another script it will still work...
            Similarly the menu item for Script2 might look something like this...

            
            if( not file_loaded?("Script2.rb") )
                UI.menu("Plugins").add_item("Make Red") {makeRed}
            end
            file_loaded("Script2.rb")
            
            

            You can leave this menu as it is, BUT if you don't want it on the menu put a # in front of the UI.menu... line. The function 'makeRed' will be loaded anyway. If you typed at in the Ruby Console OR called it in another script it will still work...

            Now, to make a Ruby that runs both - make a short script... called 'myScript.rb'...

            
            ### Suggest that you add some notes here so that you remember what it does etc...
            require "sketchup.rb" 
            require "Script1.rb"
            require "Script2.rb"
            def myScript
              selectByLayer()### this runs first - the "()" are optional IF it doesn't need an input.
              makeRed()### then this runs on the selection...
            end#def
            ### menu item
            if( not file_loaded?("myScript.rb") )
                UI.menu("Plugins").add_item("My Script"){myScript}
            end
            file_loaded("myScript.rb")
            ### the end
            
            

            Pick "My Script" off the Plugins menu and it should run the two scripts one after the other...
            Also typing myScript in the Ruby Console or calling it in yet anoother script would run it too...

            Hope that helps...

            TIG

            1 Reply Last reply Reply Quote 0
            • E Offline
              ezt
              last edited by

              Tig,

              That's superb.

              Your very clear instructions allowed me to complete the script and it all works superbly.

              One point though, you mentioned using the Ruby Console. I have a problem with that. Every time I try to enter a script it returns errors. For example, if I run myscript.rb I get the error

              @unknownuser said:

              myscript.rb
              Error: #<NameError: (eval):149: undefined local variable or method `myscript' for main:Object>
              (eval):149

              I also get it with other scripts which also work when accessed from the plugins menu.

              Is there something I need to set somewhere for the console to work?

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                To run a script use its 'def' function name e.g. type myScript (Note that a def must start with a lowercase letter)

                Don't add the script suffix .rb or any 's or "s. Also remember the def might not have the same name as the script - a script can define several functions in one go.

                Usually you give the defs obvious names like 'selectByLayer', 'hidden2Layer' etc...

                The only time you need to type the full script name is if it's new or you have changed it and want to reload it (that's why there's usually that trap in the menu code section to ensure there's only one menu item loaded - otherwise every reload would add another menu item !). To load a new or changed script you type:

                load "myScript.rb"

                Then its menu item is unchanged but the updated functionality is available.

                IF you have some scripts that are useful, BUT are not usually needed in the menus most of the time then you can add a .txt suffix to the file name, and then load it via the console just when you need it, by typing:

                load "myOtherScript.txt" (or "myOtherScript.rb.txt" if you prefer to keep its ruby-ness in the name).

                Scripts that end in ".rb" (and some 'scrambled' ones like .rbs) and that are in the Plugins folder load automatically at startup, others don't. You can load a scipt from anywhere by giving it's full path e.g. load "C:/MyScripts/Ruby/myScript.rb", but usually keeping them in Plugins seems the most logical thing to do...

                TIG

                1 Reply Last reply Reply Quote 0
                • E Offline
                  ezt
                  last edited by

                  Tig - you are a star.

                  I could not figure out why one or two scripts ran in the console and others did not.
                  it seems that by coincidence the 'def name' was the same as the script name for the couple that worked.

                  Now I understand it from your explanation, it all makes sense.

                  I have a couple of other questions, but I will put them in new topics rather than take up more of your time here (although if your posting history is anything to go by you will probably chip in there too πŸ˜‰ )

                  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