Rename texture image file inside *.skp
-
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?
-
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.
-
@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
-
The API has a SketchUp 'texture writer'.
http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchup#create_texture_writer
Make a temporarygroup = 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!
-
May I query why you want to change the filename property of a Texture object?
-
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.
-
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
Advertisement