sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Rename texture image file inside *.skp

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 1.9k Views 4 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.
    • N Offline
      nick9111
      last edited by

      Is there a way to rename texture image file inside *.skp? For example I can get name of such file using

      Sketchup.active_model.materials[0].texture.filename
      

      and when the result is filename with no path how to rename it and if it is possible keep association with material which using it?

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        The texture is stored inside of the skp file (no matter whether you get a original path, most likely the path does not exist).

        Export it to the new filename (in a temporary folder), import it again to the material ( material.texture=), delete the temporary file.

        You need to restore the material sizes, and the material alpha and color shift and colorization is lost. When you decide to restore the material's color, it will always be used as "colorized" even if it original was only used as color shift.

        1 Reply Last reply Reply Quote 0
        • N Offline
          nick9111
          last edited by

          @aerilius said:

          Export it to the new filename (in a temporary folder), import it again to the material ( material.texture=), delete the temporary file.

          Thank you for a quick answer, but I always do this work manually for many times and wondering if there's a way to rename it just with ruby command/routine 😐

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

            The API has a SketchUp 'texture writer'.
            http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchup#create_texture_writer
            Make a temporary group = model.active_entities.add_group()
            Assign the textured material to it: group.material = material.
            You make/use a texture writer to export that texture image file into the temp-folder.
            You can specify the image's file name and type if you write individual files:
            e.g. path_to_png = File.join(temp_folder_path, material.display_name+".png").
            Then:
            tw = Sketchup.create_texture_writer tw.load(group)
            Write the file:
            tw.write(group, path_to_png)

            Remember the original texture size in case changing it looses data:
            w = material.texture.width h = material.texture.height

            Now reset the material.texture = path_to_png
            and its sizes:
            material.texture.size = [w, h]

            Then tidy up...
            Erase the temp file: File.delete(path_to_png)
            Delete the temp group: group.erase! - although GC should remove the zero geometry group anyway

            [Do this inside a model.start_operation...model.commit_operation block...]

            Done!

            TIG

            1 Reply Last reply Reply Quote 0
            • tt_suT Offline
              tt_su
              last edited by

              May I query why you want to change the filename property of a Texture object?

              1 Reply Last reply Reply Quote 0
              • N Offline
                nick9111
                last edited by

                Thanks TIG, I'll check out this method πŸ‘

                @tt_su said:

                May I query why you want to change the filename property of a Texture object?

                I am working with a 3rd party program which directly imports *.skp files. My skp files have many textures with non-English letters in their file names. Program can't see those textures.

                1 Reply Last reply Reply Quote 0
                • N Offline
                  nick9111
                  last edited by

                  So there is a ruby plugin I've done (based on TIG's post). It renames all texture files in model with a template.

                  
                  module VNV
                  
                  def VNV.rename_textures
                  UI.messagebox("All texture file names will be changed with a template - mat000")
                  model = Sketchup.active_model
                  
                  model.start_operation('Rename texture files', true)
                  tw = Sketchup.create_texture_writer
                  
                  
                  temp_path = File.expand_path( ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] )
                  temp_folder = File.join( temp_path, 'Sketchup_tmp_mtl' )
                  unless File.exist?( temp_folder )
                          Dir.mkdir( temp_folder )
                  end
                  
                  group = model.active_entities.add_group()
                  
                  Sketchup.active_model.materials.each_with_index{ |m,i|
                  if m.texture then
                  group.material = m
                  tw.load(group)
                  path_to_png = File.join(temp_folder, "mat#{'%03d' % i}"+".png") 
                  tw.write(group, path_to_png)
                  
                  w = m.texture.width
                  h = m.texture.height
                  
                  m.texture = path_to_png
                  m.texture.size = [w, h]
                  
                  File.delete(path_to_png)
                  end
                  }
                  
                  group.erase!
                  model.commit_operation
                  
                  end # def
                  end # module
                  
                  if (!file_loaded?(__FILE__))
                    menu = UI.menu("Plugins")
                    menu.add_item("Rename texture files") { VNV;;rename_textures }
                    # Let Ruby know we have loaded this file
                    file_loaded(__FILE__)
                  end
                  
                  

                  RenameTextures.rb

                  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