I tried the following command in macOS terminal (I’m using macOS 15.1 and SketchUp 2024): /Applications/SketchUp.app/Contents/MacOS/SketchUp -RubyStartup ~/exporter.rb ~/test.skp
where the exporter.rb has only three lines, which exports the current model to glb and exit:
model = Sketchup.active_model
status = model.export('/Users/ZhouYucheng/Downloads/output.glb')
Sketchup.quit()
However, my problem is: when I run the above command in terminal, the ruby script will not run automatically (maybe because the Sketchup’s window is not focused automatically). So I have to manually click the Sketchup’s window to make it focused (to be the frontmost window), and then the ruby exporter script will be executed.
How to address this issue?
I just want to use command line to let SketchUp export a glb file from a skp file. I think there should be a robust way to automate it.
P.S. I searched about SketchUp C API (SDK). But it seems that I cannot use API to directly export a ready-made format (ref: How convert skp to obj in the c sdk - #2 by DanRathbun).
P.S. I noticed that one can use AppleScript like osascript -e 'activate application "SketchUp"' to focus a window. But this solution is not robust. Actually, I want my command to run even I locked the screen (so it can be configured as a server).
I believe that on Mac before the actual command line arguments you need one that says --args. Another UNIX convention (like the ~ in backup file names instead of skb).
BTW please correct your forum profile. There is no Free Plan of SketchUp 2024. Are you using a cracked version?
Thank you for your reply!
Using open command with --args works! I also found the trailing “/Contents/MacOS/SketchUp” is not necessary in open, and I can just using open -n -a /Applications/SketchUp.app ~/test.skp --args -RubyStartup ~/exporter.rb. This command will make the opened window focused.
As for your question. Do you mean the app bundle “SketchUp.app” should be like “SketchUp 2024.app”? It’s not versioned on my mac. I forget the detail when I installing SketchUp, and maybe I renamed the app bundle name in /Application folder
However, my problem is not fully addressed. I want to config this command to run automatically in headless mode (e.g., if I locked the screen, like a LaunchDaemon). But I found the open command also doesn’t work in that case: the application it opened will not be auto focused (the opened window’s top-left red/yellow/green dot is gray), and thus the ruby script is not executed. Maybe it’s related to the macOS system.
So, is there any other way to access SketchUp 2024’s built-in export gltf method from command line?
I want this because the built-in export gltf method is added in SketchUp 2024 (I doesn’t found it in SketchUp 2023), and the third-party’s gltf export extension is not as good as the built-in’s.
SketchUp is a GUI application. It does not have a “headless mode.”
The C SDK can operate on model files in “headless mode”, however the exporters are not included in the C API. It is possible that perhaps you could use a 3rd party C library to export to glB format.
I just realized that maybe I could write my code fully in ruby and let it auto scan a folder and convert all skp files to glb. So I can open SketchUp app at the beginning and let the ruby script to run forever.
That is: use ruby API to open a skp file once it found, convert it to glb, and close the window.
I’m not familiar with ruby. I read some basic docs about the SketchUp Ruby API (Class: Layout::SketchUpModel — SketchUp Ruby API Documentation).
I found there is a open_file method (Module: Sketchup — SketchUp Ruby API Documentation), but I don’t found a close_file method?
Instead I found a quit method, but it is used to quit the app entirely. Can I just close one window? Or, can I use one window to load a model, convert it, and clear/empty it for re-using ?
The #close method is used to close this model. On Mac OS, only the active model can be closed. On Windows, since there can be only one document open, this method will perform a File/New operation.
That is a LayOut Ruby API. This is about an instance of a SketchUp Model that is inserted into a .layout file. Not that much to do with the exporters…
You may be interested in FredoBatch, which can process tasks over a set of files. In the video, below, I show how to export an open set of files to DXF/DXG.
In you case, you would create a simple Custom task, with the following Ruby code:
module MyModule
def MyModule.export_to_glb(skp_file, task_context)
#Path to the GLB file (same basename as the skp file, with glb extension)
file_glb = skp_file.sub(/skp\Z/i, 'glb')
#Export to GLB
Sketchup.active_model.export(file_glb , false)
#Log the information
task_context.log("Exported to GLB: <b><!blue>#{File.basename(file_glb)}<!></b>")
end
end #module MyModule
Note that MyModule is just for namespacing. You can choose the name you want. Likewise, you can puts this code in a rb file of your choice (below baba.rb). The task will look like:
To run the task over a set of files, just create one or several File Sets, with the choice of one or several directories and a wildcard filter (like *):
Oh! The Model#close API it what I really need now! I’ll try that, thank you for pointing this!
Yes, Sketchup.active_model.export(‘xxx.glb’) works on my SketchUp 2024 I also don’t find any document about it. I just give it a try and find it works. Maybe the Docs should be updated