New to Ruby - I want to write a script to...
-
I am a complete Ruby novice, my only experience of coding is writing some fairly simple routines in AutoLisp.
I want to write a script that will process all .skp files in a selected folder, opening each in turn, setting 'Face the Camera' and 'Shadows Face Sun' both to 'True', close and save the model (ignoring any warnings about changing the Sketchup version). It needs to work with OS X 10.8.
I am not even sure if this can be done with Ruby, as I have only encountered scripts which work within the current model, not access the OS file system etc.
Any pointers gratefully accepted! Thanks
-
(1) My 1st advice is to modify one of the templates into YOUR custom template, that has the settings you describe above.
Then set THAT custom template as YOUR default template.
(2) Read the Terms of Use regarding using (or forbidding use of,) SketchUp in an "automated manner".
DO not bother asking anyone on the Trimble SketchUp Team for legal clarification... they are not allowed to legally interpret their OWN Terms of Use. (Keeps the corporate lawyers employed I suppose.)
-
Any ideas on why they would not want any automation Dan?
-
@dan rathbun said:
(2) Read the Terms of Use regarding using (or forbidding use of,) SketchUp in an "automated manner".
You may not use the Trimble Software in any manner that could damage, disable, overburden, or impair Trimble's services (e.g., you may not use the Trimble Software in an automated manner), nor may you use Software in any manner that could interfere with any other party's use and enjoyment of Trimble Services.
my take on this is as long as your 'offline' you are not overburdening, Trimble's services in any way, in which case you can use 'Automator.app' to sequentially open and perform actions on your files.
If Trimble think Apple should not be allowing us to do this, then their lawyers can fight it out...
john
-
Dan - thanks for the response but I don't see how it would help me. I have a large number of components (actually a purchased library of entourage images as .skp files) which are not set up to Face-Me. Each time I insert one for the first time I have to set these two properties. I then save the component back to the file so I don't need to do it for that file again, but there are a hell of a lot yet to be updated...
I hadn't expected any issues with T&C though!
-
Oh and BTW... batch mode processing is easier on Windows as the same document window is reused, so that their is only ever ONE model file open, at a time.
Mac SketchUp will open a new document window for EVERY file in the folder, because there is no way via the API to close model windows.
The API does NOT have a built-in interface to simulate user keystrokes (in order to dismiss modal dialogs, etc.)
You would have to use a 3rd-party utility. Some use Applescript on Mac.
-
@driven said:
@dan rathbun said:
(2) Read the Terms of Use regarding using (or forbidding use of,) SketchUp in an "automated manner".
my take on this is as long as your 'offline' you are not overburdening, Trimble's services in any way, in which case you can use 'Automator.app' to sequentially open and perform actions on your files.
Yes that also makes sense to me.. but I have also had to deal with lawyers in the past, and they come from a galaxy far far away... and do not think like us mere mortals.
YES I DO know how to do it. BUT I am wary of distributing such a utility (which I have already written to about 90% done.)
-
I could do it Windows (via Parallels). Or perhaps process the components within a single model:
Load component from file
Set attributes
Save component back to file
Delete component instance
Purge component
NextI'll get reading the Newbie guides... Thanks again.
-
hi Keith,
the first bits relatively easy. if you paste this into 'Ruby Console' and hit 'return' it will open and mod all the .skp's found. Personally I find 'shadows_face_sun' a bit odd looking...# add your folder your path replaces this loadFolder = "/your/path" skpFiles = Dir[File.join( loadFolder, "*.skp" )] #open the files skpFiles.each {|f| Sketchup.open_file f; # do your ruby actions Sketchup.active_model.selection.to_a.find_all{|e|e.class==Sketchup;;ComponentInstance}.map{|i|i.definition}.each{|d|d.behavior.always_face_camera=true; d.behavior.shadows_face_sun = true} }
once there all done, you can either use Cmd "S" to save them, and if in the correct format Cmd "W" to close...
alternatively, you could run this Applescript which will do the save and click any drop-downs that appear.
for some reason it stalls on some windows, but if you click the error box and then 'Run' again it carries on.
Run in Applescript Editor after all the skps are open...activate application "SketchUp" tell application "System Events" tell process "SketchUp" set winCount to count of every window repeat with n from 1 to winCount if description of window n is "standard window" then set thisWindow to window n try if exists (click menu item "Save" of menu "File" of menu bar 1) then end if end try delay 0.5 try if exists (click button "Save" of sheet 1 of thisWindow) then end if end try delay 0.5 try if exists (click button "Replace" of sheet 1 of sheet 1 of thisWindow) then end if end try delay 0.5 try if exists (click menu item "Close" of menu "File" of menu bar 1) then end if end try end if end repeat end tell end tell
-
Driven - Thanks so much for taking the time to write that up for me.
Unfortunately I don't think this approach will work as it seems that it opens all the files in the folder in one go? I have hundreds of files to process (although I could put them into a working folder in batches I suppose). I am going to try modifying your code to get the array of *.skp files in the folder, then sequentially load each one as a component, modify it, save it back to the folder, delete and purge the instance, and move on to the next file. It seems like that will avoid the issue of not being able to close the current model via Ruby, and will only open one component at a time.
cheers Keith
-
This code appears to work on a test folder of 10 .skp files. Haven't let it loose on the main folder of 500!
model = Sketchup.active_model # set folder and get array of filenames loadFolder = "/Users/Keith/Illustration Library/Testfolder" skpFiles = Dir[File.join(loadFolder, "*.skp")] # Returns a DefinitionList definitions = model.definitions # iterate through files skpFiles.each do |compFile| compDefinition = definitions.load compFile behavior = compDefinition.behavior behavior.always_face_camera = true behavior.shadows_face_sun = true compDefinition.save_as compFile definitions.purge_unused end
no error handling of course so it's a bit 'quick and dirty'.
-
@unknownuser said:
This code appears to work on a test folder of 10 .skp files. Haven't let it loose on the main folder of 500!
no error handling of course so it's a bit 'quick and dirty'.
Yes make backups first.
And when it comes to file work trapping errors is best.
I would think that the user would be prompted to overwrite the old skp file.
You don't see a confirm MB on the Mac ?
-
If you make a subfolder then you can save the reformatted SKPs into there and leave the original SKPs alone...
That way you never change the original SKPs' data and don't need prompts etc... -
As the code stands it runs without any prompts at all. It processed 10 files in the folder in an instant. For my purposes I'd rather manually copy files to be processed into the folder and run the script unattended, knowing I had my own backups. It's only for me to use so I c an leave it rough and ready... Might be nice to select the folder via the file open dialog, other than that I think it serves my needs
Thanks to all for the help and encouragement.
-
@unknownuser said:
Might be nice to select the folder via the file open dialog, ...
folder = UI.openpanel("Choose a skp file to set folder","*.skp") if folder && folder = File.dirname(folder) # do your stuff end
-
@unknownuser said:
Thanks to all for the help and encouragement.
no problem Keith,
we need to encourage mac users who are prepared to dip into coding, we're in a minority,
if nothing else, there are always PC authored scripts that need a bit of ironing before they're fully mac compatible.@dan wrote a good 'chunking' script for me to process a 3D text sampler that may be useful/adaptable for handling a 500 file array. I'll see if I can find the link.
FOUND: http://sketchucation.com/forums/viewtopic.php?f=180&t=48761&p=444342&hilit=3d+text+chunk#p444342
johnPS, are you in London UK, or one of the impostors, I'm in East Dulwich?
-
Ah - I was wondering what might happen if I loaded a really huge folder - actually I have a couple of thousand file to process. Not all at once though... I will check out that link when I have more time, at first glance looks a bit scary for a novice!
Yes, I really am in London UK (live in W14, work SE11)!
-
Thanks Dan for the pointer to UI.openpanel, that all works fine.
Not sure if it is because I need to learn about chunking my array, but my routine stops every time after processing 33 or 34 files (it is always after 33 or 34 files) and then SU8 crashes. Given that such a small number of files are processed before the hang/crash, I am not sure the array size is the problem? When the script stops running, I am left with the last loaded component in the component list. If I try and and check its 'Face-Me' status, SU crashes.
def setcompfm model = Sketchup.active_model # set folder and get array of filenames folder = UI.openpanel("Choose a skp file to set folder","*.skp") if folder && folder = File.dirname(folder) skpFiles = Dir[File.join(folder, "*.skp")] end length = skpFiles.length result = UI.messagebox "Process " + length.to_s + " files?", MB_YESNO if result == 6 # Yes # Returns a DefinitionList definitions = model.definitions # iterate through files begin skpFiles.each do |compFile| compDefinition = definitions.load compFile behavior = compDefinition.behavior behavior.always_face_camera = true behavior.shadows_face_sun = true compDefinition.save_as compFile definitions.purge_unused end #end do rescue puts compFile end #end begin end #end if end #end def
-
The folder check was meant to enclose everything... but I see you have a empty folder check (which probably cannot happen because an actual file must be selected. BUT...
change line 7 to:
return unless folder && folder = File.dirname(folder)
REMOVE the
end
(line 9)If you cancel the open panel, NOW your method exits (because the result of
UI.openpanel
will eval falsely.) -
change the name of THAT 33rd or 34th file to ".OFF" or whatever.
Then try again. Just eliminate the bad file scenario.
Advertisement