Call Ruby Script from File
-
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.
-
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.
-
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?
-
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.
-
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
-
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 againload "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 scriptdo_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 calledMyBox
you could include in itMyBox::type1(x,y,z)
which you call in another script asMyBox::type1(123,456,666)
- 'Mybox' could include many different ways of making boxes - type one is perhaps at the origin to xyz dims... -
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.
Advertisement