# derived from texturewrite.rb which was Copyright 2005, @Last Software, Inc. # this version created Nov 2006 by Render Plus Systems filename = File.basename(__FILE__) require 'sketchup.rb' #----------------------------------------------------------------------------- def exportTextures ss = Sketchup.active_model.selection # this next lines uses all entities # remove it if you want only selected items ss = Sketchup.active_model.entities # create the texture writer tw = Sketchup.create_texture_writer $tw = tw # remove any $ variables when you are done debugging # loop through the entities and process each ss.each do |ent| export_texture(tw, ent) end#loop # write all does not seem to work #tw.write_all(get_tempory_directory + "/texture_test", false) puts "TW Count=" + tw.length.to_s puts "done" end#def def export_selected_texture # extracts a texture from the selected entity model = Sketchup.active_model ss = Sketchup.active_model.selection # create the texture writer tw = Sketchup.create_texture_writer ss.each do |ent| material = nil texture = nil case ent.typename when "Face" material = ent.material material = ent.back_material if material == nil when "ComponentInstance" material = ent.material when "Group" material = ent.material when "Image" filename = ent.path fname = UI.savepanel("Texture file", "", filename) export_texture(tw, ent, fname) return(fname) end#case next if !material texture = material.texture next if !texture filename = texture.filename fname = UI.savepanel("Texture file", "", filename) export_texture(tw, ent, fname) return(fname) end#loop end#def # export the image for a single entity # If a group or component, call this again recursively # if fname is passed in, use it def export_texture(tw, ent, fname) case ent.typename when "Face" # this only creates a new texture if it needs it index1 = tw.load(ent, true) # front face index2 = tw.load(ent, false) # back face #trace("%s %d %d",ent.typename, index1, index2) if index1 > 0 || index2 > 0 if (index1 > 0) if (!fname) fname = sprintf(get_tempory_directory + "/texture_test/F%d.jpg",index1) end#if tw.write(ent, true, fname) end#if if (index2 > 0) if (!fname) fname = sprintf(get_tempory_directory + "/texture_test/F%d.jpg",index1) end#if tw.write(ent, false, fname) end#if # get UV infomation for the face uvHelp = ent.get_UVHelper(true, true, tw) # this gets the handle from a previous texture if it did not need to make a new one handle1= tw.handle(ent, true) if (handle1 > 0) puts "handle1= " + handle1.to_s # filename() does not seem to work # You will want to make your own array of file names for the textures puts "Front Face Path = " + tw.filename(handle1).to_s if handle1> -1 # $ variables are for debugging $h = handle1 $ent = ent ent.outer_loop.vertices.each do |vert| uvq = uvHelp.get_front_UVQ(vert.position) puts "u=" + uvq.x.to_s + " v=" + uvq.y.to_s end end#if handle2 = tw.handle(ent, false) if (handle2 > 0) puts "handle2 = " + handle2.to_s # filename() does not seem to work puts "Back Face Path = " + tw.filename(handle2).to_s if handle2 > -1 ent.outer_loop.vertices.each do |vert| uvq = uvHelp.get_back_UVQ(vert.position) puts "u=" + uvq.x.to_s + " v=" + uvq.y.to_s end end#if when "ComponentInstance" index = tw.load(ent) #trace("%s %d",ent.typename, index) if (index > 0) if (!fname) fname = sprintf(get_tempory_directory + "/texture_test/C%d.jpg",index1) end#if tw.write(ent, fname) end#if $ci = ent handle = tw.handle(ent) if (handle > 0) puts "ci handle = " + handle.to_s # filename() does not seem to work puts "CI path = " + tw.filename(handle).to_s if handle > -1 end#if # get entities in instance - recursively ent.definition.entities.each { |ent2| export_texture(tw, ent2) } when "Group" index = tw.load(ent) if (index > 0) if (!fname) fname = sprintf(get_tempory_directory + "/texture_test/G%d.jpg",index1) end#if tw.write(ent, fname) end#if handle = tw.handle(ent) if (handle > 0) puts "group handle = " + handle.to_s # filename() does not seem to work puts "Group path = " + tw.filename(handle).to_s if handle > -1 end#if # get entities in group recursively #trace("%s %d",ent.typename, index) ent.entities.each { |ent2| export_texture(tw, ent2) } when "Image" index = tw.load(ent) if (index > 0) if (!fname) fname = sprintf(get_tempory_directory + "/texture_test/I%d.jpg",index1) end#iftw.write(ent, fname) end#if #trace("%s %d",ent.typename, index) handle = tw.handle(ent) if (handle > 0) puts "image handle = " + handle.to_s # filename() does not seem to work puts "Image path = " + tw.filename(handle).to_s if handle > -1 end#if end end # Add some menu items to access this if (not file_loaded?(filename) ) plugins_menu = UI.menu("Plugins") plugins_menu.add_item("Export Textures") { exportTextures } plugins_menu.add_item("Export Selected Texture") { export_selected_texture } end #----------------------------------------------------------------------------- file_loaded(filename)