Export images to saved file location with file name
-
hope it's making any 'copy/paste' sense
what i'm trying to do is:
1 - get a 'front' view, save the file into a folder called 'front' then export an image in the same folder ('front')
2 - get a 'top' view, save the file to a folder called 'top' then export an image in the same folder ('top').
and so on for any other view...
now the saving to a new folder for each view has been basically done by TIG, but i want to add export the relative image too and i can't seem to set the path to be the same as the new saved file.
am i too far off with this skillful code?
model = Sketchup.active_model model_filename = File.basename(model.path) # i think i'm find out the models path view = model.active_view view.write_image( File.join(model_filename),600,600,true )
thanks...
-
model.path
is the path to the model's SKP, OR '' if the models has never been saved.
model_filename = File.basename(model.path)
is the name of the SKP 'xxx.skp'
model_dirname = File.dirname(model.path)
is the name of the folder containing the SKP 'C:/...../myskpsfolder/'
The code I've given you previously shows how to make a new folder in thatnewfolder=Dir.mkdr(File.join(model_dirname, 'mynewfolder'))
You meed to specify the image-name too...
myimage=File.join(newfolder, 'myimagename.png') view.write_image( myimage, 600, 600, true )
-
thanks - i think i forgot to mention the image name has to be the same as the sketchup file just saved
that's why i was trying: model_filename = File.basename(model.path)
(i thought i was getting the name of the file in that particular folder)
-
To get the 'root' of the model name you can use
title = model.title
then to make the full image path...
myimage=File.join(newfolder, title+'.png')
-
ok TIG, thanks, i think i understand it - i shall look into it (won't take more than a few months)
-
this does produce an image but the name is empty - only '.png'
title = model.title
myimage=File.join(dir2, title+'.png')
view.write_image( myimage, 600, 600, true )anyway at least i know i'm on a better track now.
-
Did that happen in a new (unsaved) file? Then the model has no file name and the title is empty. You would then need to provide a fallback name like:
title = model.title title = "untitled" if title.empty?
Eventually we would hve to check if such a file already exists and iterate until the file name is unique. -
yes, it already has a name, this is what i'm running after making a directory and saving the file..
dir=Dir.mkdr(File.join(model_dirname, 'TEST')) model.save(File.join(dir, File.basename(skp))) model_filename = File.basename(model.path) imager=File.join(dir, model_filename+'.png') view.write_image( imager, 600, 600, true )
-
BUT model.title and model.path etc are empty [''] UNLESS it's been saved.
Using the model.save() does NOT change the current model, it just saves a copy of it as it is...
So suspect you haven't saved the base SKP.
therefore the save you are doing is '.png', and the folder is probably not set wither
Therefore you need to do something to establish a 'title' some other way...Tip, to see what is going on run with the Ruby console open and put a temporary '
p
' in front of each reference you are setting to see what you get - e.g.p title=model.title
should return"mymodelname"
NOT""
- which shows it's not been saved... -
can't i just save it with the name of the copy then?
-
If you know that the file is saved to
File.join(dir, File.basename(skp)))
,
you can also write the image toFile.join(dir, File.basename(skp, ".skp")+".png"))
File.basename
understands the second argument as an extension and strips it off; if the extension was unknown, we could obtain it withFile.extname
("a_model.skp")
or a regular expression. -
friends, let me say.. you are not from this planet...! (..but i'm very glad you landed!)
-
I recommend a minor adjustment
File.join(dir, File.basename(skp, **".*"**)+".png"))
- it is safer, because a file ending with '.SKP' would fail == not matching ".skp"...
-
ok, TIG, i'll see what i can do.
this is what i'm getting towards: (and of course TIG has already done the biggest part)
i'll call it a batcher:
starts and asks for some options:
views to make = top, front, side, hi angle
delete all materials from model entities - on/off
shadows - on/off
fov value
layer name (to put the file contents into it)when it starts, it opens a folder to choose and processes all files in it.
process does:
delete all guides,
purge all unused entities,
create a layer (named from previous options) and move everything in it,
set a custom style
go to the first view (ie. top) and save the model to a subfolder (top),
save a transparent png image of the same view and place it in the same subfolder (top),then moves to the next file...
-
I have run into an issue when I'm working on the same image on two different computers - when exporting the Sketchup image, the Width & Height are locked (when one is edited, the other automatically matches that computer's viewport ratio). I am trying to find a way to export an image with independently filled in Width and Height resolutions. This seems to be the closest Ruby I could find to fixing my issue. Is there any way to modify this to get what I'm looking for? I export so often that I would like to create a button on a new toolbar to keep at the top of the screen, instead of entering the Ruby Console every time. Is there any way to do this?
Advertisement