Skip to content

Instantly share code, notes, and snippets.

@davidejones
Created February 27, 2014 17:31
Show Gist options
  • Save davidejones/9254895 to your computer and use it in GitHub Desktop.
Save davidejones/9254895 to your computer and use it in GitHub Desktop.
import math, os, time, bpy, random, mathutils, re, ctypes, struct, binascii, zlib, tempfile
from bpy import ops
from bpy.props import *
def GetMaterialTextures(Material):
if Material:
#Create a list of Textures that have type "IMAGE"
ImageTextures = [Material.texture_slots[TextureSlot].texture for TextureSlot in Material.texture_slots.keys() if Material.texture_slots[TextureSlot].texture.type == "IMAGE"]
#Refine a new list with only image textures that have a file source
ImageFiles = [os.path.basename(Texture.image.filepath) for Texture in ImageTextures if Texture.image.source == "FILE"]
if ImageFiles:
return ImageFiles
return None
def copyUV(name, dstme, srcme):
uvtex = dstme.uv_textures.new()
uvtex.name = name
#we are only using the first uv data we find [0]
x=0
for mtfl in srcme.uv_textures[0].data:
datum = uvtex.data[x]
datum.uv = mtfl.uv
x = x+1
def copyMaterial(srcmat,dobj,face):
dstme = dobj.data
newmat = bpy.data.materials.new(srcmat.name)
newmat.diffuse_color = srcmat.diffuse_color
newmat.alpha = srcmat.alpha
newmat.ambient = srcmat.ambient
newmat.darkness = srcmat.darkness
dstme.materials.append(newmat)
# set the material to this created one - active_material_index
dobj.active_material_index = len(dstme.materials)-1
#switch to edit mode
#bpy.ops.object.mode_set(mode='EDIT', toggle=False)
#Deselect All
#bpy.ops.mesh.select_all(action = 'DESELECT')
#switch to face select mode
#bpy.context.tool_settings.mesh_select_mode = [False, False, True]
#face.select = True
# Assign materials to faces
face.material_index = dobj.active_material_index
#Assign the material on all the vertices
#bpy.ops.object.material_slot_assign()
return newmat
def copyTextures(dstmat,srcmat,matnames):
#realpath = os.path.expanduser('./presnh.jpg')
#tex = bpy.data.textures.new('ColorTex', type = 'IMAGE')
#tex.image = bpy.data.images.load(realpath)
#tex.use_alpha = True
#get first tex slot of src material
#srctex = srcmat.texture_slots[srcmat.active_texture_index]
srctex = srcmat.active_texture
print(srctex)
#tex = bpy.data.textures.new('lightmap',type = 'IMAGE')
#tex.image = srctex.image
#tex.use_alpha = srctex.use_alpha
if srctex is not None:
print(dstmat.name)
#check if texture already copied
if dstmat.name in matnames:
print("texture already copied")
else:
mtex = dstmat.texture_slots.add()
mtex.texture = srctex
mtex.texture_coords = 'UV'
mtex.use_map_color_diffuse = True
matnames.append(dstmat.name)
print("------------Start-------------")
objs = [obj for obj in bpy.context.selected_objects if obj.type == 'MESH']
if len(objs) <= 1:
print("Sorry but you need to select 2 meshes to make this work...\n")
else:
srcobj = objs[1]
dstobj = objs[0]
srcmesh = objs[1].data
dstmesh = objs[0].data
srcmats = srcmesh.materials
dstmats = dstmesh.materials
dfaces = dstmesh.faces
matnames = []
#copy first uv mapping from source mesh
copyUV("CopiedUV",dstmesh,srcmesh)
x=0
for face in srcmesh.faces:
# if face doesn't have material then create material and copy textures to it
srcmat = srcmats[face.material_index]
#print(dstmats[dfaces[x].material_index])
#copyMaterial(srcmat,dstobj,dfaces[x])
if len(dstmats) <= 0:
print("There are no existing materials...\n")
mat = copyMaterial(srcmat,dstobj,dfaces[x])
else:
print("There are existing materials...\n")
#print(str(dfaces[x].material_index))
#if dfaces[x].material_index <= len(dstmats):
# mat = dstmats[dfaces[x].material_index]
#else:
# mat = copyMaterial(srcmat,dstobj,dfaces[x])
mat = dstmats[dfaces[x].material_index]
copyTextures(mat,srcmat,matnames)
x = x+1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment