• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Require 'sketchup'

Scheduled Pinned Locked Moved Developers' Forum
24 Posts 5 Posters 1.5k Views 5 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.
  • H Offline
    honoluludesktop
    last edited by 7 Dec 2010, 07:43

    When is require 'sketchup' required at the top of a plugin?

    1 Reply Last reply Reply Quote 0
    • T Offline
      thomthom
      last edited by 7 Dec 2010, 08:29

      Always include it. If your plugin should be the first one to load it would need it. But since you can't be certain of the load order, always include it.

      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
        TIG Moderator
        last edited by 7 Dec 2010, 09:47

        It will load 'sketchup.rb' IF it's not already loaded.
        This is pretty unlikely... BUT it's a safety net just in case you script is loading very early...
        So always add it at the start of your code.

        TIG

        1 Reply Last reply Reply Quote 0
        • H Offline
          honoluludesktop
          last edited by 7 Dec 2010, 12:27

          Thanks, but what is its function?

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by 7 Dec 2010, 12:31

            You can look at the sketchup.rb file - it is in the Tools folder.

            Hi

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 7 Dec 2010, 16:16

              The true answer is that the require 'sketchup.rb' statement is only needed IF and only if your script will be calling any of the methods defined in the Tools/sketchup.rb file.

              If your script does not call any of the methods defined in sketchup.rb, then your script does not need to have the require statement.

              I disagree with the "always have the require 'sketchup.rb' statement in every ruby script" rule.

              I think it makes more sense to have a master control script that begins with an exclamation point, named such as "!autoload.rb" in the Plugins folder, so as to make it the very first script to load.

              Then in that master script you decide what gets loaded first (second, third, etc..) by listing require statements.

              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.)

              If you look at the Tools/dynamiccomponents.rb file, you'll see the first 3 lines of code are:
              require 'sketchup.rb' require 'extensions.rb' require 'LangHandler.rb'

              SO.. by having a master script load controller script in your plugins folder, that has as it's FIRST statement
              require 'dynamiccomponents.rb'
              as the first line, will always ensure that

              1. the sketchup.rb methods and $loaded_files array1. the SketchupExtension class1. the LanguageHandler class1. and the various Dynamic Components classes are always loaded and loaded in that order.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by 7 Dec 2010, 18:26

                @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-requires dynamiccomponents.rb.

                Right?

                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.

                Hi

                1 Reply Last reply Reply Quote 0
                • T Offline
                  thomthom
                  last edited by 7 Dec 2010, 19:08

                  @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.

                  Ditto.

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

                  1 Reply Last reply Reply Quote 0
                  • H Offline
                    honoluludesktop
                    last edited by 7 Dec 2010, 21:00

                    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?

                    Is is also correct to assume that other sketchup .rb's may require it, and that they would call it?

                    1 Reply Last reply Reply Quote 0
                    • T Offline
                      TIG Moderator
                      last edited by 7 Dec 2010, 21:32

                      A require 'sketchup.rb' does no harm.
                      Using if when you don't need to might delay SUp's startup by a nanosecond... ❓

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • H Offline
                        honoluludesktop
                        last edited by 7 Dec 2010, 21:41

                        OK, just curious.

                        1 Reply Last reply Reply Quote 0
                        • T Offline
                          thomthom
                          last edited by 7 Dec 2010, 21:49

                          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.

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

                          1 Reply Last reply Reply Quote 0
                          • H Offline
                            honoluludesktop
                            last edited by 7 Dec 2010, 22:20

                            Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_method without the "drive\path" from any of my plugins?

                            1 Reply Last reply Reply Quote 0
                            • T Offline
                              TIG Moderator
                              last edited by 8 Dec 2010, 07:47

                              @honoluludesktop said:

                              Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_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) 
                              ###
                              
                              

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • D Offline
                                Dan Rathbun
                                last edited by 8 Dec 2010, 10:39

                                @honoluludesktop said:

                                Does that mean that I can add require "drive\path\my_folder\my_method" to the top of my code, and call my_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.

                                I'm not here much anymore.

                                1 Reply Last reply Reply Quote 0
                                • D Offline
                                  Dan Rathbun
                                  last edited by 8 Dec 2010, 10:58

                                  @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

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • T Offline
                                    thomthom
                                    last edited by 8 Dec 2010, 11:18

                                    @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.

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

                                    1 Reply Last reply Reply Quote 0
                                    • H Offline
                                      honoluludesktop
                                      last edited by 8 Dec 2010, 12:50

                                      Thanks All, for the help.

                                      1 Reply Last reply Reply Quote 0
                                      • D Offline
                                        Dan Rathbun
                                        last edited by 8 Dec 2010, 14:02

                                        @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-requires dynamiccomponents.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.

                                        I'm not here much anymore.

                                        1 Reply Last reply Reply Quote 0
                                        • D Offline
                                          Dan Rathbun
                                          last edited by 10 Dec 2010, 21:09

                                          @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=()`

                                          .

                                          I'm not here much anymore.

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

                                          Advertisement