Running SketchUp API functions without SketchUp

Hi!

I have made a script that adds a dictionary and sets some attributes to a model; basicly tagging them for use in an extension I’m making. Now, my issue is that I have a lot of models, and I’d rather not open each model in sketchup, just to set this piece of metadata on the file. Is it possible to have ruby use the sketchup::model.set_attribute function inside a ruby script?

My hope is to just run a script that finds all models in my model-folder, and tags them using the set_attribute-function and some logic, which will be a lot faster than opening all of the models one by one and running a function to set the attribute. Does anyone know of a way to do this?

It can’t be done in Ruby without opening the SketchUp model.
But you can write a small RB file which will open each SKP file within a selected folder in turn, then use you code to set the model’s attributes as you want etc, and then finally save the SKP and close it.
Repeat the iteration until all of the SKP files are done…
Finally delete any SKB backup files that might get made in that folder…

Yes, but not with SketchUp’s Ruby API, which is only exposed by the SketchUp executable.

You’d have to use the SketchUp C API.

Either you’d …

  1. Write your utility in C and compile it as a standalone CLI utility

… or…

  1. Try using the Ruby Standard Fiddle library to have a Ruby script access your folder of SKP files and make the changes.

There are several topic threads in the SDK category on attribute dictionaries.

… and some in Issue Tracker comments …

I wrapped the SDK in a Ruby/FFI wrapper which can be used outside of SketchUp to open models, create dDictionaries, write attributes, and save models.

This is not a turn-key solution - you would need to know how to do a few things like download the source from github, create a .gem file from source, install the gem file, and edit the source to use your SketchUp installation’s shared libraries. Familiarity with the C SDK would also be helpful.

See the file attr-dict-write.rb in the examples folder for an example of writing attributes and saving the model.

You would need to provide your own shared library files (sketchupapi.dll) which will be located in the installation folder of your SketchUp installation. See this line for an example of how to use a shared library from a SketchUp installation.

1 Like

Thanks a lot for your answers! All 3 of the answers are super relevant for my project, so I’m gonna have to play around with my options to find out how to proceed now :slight_smile: Thanks again, this was super helpful!

There is another preferences DLL required.
Obtain the C API SDK from the developer portal:
https://developer.sketchup.com/en