The following is a cross-post from my other site http://sketchupplugins.com. When you are done creating your new plugin, feel free to add it to the index on http://sketchupplugins.com. It’s free and easy.
Learn Plugin Developement
There are many tutorials, resources and forums available online that will help you get started. Use the following list as a springboard:
- Hello world!
- Distributing your plugin
- SketchUp API documentation
- SketchUp Ruby Cheatsheet
- Official SketchUp API Blog
- SketchUp Ruby Resources
- Ruby Code Snippets
- Ruby Documentation
You can also find great forums for discussion in the official Google Developer Forum or at SketchUcation.
The following concerns plugins written in Ruby (the programming language included with SketchUp’s free and Pro versions). It is a basic template to get you started. Also note that SketchUp has a C++ SDK (in case you intend to work with it from other software).
Although you can use any text editor to create a plugin, it might be a good idea to install my Ruby Code Editor plugin into SketchUp first. This allows you to experiment with Ruby scripting. You can then afterwards package your code in a plugin using the instructions below.
RBZ Plugin File Structure
The RBZ file format was introduced in Maintenance Release 2 of Version 8. Packaging a plugin this way allows a user to easily install it from SketchUp’s Preferences dialog.
To enable use of the RBZ-installer functionality, all files that make up a plugin must be packaged in one single compressed file (note that the RBZ file is simply a re-named ZIP file). The following is a minimal sample file structure. You can add as many other files and folders as necessary for your plugin. The entire contents of the compressed file will get copied into SketchUp’s Plugins folder on installation.
my_plugin.rbz (compressed file) | |-- my_plugin_loader.rb | |-- my_plugin (directory) | |-- my_plugin.rb
As a reference: SketchUp’s default Plugin installation folder can be found here (replace version number with current major version):
Windows: C:\Program Files\Google\Google SketchUp 8\Plugins\
Mac: /Library/Application Support/Google SketchUp 8/SketchUp/Plugins/
Plugin Template
It is a good idea to a) use SketchUp’s extension system and b) wrap your plugin in a Ruby Module. Below is some sample code to get you started. Replace “my” with your initials to keep things clean.
Contents of /my_plugin_loader.rb (in main directory)
require "sketchup.rb" require "extensions.rb" # Load plugin as extension (so that user can disable it) my_plugin_loader = SketchupExtension.new "My_Plugin Loader", "my_plugin/my_plugin.rb" my_plugin_loader.copyright= "Copyright 2011 by Me" my_plugin_loader.creator= "Me, myself and I" my_plugin_loader.version = "1.0" my_plugin_loader.description = "Description of plugin." Sketchup.register_extension my_plugin_loader, true
Contents of /my_plugin/my_plugin.rb (in subfolder)
=begin Copyright 2010, Author All Rights Reserved THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. License: AuthorsLicenseStatement Author: AuthorName Organization: AuthorAffiliationOrOrganizationIfAny Name: ScriptName Version: ScriptVersion SU Version: MinimumSketchUpVersion Date: Date Description: ScriptDescription Usage: ScriptUsageInstructions History: 1.000 YYYY-MM-DD Description of changes =end require "sketchup.rb" # Main code (start module name with capital letter) module My_module def self.my_method # do something... end def self.my_second_method # do something... end end # Create menu items unless file_loaded?(__FILE__) mymenu = UI.menu("Plugins").add_submenu("My Plugin Collection") mymenu.add_item("My Tool 1") {My_module::my_method} mymenu.add_item("My Tool 2") {My_module::my_second_method} file_loaded(__FILE__) end
Tip: If you prefer to not have your plugin’s source code visible, then you can use SketchUp’s Scrambler to encrypt Ruby files.