sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Export images: how to?

    Scheduled Pinned Locked Moved Developers' Forum
    14 Posts 5 Posters 728 Views 5 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      @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 to i+1 in the tw.write line.

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        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 on model.materials. But you can get them all if you iterate by index, because model.material.count returns the number of normal material and 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
        
        

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          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 type extractimages in the Ruby Console...extractimages.rb

          TIG

          1 Reply Last reply Reply Quote 0
          • Didier BurD Offline
            Didier Bur
            last edited by

            Hi,
            Thank you all for resolving my problem. With a mix of all ideas and code, I'll do it ! 😍

            DB

            1 Reply Last reply Reply Quote 0
            • Didier BurD Offline
              Didier Bur
              last edited by

              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 !

              DB

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @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 use Sketchup.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.

                Thomas Thomassen β€” SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  @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 use Sketchup.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 calls Class.new, and certain things in the instance do not get initialized.

                  I'm not here much anymore.

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    @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?)

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      @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 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?)

                      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. The tw 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 becomes myimage_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 as myimage_1234.jpg and myimage_12341.jpg etc. The exported images take the format of the original, deduced from the name's suffix - jpg/png/tif etc.......

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • thomthomT Offline
                        thomthom
                        last edited by

                        @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.

                        Thomas Thomassen β€” SketchUp Monkey & Coding addict
                        List of my plugins and link to the CookieWare fund

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @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.

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • Didier BurD Offline
                            Didier Bur
                            last edited by

                            @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.

                            DB

                            1 Reply Last reply Reply Quote 0
                            • 1 / 1
                            • First post
                              Last post
                            Buy SketchPlus
                            Buy SUbD
                            Buy WrapR
                            Buy eBook
                            Buy Modelur
                            Buy Vertex Tools
                            Buy SketchCuisine
                            Buy FormFonts

                            Advertisement