How to run SketchUp extensions?

Hi there, I’m following Tutorial: How to Write a Short Script for Selecting SketchUp Outer Edges. (v0.0.3) to write a ruby file. Then I zipped it and renamed it into rbz file. I installed it from Extension Manager. There I can see other preinstalled extensions such as Sandbox Tools, etc. However, I cannot find anywhere to run these extensions. No menu or toolbar at all. I even tried to restart SketchUp.

If written properly the extension code will be loaded when SketchUp starts.

SketchUp extensions use the event driven programming pattern. Meaning that they do not do anything until the user causes some event to happen, and the extension code reacts. These events can be a simple as clicking a menu item, or a toolbar button.

Well, … did you create a menu item or a toolbar command, or a right-click context menu handler command ?

You would need to look at examples we’ve posted here in this category.

Pay heed to the class and methods of the UI module and the Sketchup::Menu class.

Okay… this tutorial example is not an extension example. It’s a snippet of code meant to run in the console by cut and paste. Or in one of the console+ editor extensions that has a “run” button.

To build a proper extension refer to the documentation for the SketchupExtension class.

… also see Trimble’s extension examples …

1 Like

I see. Thanks for your explains. My purpose is to draw some 3D objects with scripts running in background, which means I cannot copy & paste. What do you suggest, extension or console + editor? If the later, could you give me an example?

You will want to write extensions.
You will need to learn basic Ruby first.
Then learn the SketchUp API that enhances Ruby.

---> Ruby Learning Resources [WikiLists] - #10 by LE_GALL

Search this category for “example” or “code”. (Use the :mag: search icon top right.)

I also linked the Trimble tutorial examples above.

There are also examples you can download from the Extension Warehouse …

I heard somewhere that extensions are event driven. I guess that means it does need user input. For my purposes, the background process has to run periodically without human intervention, so what can be the event? Also, I tried some of your examples and they all need me to operate in the GUI. As I said I have to avoid using the GUI.
I’m more interested in the console + editor approach. Can I edit my scripts and then run it with a command line?
BTW, I had 2 years experiences in Ruby.

All you code must be inside a company / author namepsace module.
Each of your extensions need to be inside a submodule of your toplevel namespace module.

If the “event” will be you starting a process from the console, then define a class, module or singleton method that can be run from outside the modules using a qualified method call …

WenTao::SomePlugin.start

If for example your structure was …

module WenTao
  module SomePlugin

    extend self

    def periodic_task
      # do stuff 
    end

    def start
      @timer_id = UI.start_timer(15.0, true) do
        periodic_task() # gets called every 15 seconds
      end
    end

    def stop
      UI.stop_timer(@timer_id) if @timer_id 
    end

  end
end

:bulb:

Or you could create a menu item …

module WenTao
  module SomePlugin

    extend self

    @loaded = false if @loaded.nil?

    def periodic_task
      # do stuff 
    end

    def start
      @timer_id = UI.start_timer(15.0, true) do
        periodic_task() # gets called every 15 seconds
      end
    end

    def stop
      UI.stop_timer(@timer_id) if @timer_id 
    end

    if !@loaded
      submenu = UI.menu('Plugins').add_submenu('Nifty Process')
      submenu.add_item('Start Nifty Process') { start() }
      submenu.add_item('Stop Nifty Process') { stop() }
      @loaded = true
    end

  end
end

I spent some time to experiment as per what you said.
My first approach is to run my ruby script like
.../SketchUp -RubyStartup ".../drawBox.rb &
It started SketchUp GUI and then stopped. I have to open a file from the GUI before the drawBox.rb can run to draw a box. I want to avoid this manual step.
I’m trying to create an extension as what you said to see if manual steps can be avoided. However, I just found that without opening a file in SketchUp the extensions won’t load. Am I right? If yes, I have to keep a file open all the time while the extension periodically draw things on it, correct?

Your “drawbox.rb” needs to wait until the model object is valid.

One way to do this to use an AppObserver. The plugin submodule can itself be an AppObserver object if it has the needed callback methods.

During the startup cycle on Windows, a new empty model is created before the extensions load.
But on Mac, the empty model is not created until after the extensions load.

So you need a trigger to tell you when the model is valid and ready. That trigger might be the
AppObserver#expectsStartupModelNotifications callback, which should not be called until the model object is ready. But anyway, if this callback returns true, then the AppObserver#onNewModel callback will definitely be called when there is a valid and ready empty model object.

So again looking at the 2nd example above, let us add in AppObserver methods, and attach the module itself to the application as an observer instead of adding a manual menu item …

"WenTao_SomePlugin.rb"

module WenTao
  module SomePlugin

    extend self

    @loaded = false if @loaded.nil?

    def periodic_task(model)
      # do stuff 
    end

    def onNewModel(model)
      @timer_id = UI.start_timer(15.0, true) do
        periodic_task(model) # gets called every 15 seconds
      end
    end

    def expectsStartupModelNotifications
      return true # fire the onNewModel callback at end of load cycle 
    end

    if !@loaded
      # Attach this module itself as an AppObserver object during load cycle:
      Sketchup.add_observer(self)
      @loaded = true
    end

  end
end
1 Like

Hi, the example script in the tutorial is not yet a SketchUp Extension and can only be run using a Ruby Code Editor inside SketchUp.

I am planning to explain the process on how to take the script on the tutorial and writing a SketchUp Extension soon.

I’ll pm you when the new tutorial is ready.

Which tutorial is that?

I quoted the link to Rafael’s tutorial in post 3, above.