New to Ruby - I want to write a script to...
-
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.
-
Thanks for the correction - I see my mistake now. However, the corrected syntax doesn't fix the crash. The folder selection works as it did before and populates the array with all the .skp files in the folder (about 200 for testing purposes), and my message box pops up and correctly reports the number to be processed.
However it stops on the 33rd or 34th file no matter what that file is. Each time I run the script, I move the 33/34 successfully modified files out of the folder, so I am never processing the same files twice. I don't know what might be special about the 33/34 mark, but it bails at that point.
Some kind of limit appears to be reached? Memory? I've no clue!
-
not sure how to do it in ruby but in the osascript/applescript/bash I tend to use, if you don't add a distinct delay point, the process reaches some limit and errors. [That's what happening with the one I posted earlier]
maybe adding a
puts "done"
afterdefinitions.purge_unused
will allow a break before opening the next?total stab in the dark
john -
(1) The iterator block.. use curly braces instead of
do
...end
So the block can have access to external references, such asdefinitions
(2) Make sure the loading is done before proceeding.
(3) perhaps use undo operations ?
Try an iterator like:
i = 0 puts() # skpFiles.each { |compFile| ### model.operation( "Update '#{File.basename(compFile)}'", true ) ### compDefinition = definitions.load compFile begin if compDefinition.internal? behavior = compDefinition.behavior behavior.always_face_camera = true behavior.shadows_face_sun = true compDefinition.save_as compFile else putc(46) # period char redo end rescue => e ### model.abort_operation() ### puts() puts "ERROR saving component file;" puts " #{compFile}\n" puts e.message puts e.backtrace if $VERBOSE puts() else i += 1 if i >= 10 definitions.purge_unused i = 0 end ### model.commit_operation() ### end # each skp file } definitions.purge_unused if definitions.length > 0
-
Thanks again for your help and suggested code. As it stands it does not modify any components. I discovered this is because it seems that even when the component file is loaded successfully, compDefinition.internal? returns false. I can only imagine that this is actually testing whether the component was created within the model or loaded from a file - in which case we are getting the expected result. I commented it out and it runs but... sadly it still bombs out at around 33 components.
I really don't want to take up any more of anyone's time - this was just a little project for me to play with but it seems rather trickier than I could have forseen. But I thought you might like an update!
def setcompfm model = Sketchup.active_model # set folder and get array of filenames folder = UI.openpanel("Choose a skp file to set folder","*.skp") return unless folder && folder = File.dirname(folder) skpFiles = Dir[File.join(folder, "*.skp")] length = skpFiles.length result = UI.messagebox "Process " + length.to_s + " files?", MB_YESNO return unless result == 6 # Yes # Returns a DefinitionList definitions = model.definitions # iterate through files i = 0 puts() # skpFiles.each { |compFile| ### model.start_operation( "Update '#{File.basename(compFile)}'", true ) ### compDefinition = definitions.load compFile begin #if compDefinition.internal? behavior = compDefinition.behavior behavior.always_face_camera = true behavior.shadows_face_sun = true compDefinition.save_as compFile #else # putc(46) # period char # redo #end rescue => e ### model.abort_operation() ### puts() puts "ERROR saving component file;" puts " #{compFile}\n" puts e.message puts e.backtrace if $VERBOSE puts() else i += 1 if i >= 10 definitions.purge_unused i = 0 end ### model.commit_operation() ### end # each skp file } definitions.purge_unused if definitions.length > 0 end #end def
-
Hi Keith,
I'm sitting at home recovering from having my ear cut off [and put back on] on Friday.
So, glad for the distraction...
Anyhow, heres an Automator script that has a safe test action, so won't hurt any files...
I missed a filter function, in the first...- opens each .skp in a 'pre-selected' folder in SU v2013 [should use your version if not]
- opens 'Ruby Console' and 'types' some safe for testing code
Sketchup.send_action('viewIso:');Sketchup.send_action('viewZoomExtents:')
clicks return - 'Saves' the changes
- closes 'RC'
- closes the .skp
- loops to the next skp in folder... and repeats
You need to open SU first with an empty new drawing... this just saves hassle.
Pre-Sellect skps in a Folder.
Open the workflow in 'Automator.app' by double clicking after unzipping it.Click Run, watch the show [I like the way it types for me, rather than just inserting it en-masse]
If your not using v2013 you'll may need replace the SketchUp.app shown with the one on your path [I know it looks right but I did a trick to avoid using v8, so just swap it if you need to, run it once to see if it fails...]
Once your happy the safe version works, edit the applescript with your SU [a 'one-liner' is probably best]
try it on a subset, if it runs those, it will run hundreds as long as you adjust the timeout [in loop field] to cover it...
Let me know if it works
john
-
Hi John,
The ear sounds painful! I hope you are recuperating well.
Thanks for the workflow. I have got it to work, with SU loading a script automatically (which defines a function to set the current model behavior as I want it) and I call that function in console. It runs slowly compared to the all-Ruby solution - but at least it processes without crash. So I can send lots of files and leave it to run over lunch, or night!
Many thanks again,
Keith
Advertisement