Export images: how to?
-
Hmm, can you insert one, explode it and then get its material? I really do not know if that is possible. Or perhaps can you get the entities from an image, and find the material used and export it?
Is it not listed in the textures in the model?
-
@didier bur said:
Hi,
Is there a way (like the texturewriter for materials) to export images that are used in a model as images (not as materials or textures) ?Yea.. well the API says..
TextureWriter#load
http://code.google.com/apis/sketchup/docs/ourdoc/texturewriter.html#load
1st arg entity A face, image, component instance, group, or layer to load... and TextureWriter#write
http://code.google.com/apis/sketchup/docs/ourdoc/texturewriter.html#write
1st arg entity A face, image, component instance, group, or layer to write.
You will need to generate sequential filenames and append them to a pathstring of your choice, probably from the index of the images array:images=[] Sketchup.active_model.definitions.each {|e| images.push(e) if e.class==(Sketchup;;Image) } path='C;//some/path/to/folder' ext='png' # or 'tiff' or 'jpg' etc. tw=Sketchup;;TextureWriter.new images.each_with_index {|img,i| tw.load(img) tw.write(img,File.join(path,"%s%04d.%s" % ['img',i,ext])) } tw=nil if tw.count==0
The above assumes the TextureWriter removes them from the tw object as they are written out. (I've never used this class so... test.)
[as always wrap in a method, inside a module, inside your toplevel module namespace]EDIT: if you want your filenames to begin 'img0001.png' then change
i
toi+1
in thetw.write
line. -
That assumes the source file for the Image is present at the computer the script is being run at.
You can iterate all the Image definitions as Dan demonstrated, but I'd then dig into the entities of the Image's definition (they are components after all) and find the Face. Then you can extract it with the TextureWriter and be 100% sure it'll work.
The material you obtain is not in the list in SketchUp's material browser, nor do you get it listed if you do a
.each
onmodel.materials
. But you can get them all if you iterate by index, becausemodel.material.count
returns the number of normal materialand
image materials.Here's a snipped from a WIP plugin that let the user set teh opacity of Images:
# Context menu UI.add_context_menu_handler { |context_menu| model = Sketchup.active_model sel = model.selection if sel.single_object? && sel.first.is_a?(Sketchup;;Image) menu = context_menu.add_submenu('Opacity') (0..100).step(10) { |i| menu.add_item("#{i}%") { self.image_opacity(i) } } menu.add_separator menu.add_item('Custom') { result = UI.inputbox( ['Percent Opacity; '], [self.get_image_opacity], 'Set Image Opacity' ) if result opacity = result[0] opacity = 0 if opacity < 0 opacity = 100 if opacity > 100 self.image_opacity(opacity) end } end } # Modifies the selected Image's transparancy def self.image_opacity(opacity = 0) image = Sketchup.active_model.selection.first definition = get_definition(image) face = definition.entities.select{|e|e.is_a?(Sketchup;;Face)}.first face.material.alpha = opacity / 100.0 end def self.get_image_opacity image = Sketchup.active_model.selection.first definition = get_definition(image) face = definition.entities.select{|e|e.is_a?(Sketchup;;Face)}.first face.material.alpha * 100.0 end
-
Here my simple script that extracts all of the model's images' image-files into a special folder [Extracted Images] in the model's folder, or if the model is unsaved in the user's home directory.
To use typeextractimages
in the Ruby Console...extractimages.rb -
Hi,
Thank you all for resolving my problem. With a mix of all ideas and code, I'll do it ! -
Yessssssssss,
Here is my snippet to export images out of a model, assumed these images are NOT used as textures or materials, and if the original images are NOT present at their respective pathes on the disk:
def exportEmbeddedImages() @model=Sketchup.active_model @images={} @model.definitions.each { |e| @images[e]=e.name if e.image? } #@tw=Sketchup;;TextureWriter.new seems buggy because when write_all is called, # got the error "<wrong argument type (expected Sketchup;;TextureWriter)>" @tw= Sketchup.create_texture_writer @images.each_key do |idef| # find the corresponding face in each definition of image idef.entities.each { |e| @f=e if e.typename=="Face"} #retrieve texture of face and load it in texturewriter if @f #puts @f,@f.material.texture.filename @tw.load(@f,true) end end # set the path as needed @tw.write_all("c;\\images",false) end
Thank you all for your help !
-
@unknownuser said:
> #@tw=Sketchup;;TextureWriter.new seems buggy because when write_all is called, > # got the error "<wrong argument type (expected Sketchup;;TextureWriter)>" > @tw= Sketchup.create_texture_writer >
Sketchup::TextureWriter.new
is not even documented. All examples from Google useSketchup.create_texture_writer
Sidenote:
if e.typename=="Face"
That is slow -because it compares strings.if e.is_a?(Sketchup::Face)
This is much faster. -
@thomthom said:
@didier bur said:
@tw=Sketchup::TextureWriter.new
seems buggy because when@tw.write_all
is called, got the error
<wrong argument type (expected Sketchup::TextureWriter)>
Sketchup::TextureWriter.new
is not even documented. All examples from Google useSketchup.create_texture_writer
Sorry.. noticed that (it was late, too tired to go looking for the constructor.)
After fiddling around I think that.new
is undefined, and calling it just callsClass.new
, and certain things in the instance do not get initialized. -
@didier bur said:
Here is my snippet to export images out of a model ...
a couple of questions:
(1) why?
@images={}
and not@images=[]
(why use a Hash when an Array should do?)
You'd also need to change@images.each_key
to@images.each
(2) when you use
tw.write_all
,- how does Sketchup name the files for those images that had no name?* what format does Sketchup write the images in?
(the original format or all TIIF?)
- how does Sketchup name the files for those images that had no name?* what format does Sketchup write the images in?
-
@dan rathbun said:
@didier bur said:
Here is my snippet to export images out of a model ...
a couple of questions:
(1) why?@images={}
and not@images=[]
(why use a Hash when an Array should do?)
You'd also need to change@images.each_key
to@images.each
(2) when you usetw.write_all
,- how does Sketchup name the files for those images that had no name?* what format does Sketchup write the images in?
(the original format or all TIIF?)
I agree that an array
[]
and.each
would be the best solution.The
texturewriter
names the images from their textures' image-path-name - even if it no longer exists as a file - ALL textures will have an image-path. Thetw
also adjusts the exported file's name, making spaces into underscores and removing all other punctuation - e.f.C:\images\my+image 123-4.jpg
becomesmyimage_1234.jpg
: should this result in two exported images with the same name the later ones have a numerical suffix added so../my+image 123-4.jpg
and../my-image 123-4.jpg
would export asmyimage_1234.jpg
andmyimage_12341.jpg
etc. The exported images take the format of the original, deduced from the name's suffix - jpg/png/tif etc....... - how does Sketchup name the files for those images that had no name?* what format does Sketchup write the images in?
-
@tig said:
I agree that an array
[]
and.each
would be the best solution.I assumed it was part of a larger piece of code since it used instance variables.
-
@didier bur said:
@Dan: - I didn't even know that an image could have no name...
Well TIG says it always has an image.path
.. I was going by what you said in the 1st post. You said your image.path=nil ??@TIG: Great answers! I asked because the API is vague. Your info should go into the API TextureWriter.write_all description, most definately.
-
@thomthom: correct, I need the names for another purpose, along with the associated definition. (su2pov)
@Dan: - I didn't even know that an image could have no name... How can this occur (each image has a path (with a file name at the end) image.path=nil occured only one time with an old model and I cannot reproduce this, so I think I made an error whan typing code in the console or something like that...
- tw exports at the original format.
Advertisement