Ruby to open a sequence of files - run ruby
-
Is it possible to make a ruby that opens a sequence of files and then runs a ruby?
For example, I am in a sketchup file called bob_000001.skp. (Starting file)
Run ruby in this file
It opens bob_000002.skp and runs a ruby. (all files in same directory)
Then it loops to the next file, opens bob_000003.skp and runs ruby, etc
This would continue until it runs out of files.
something like this (sorry I'm not proficient in ruby)require 'sketchup.rb'
model = Sketchup.active_model
title = model.titleopen title + 1
run ruby
repeatThanks!
TBG
-
Yes.
There are 'batch tool' methods that can do this.
Often used for batch exporting/converting, batch unit-changing etc...
I've made a few... you just need to change parts of the ruby to do what you want then decide if you save changes etc before opening the next one etc, depending on what want the batch process to do...
Search the Plugins at SCF and see what suits your needs best... -
Thanks! i will take a look. (now that I know it will work)
Edit
So I want to open a file like
open bob_0000001.skp
run a ruby
open bob_000002.skp
run a ruby
etcSince I am just learning ruby, this is what I have so far ( I will update as I learn)
require 'sketchup.rb'
model = Sketchup.active_model
model_filename = File.basename(model.path) #current open filename bob_000001.skp to startLoop
name = model_filename.slice!(/\d+/) #splits out 000001 as a string? need a number?#add a number - doesn't work but thats the goal
bob = name + 1run ruby.rb here # other ruby in plugin directory
#open next file i.e. bob_000002.skp
Sketchup.open_file "bob_" + bob ".Skp"
repeat until I run out of files...
goto loop -
would this code run a .rb file for every sketchup file in a directory?
Dir.glob('/path/to/dir/*.skp') do |rb_file|
do work on files ending in .skp in the desired directory
end
This is kinda what I would like but I can't seem to get it to work.
s
-
Dir.getwd()
returns the current working directoryDir.chdir( dirpath )
changes the working directory to the argument.[list]can be an absolute path, or a relative path from the current working directory.SketchUp usually starts with the User's home directory set as the WD. BUT DON'T count on that, ... any script can change it.
RULES:
(1) Don't assume the WD is what you need it to be.
(2) Save the current WD before your code changes it.
(3) Change it just before you need it.
(4) Restore the old working directory, when done. -
Thanks Dan, I appreciate the help.
One of the things I can't figure out is how to say "no to saving file" in ruby code.
I open a file, run a ruby, and when I want to go to the next file it asks "do you want to save your file".
How do you bypass this?
Thanks!
Stuart
-
@tallbridgeguy said:
I open a file, run a ruby, and when I want to go to the next file it asks "do you want to save your file". How do you bypass this?
Don't modify the model !
Well.. seriously this is true.
But you can save out the model to a temp file (in the working directory:
Sketchup.active_model.save("Temp_#{model.name}.skp")
Or if you wrap what you've done in a UNDO operation, then you can undo it.
Sketchup.undo()
Or to just return to an unmodified state:
Sketchup.undo() while Sketchup.active_model.modified?
-
Thanks Dan, this worked great!
Sketchup.undo() while Sketchup.active_model.modified?
Stuart
Advertisement