Export images: how to?
-
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