Well this depends. If these materials are something every one of your models will need, then usually is is better to add them to an empty model, and save that as a template. Then use this template when staring new models. It’s also handy to set up some standard scene pages (views) and styles for those scenes, to help in modeling and / or output.
(For example my template has a specific “Thumbnail” scene with it’s style, and a Print scene with it’s own black and white style, in addition to a “Work” scene that is reverse video, black background scene,m that I use for just modeling in monochrome mode. These 3 template scenes have their “Include in animation” flag unchecked.)
But if you really want to run a snippet like this “on demand” you could get Aerilius’ Toolbar Editor plugin and create a button and use your snippet for the button’s code.
If you’d rather do it simpler, with a Extensions menu item then you can do something as simple as …
module JonahHawk
module AddMyMaterials
extend self
def add_materials
path = 'D:/SketchupMaterial/*_d.png'
model = Sketchup.active_model
materials = model.materials
before = model.materials.size
puts "\nAdding Jonah Hawk Materials:"
Dir.glob(path) do |file|
bName = File.basename(file, "_d.png")
next if materials[bName]
puts " Adding material: #{bName}"
material = materials.add(bName)
material.texture = file
end
added = model.materials.size - before
msg = "Materials added: #{added}"
puts msg
UI.messagebox(msg,MB_OK|64)
return added
end
# Note: Undefined instance references evaluate as nil
if !@loaded
# Only add menu item once if reloading during development
UI.menu('Extensions').add_item("Add Jonah's Materials") {
add_materials()
}
@loaded = true
end
end # module AddMyMaterials
end # module JonahHawk
Drop the file in your user “Plugins” folder, or package it up as a valid extension.
See Class: SketchupExtension — SketchUp Ruby API Documentation for the explanation of a extension registrar file.
In the case of an extension, the extension code file(s) go in a subfolder of their own.
An alternative to a “proper” extension can be a subfolder of snippets (like above) which has a loader file that has a loop similar to your Dir.glob loop, but globs rb
files and passes them to the global require
method.
Notice, that in Ruby, it is not the name of the file that creates module or class names, as with Python. In Ruby we are free to create whatever and however many, different modules and or classes within the same file. Some coders do, as they prefer to edit all the code in one file. I myself do not. I will usually at least have each nested class in it’s own file.
Also, differing from Python, Ruby module and class definitions can span multiple files. Ruby’s module and class scripting blocks are actually read by the interpreter as “open for edit and create if needed”. (ie, behind the scenes, the interpreter takes the text of the block and passes it either to a constructor method, or an object evaluation method.)
This is how extension authors publish code in multiple files. Each one of an authors files for ALL of their extension must be inside their top level namespace module. Each author should then separate each of their plugins from one another using a submodule.
But it is convention (because SketchUp’s “Plugins” folder is a shared file space,) to at least qualify the name of the extension registrar script file using your top level author/company namespace module name, followed by an underscore, and then the plugin name (which is likely to be the nested plugin module name.)
So taking the posted example, the registrar script would be: "JonahHawk_AddMyMaterials.rb"
.
This registrar script would name the loader file (the one I posted above) which should be in a subfolder of the same name as the registrar script, ie subfolder: "JonahHawk_AddMyMaterials"
.
The actually name inside your plugin subfolders can be whatever convention you desire. I usually always have a file named “loader.rb”. Others have the loader named “main.rb” etc.
Lastly, you may see use of global variables in examples. This is unacceptable for any published extensions, and dangerous in any case as clashes could occur.