Require 'sketchup'
-
A require 'sketchup.rb' does no harm.
Using if when you don't need to might delay SUp's startup by a nanosecond... -
OK, just curious.
-
require
is like load, with the difference that it only loads the file if it has not been loaded before. It ensures the file is loaded only once. -
Does that mean that I can add
require "drive\path\my_folder\my_method"
to the top of my code, and callmy_method
without the "drive\path" from any of my plugins? -
@honoluludesktop said:
Does that mean that I can add
require "drive\path\my_folder\my_method"
to the top of my code, and callmy_method
without the "drive\path" from any of my plugins?Yes [but with 'forward-slashes'=/] BUT you could just
load "drive/path/my_folder/my_method"
if it's only needed once - or even load it from a separate 'loading ruby' in Plugins... The 'require' will 'load' it if it isn't already loaded, whereas a 'load' will re-load it even if it is already loaded...
You can even set Ruby to load all of the files in you folder at startup by changing the $LOAD_PATH array.
I use this code below to allow my students to load their own Plugins at uni where they don't have access rights to the C:../Plugins folder on the system. Adjust the 'path' to suit your folder needs...### This file goes in MAIN ..Sketchup../Plugins/ folder - named ### "ZZ_LoadPathUpdater.rb" so that it loads near [or at] the end. ### This code sets the 'path' to the User's "Sketchup_Plugins" folder on the H; drive. path = "H;/Sketchup_Plugins" ### ### This next code extends the Ruby search paths to include 'path' ### therefore any subsequent use of "load 'xxx.rb'" or "load 'xxx.txt'" ### commands in the Ruby Console etc will also search/load from the 'path' ### in addition to the more usual paths like ../Tools/ and ../Plugins/ $LOAD_PATH << path; $LOAD_PATH.uniq! ### ### If the User has a folder that matches this 'path' then ### any Ruby scripts in 'path' will auto-loaded into Sketchup... Dir.entries(path).each{|file| load(File.join(path, file)) if File.extname(file)==".rb" or File.extname(file)==".rbs"} if File.exist?(path) ###
-
@honoluludesktop said:
Does that mean that I can add
require "drive\path\my_folder\my_method"
to the top of my code, and callmy_method
without the "drive\path" from any of my plugins?Just to clarify.. load and require both open and read FILES not methods.
Your file could have the same name as the method within it, or the file could contain a module with many methods.
-
@honoluludesktop said:
I looked up sketchup.rb. Is it correct to assume that none of that stuff is covered in the api, or required by the methods therein? If so, and if I do not call any of the sketchup.rb methods, should I still "require sketchup.rb" in my script?
@tig said:
A require 'sketchup.rb' does no harm.
Using if when you don't need to might delay SUp's startup by a nanosecond...What TIG says is true.. BUT
There is no point, if your script does not "require" the services of a script to put a require 'suchandsuch.rb' statement at the top of your code.
There are ruby utilities the use the require statements to "map" out dependencies for scripts. If you insert bogus require statements, your really lying to Ruby and any person that reads your code. Your declaring a false dependency.
So in the end, YOU decide whether you wish to be a precise programmer or a sloppy one ... (but sloppy is OK for a quick and dirty test, or a script that isn't used that often, or one for your own use.)
@honoluludesktop said:
Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?
TRUE
-
@dan rathbun said:
@honoluludesktop said:
Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?
TRUE
But one can't rely on other scripts to do so. Your plugin can be the only one to be loaded - or even the first.
-
Thanks All, for the help.
-
@Jim and ThomThom: I am speaking of general use of Sketchup, not Coders doing Debugging or Testing of plugins, etc.
@jim said:
@dan rathbun said:
I also advise that the first extension loaded (and that it always be loaded,) is dynamiccomponents.rb (because there are miscellaneous extra API methods for various classes within the DC extension, that need to be loaded. In addition when the DC extension is not loaded, some of the AttributeDictionary methods no longer work.)
dynamiccomponents.rb
is always loaded, anyway - it's in the Tools folder. Requiring it first or again does nothing about these "miscellaneous extra API methods" and the "AttributeDictionary methods" (which are these, anyway?) because the file only registers DC's as a SketchupExtension. The DC loader, which defines the various DC classes, is not called if the user has DC's disabled, regardless if a script re-requiresdynamiccomponents.rb
.Right?
TRUE Which is why I suggested in parens "that they be always loaded".
(I could edit & clarify the first post.)@jim said:
Also, I question the practice of scripts sneakily loading DC's when I explicitly disabled them. Seems like it would cause greater confusion - especially if I'm working on the assumption that I have disabled DC's.
Perhaps the issue of DCs clouds the idea?
I never said anything about creating "scripts sneakily loading DCs".
I said (and I clarify,) that YOU (an experienced Ruby coder,) can create a master load order rb file, for YOUR OWN use, in which YOU control what scripts load and in what order they load, on YOUR OWN computer.
This would be in the normal use of Sketchup for modeling, not when testing or debugging plugins.
But forget about DCs for a moment, .. pretend I never said anything about DCs. Instead imagine the !autoload.rb file as having it's first line be:
require 'sketchup'... the idea is the same.
-
@jim said:
Requiring it first or again does nothing about these "miscellaneous extra API methods" and the "AttributeDictionary methods" (which are these, anyway?) ....
See this API topic post for info on AttributeDictionary class methods that need the DC extension.
Extended API Methods defined by the DC Extension
` ::Geom::Transformation.rotx()
::Geom::Transformation.roty()
::Geom::Transformation.rotz()
::Geom::Transformation.xscale()
::Geom::Transformation.yscale()
::Geom::Transformation.zscale()::Sketchup::ComponentInstance.copy()
::Sketchup::ComponentInstance.description()::Sketchup::Drawingelement.last_scaling_factors()
::Sketchup::Drawingelement.local_transformation()
::Sketchup::Drawingelement.scaled_size()
::Sketchup::Drawingelement.set_last_size( lenx,leny,lenz )
::Sketchup::Drawingelement.unscaled_size()::Sketchup::Model.delete_attribute( dictionary_name, key )
::Sketchup::Model.entityID()
::Sketchup::Model.layer()
::Sketchup::Model.typename()::UI::WebDialog.last_height()
::UI::WebDialog.last_height=()
::UI::WebDialog.last_width()
::UI::WebDialog.last_width=()`.
-
... why didn't they just make this part of the API?
-
@thomthom said:
... why didn't they just make this part of the API?
A good question for Scott and Tyler.
My guess is, it was faster for development for Scott to write the extensions in Ruby to "prove the concept." They may be thinking to "bake them into" the C side API code at a later date.
Likely this is low priority, which means it is always (and probably will remain,) at the bottom of the to do list. Bug fixes to the application code come first.
-
I think if you wished to have the DC extension OFF, but still have the extended methods defined, you could issue the following call at the Console:
Sketchup::require( 'DynamicComponents/ruby/dcutils' )
-
You could do this automatically by adding the above statement to the end of the Tools/dynamiccomponents.rb file (after the statement that registers the DC extension.)
IF the extension is OFF then the extra class methods would be defined.
IF the extension is ON, the file may get reloaded, but no harm should be done.
Advertisement