• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Call Ruby Script from File

Scheduled Pinned Locked Moved Newbie Forum
sketchup
7 Posts 5 Posters 2.4k 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.
  • T Offline
    TheDro
    last edited by 11 Mar 2011, 03:17

    Hey everyone! I'm new here on the forums but I've been playing around with sketchup for a solid month now. I'm just starting to look at writing ruby scripts to automate some processes and I've got a couple questions. I've been looking around the web for some information and I've just read through the book of ruby (quickly) and I still can't find a way to call a ruby script from a ruby script. What I mean is call a function (method) called drawcubes in drawcubes.rb from another script, let's say called cubes in the cubes.rb file which contains some header stuff such as adding "drawcubes" to the plugin menu. The reason I want to do this is probably more important and I might not be approaching the problem the right way.

    I'm basically just looking for a way of having the "drawcubes" option in the plugin menu and have it call the function which I'm working on, without having to restart Sketchup every time I make a change. I could just write the function in the webconsole (plugin) without adding the option to the plugin menu but the webconsole doesn't have the syntax highlighting and proper tabbing that my external editor has.

    Hope that's clear enough.

    1 Reply Last reply Reply Quote 0
    • C Offline
      Chris Fullmer
      last edited by 11 Mar 2011, 04:24

      SketchUp loads the plugin when SketchUp loads, and then it never checks the .rb file again to see if it changed. So you also need to type load "myscript.rb" into the concole to reload your script. Then the menu item will be current to the script file.

      There are some things to watch out for, SU already has your script loaded, so methods that got loaded, then deleted in the script, still exist in SketchUp's memmory until you restart sketchup. Some there are things that get loaded into memmory that you will need to restart SU to make sure you get the script completely functioning right in those cases.

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

      1 Reply Last reply Reply Quote 0
      • H Offline
        honoluludesktop
        last edited by 11 Mar 2011, 05:26

        Am I wrong in assuming that any script can call any procedure in another script? If all the scrips load upon starting SketchUp, why do you need the console to reload the script?

        1 Reply Last reply Reply Quote 0
        • C Offline
          Chris Fullmer
          last edited by 11 Mar 2011, 05:56

          Hmm, that is interesting. This is not my workflow, so I really don't know much about it, but that seems reasonable. Make a script that loads the script. Tthen you just add that script to the menu, put a shortcut key to it - F5 for example. Then everytime you press F5, it re-loads the script of your choosing.

          That might well work.

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

          1 Reply Last reply Reply Quote 0
          • T Offline
            tbd
            last edited by 11 Mar 2011, 08:18

            I use Sketchup Bridge and have a shortcut key in my editor that loads the code into Sketchup so I can test ideas quickly. There is a tutorial there on how to setup it with RDE (Ruby editor).

            I have a version that is cross platform (SU Bridge is Windows only now), but it needs some work to make it releasable

            SketchUp Ruby Consultant | Podium 1.x developer
            http://plugins.ro

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 11 Mar 2011, 09:35

              You can load a .rb/.rbs script within another script - load "myscript.rb".
              Any of these directly in the Plugins folder are auto-loaded at startup anyway, so this is only needed if your scripts are in subfolders or located elsewhere on your PC - e.g. load "myscripts/myscript.rb".
              IF a file has Ruby code in it BUT its name ends with .txt it doesn't auto-load, BUT again load "myscript.txt" will load it for you.

              Any method defined within a script can be used in another script, so if one script makes a method called do_my_box() then adding that into another script runs it too. Often you'll want to pass values across, so as long as the called script takes arguments you can, e.g. def do_my_box(x=1,y=1,z=1)...end [making a default 1" cube if no arguments are passed]... then in the other script do_my_box(1,2,3) makes the box to those dimensions.
              It's best to include your methods within your own class or module, so for example if you have a new class called MyBox you could include in it MyBox::type1(x,y,z) which you call in another script as MyBox::type1(123,456,666) - 'Mybox' could include many different ways of making boxes - type one is perhaps at the origin to xyz dims...

              TIG

              1 Reply Last reply Reply Quote 0
              • T Offline
                TheDro
                last edited by 11 Mar 2011, 13:25

                Thanks a lot guys. The load method is just what I was looking for. Here's what I've written in the first file and it works:

                require 'sketchup.rb'
                
                def drawc
                	load 'cubegrid2.rb'
                	include Cubegrid
                	drawgrid
                end
                
                UI.menu("Plugins").add_item("cubegrid") { drawc }
                
                
                

                Every time I call cubegrid from the Plugins menu, it reloads the cubegrid2.rb file and executes its main method, drawgrid, from the module Cubegrid. I realize that the function names aren't very descriptive and quite uninspired, but that's not important and the moment 😄.

                Again, thanks for the help.

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

                Advertisement