Advertisement
yelby

Yelby's Texture Grabber

Aug 3rd, 2021
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import bpy
  2. import bmesh
  3. import glob
  4. import shutil
  5. import os
  6. from pathlib import Path
  7.  
  8. def run():
  9.     #Check File
  10.     path = bpy.path.abspath("//")
  11.     path += "Textures"
  12.     isFile = os.path.isdir(path)
  13.     if not isFile:
  14.         os.makedirs(path)
  15.         print("Folder Created")
  16.     else:
  17.         print("File Already Exists")
  18.    
  19.     #Get the selected object
  20.     obj = bpy.context.object
  21.  
  22.     #Get materials
  23.     materials = obj.data.materials
  24.     texture_list = []
  25.     materialSlots = obj.material_slots
  26.  
  27.     for mat in materialSlots:
  28.         print("~~Material: " + mat.name)
  29.         if mat.material and mat.material.use_nodes:
  30.             for texture in mat.material.node_tree.nodes:
  31.                 if texture.type == 'TEX_IMAGE':
  32.                     texture_list += [texture.image]
  33.                     tex = bpy.data.images[texture.image.name]
  34.                     full_path = bpy.path.abspath(tex.filepath, library=tex.library)
  35.                     norm_path = os.path.normpath(full_path)
  36.                     #print("~Texture Name: " + tex.name)
  37.                     #print("FullPath: " + full_path)
  38.                     #print("NormPath: " + norm_path)
  39.                     newLocation = path + '\\' + texture.image.name
  40.                     #print("New Location: " + newLocation)
  41.                     if os.path.isfile(norm_path) and not os.path.isfile(newLocation):
  42.                        
  43.                         temp = os.path.splitext(texture.image.name)
  44.                         print("_Split_")
  45.                         print(temp)
  46.                         fixedName = str(cleanName(temp))
  47.                         print("Fixed Name: " + fixedName)
  48.                        
  49.                         print(mat.name + " | uses: " + texture.image.name + " Location: " + texture.image.filepath)
  50.                         #newLocation = path + '\\' + mat.name + " - " + fixedName
  51.                         #print(newLocation)
  52.                         #shutil.copy(norm_path, newLocation)
  53.                         #texture.image = bpy.data.images.load(newLocation)
  54.                     elif os.path.isfile(norm_path) and os.path.isfile(newLocation):
  55.                         print("Already Exists: " + texture.image.name)
  56.                     else:
  57.                         print("File Missing: " + texture.image.name)
  58.                     print("")
  59.        
  60.  
  61. def cleanName(fileName):
  62.     fileTypes = [".png",".jpg",".jpeg",".psd",".bmp",".gif",".dds",".tif",".tiff"]
  63.     for extention in fileTypes:
  64.         if fileName[1] == extention:
  65.             name = fileName[0] + fileName[1]
  66.             print("Cleaned Name: " + name)
  67.             return name
  68.     name = os.path.splitext(fileName[0])
  69.     cleanName(name)
  70.        
  71.  
  72. def print(data):
  73.     for window in bpy.context.window_manager.windows:
  74.         screen = window.screen
  75.         for area in screen.areas:
  76.             if area.type == 'CONSOLE':
  77.                 override = {'window': window, 'screen': screen, 'area': area}
  78.                 bpy.ops.console.scrollback_append(override, text=str(data), type="OUTPUT")
  79.  
  80. if __name__ == "__main__":
  81.     print("~~~START OF CODE~~~")
  82.     run()
  83.     print("~~~END OF CODE~~~")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement