.rbz from .rb

I tried to create my own extension (hello.rb). In the same level I put my hello.rb and an empty folder called hello. Then I zipped them togheter as hello.zip, and I changed the extension to hello.rbz. But as soon I install hello.rbz it is executed. And everytime I open Sketchup, hello.rbz is executed again, it apears inside the administration extensions menu, but it doesn’t apper inside the extensions menu. What am I doing wrong?

Any extension that you install, or copy into the Plugins folder, will be loaded when SU starts up.

What does your “hello.rb” do when it is loaded?

Your hello folder isn’t doing anything. You’d get the same result if you just zipped hello.rb, change the .zip extension to .rbz, and either install it via the Window/Extension manager, or copy it into the Plugins folder.

And for a ‘proper’ extension, you need a specific format of file in the root of the Plugins folder called hello.rb, which registers the extension, then loads the ‘active’ part of the extension that does something, typically hello/hello.rb, or any other name you choose for the ruby file inside the folder.

If all your hello.rb does is print to the Ruby console, or pop up a messagebox saying “Hello World”, then it will do that as soon as it loads, which will happen every time SU starts. So it’s doing exactly what I would expect it to do.

I don’t know what you mean by ‘the administration extensions menu’. Possibly, the window you get in the Extension Manager, showing installed extensions?

It will appear in either no menu, or whatever menu (if any) that you programmed it to appear in.

Can you post the code inside hello.rb here? Precede it by a new starting with three backticks ``` followed by the word ruby (no space in between), then a newline and your code, then another new line with three more backticks.

And you need to read more about how to create an extension. Try downloading almost any recent extension from either the Extension Warehouse, or Sketchucation Plugin Store and study it.

There are also a number of tutorial references - I can’t remember where to find them, but @DanRathbun will know, and can maybe give you some links.

For creating extensions, please be sure to read and understand …

… and …


It is required that the extension registrar script and the extension subfolder have the names of your top level namespace, and underscore, and then the module name of the extension.
Example …

"BarcelonaGeordie_HelloWorld.rb"

… and the subfolder with the extension’s code files be …

"BarcelonaGeordie_HelloWorld"

All of your various extensions must be inside a unique namespace module.
Each of your extensions should be separated from one another within a submodule of their own.

Example registrar script that goes in the “Plugins” folder:

# encoding: UTF-8
# File: "BarcelonaGeordie_HelloWorld.rb"
# A SketchupExtension registrar file for: "Hello World"
#
module BarcelonaGeordie
  module HelloWorld

    EXTENSION = SketchupExtension.new(
      'Hello World', 'BarcelonaGeordie_HelloWorld/HelloWorld.rb'
    )
    EXTENSION.instance_eval {
      self.description= 'An example extension that says "Hello".'
      self.version=     '1.0.0'
      self.copyright=   "© #{Time.now.year}, "<<Module.nesting[1].name
      self.creator=     'Barcelona Geordie'

      Sketchup.register_extension(self, true)
    }

  end # extension submodule
end # top level namespace module
1 Like

Two points you might not have understood:

  • Zipping code into an rbz just puts it in a form the Extension Manager knows how to unpack and copy into the Plugins folder. No more, no less. Actually registering an Extension with SketchUp requires code statements such as @DanRathbun showed.

  • Unless it is wrapped in something such as a def…end or a {} block, all the code in your file is executed as the file loads, every time it loads. But top-level code does not stick around waiting to be executed again, it’s one and done. For example, SketchUp doesn’t create or add a menu item unless the code tells it to do so.

Thanks John. hello.rbz is doing what it has to do. The hello folder is empty, it was created because I read that is needed. Certainly, I got the same result when I just zip hello.rb, that’s why I thought I needed to add the folder. Where can I get information or an exemple of that file needed to register the extension?

Dan Rathbun gave you a complete example above.

And if you want to see one for a complete working extension, do what I suggested earlier - download almost any free extension from Extension Warehouse, or SketchUcation Plugin Store, and look at the code. There are hundreds of free and unencrypted examples to choose from.

The hard part is writing the extension itself, for which you need to learn Ruby, and be familiar with how SU works as well.

I’ve published several extensions myself, with help from other people, particular ThomThom who now works on the SU Team, and Steve Baumgartner, who has chipped in above, and is the lead author on several extensions we’ve worked on together. On another unpublished piece of Ruby code I wrote for internal use, Dan Rathbun and others on the forum helped me understand where my code was going wrong, and not doing what I expected.

So do your own homework, try to find example extensions that do something like what you have in mind, copy others’ code and modify it (if the licence allows you to do that), consider buying a general Ruby coding book (the classic is the Pickax book Programming Ruby - there’s a second edition targeting Ruby v1.9 and 2. It’s so called because the cover has a pickax on it), or search this forum for Dan’s list of useful Ruby resources.

If what you want to do involves user interaction with mouse as well as keyboard or ‘form filling’ on screen, you’ll need to create a Tool. Again, there are many free examples if you search for them on the internet.

Read the SketchUp Ruby API documentation to learn about how Ruby interacts specifically with SketchUp.

And when you are getting somewhere, but have got stuck, or something isn’t working the way you thought it should, ask a more specific question on the forum, choosing Developers Ruby as the categories before you post.

Thanks again John. My Ruby Code is working properly when I run it in my Ruby Console. I just wanted to transform it to store it in a menu, to use it without opening the Ruby Console. The way to do it seems to be by transforming it into .rbz. So I was wondering how to do it.

Using Dan’s example nothing changes except for the description, … in Window/Extension manager. I put the Sketchup extension and the folder in the same level with the same name, and inside the folder my Ruby Code. I zipped it and renamed it, but is running again during instalation and when I open SketchUp. Should I change my code too?

Incorrect. To create a menu item see …

UI::Command class
and UI::menu method
and Sketchup::Menu.add_item method

Thanks!!! Solved. As slbaumgartner said, the “file is executed as the file loads”, because every time you close SketchUp then plugins desapear from your menu, and when you open Sketchup again, all the plugins have to install themselves again.

According to that reasoning, I should change my hello.rb like this:

def hello_code
(...)
end

plugins_menu = UI.menu("Plugins")
plugins_submenu = plugins_menu.add_submenu("My plugins")
plugins_submenu.add_item("Hello") {hello_code}

And then, put the new hello.rb inside hello_plugin folder, to be zipped with hello_plugin (the DanRathbun registration code).

1 Like

Yes, except that ALL code must be inside an extension submodule, inside your unique toplevel module (as explained above.)

Because the code is inside a module, you will need to decide how to access the hello_code method.

If you just put …

  extend self

… inside your extension submodule then you can call it’s methods without qualification (as your example shows now.)


OR … you can define it as a module method like …

  def self.hello_code

… and then call it like …

plugins_submenu.add_item("Hello") { self.hello_code }