Skip to content
Snippets Groups Projects
Commit 0fff60f5 authored by Michael Williamson's avatar Michael Williamson
Browse files

Re-ported texface--> material.

Now this supports multiple UV assgned images (creating a material for each) and will also use existing materials and textures rather than creating new all teh time.
parent b35a3cfb
No related branches found
No related tags found
No related merge requests found
...@@ -403,47 +403,92 @@ def assign_mat(matname="Default"): ...@@ -403,47 +403,92 @@ def assign_mat(matname="Default"):
if editmode: if editmode:
bpy.ops.object.mode_set(mode = 'EDIT') bpy.ops.object.mode_set(mode = 'EDIT')
def texface_to_mat():
def check_texture(img,mat):
#finds a texture from an image
#makes a texture if needed
#adds it to the material if it isn't there already
try:
tex = bpy.data.textures[img.name]
except:
tex = bpy.data.textures.new(name=img.name)
finally:
tex.type = 'IMAGE'
tex = tex.recast_type()
tex.image = img
#see if the material already uses this tex
#add it if needed
found = False
for m in mat.texture_slots:
if m and m.texture == tex:
found = True
break
if not found and mat:
mat.add_texture(tex, texture_coordinates='UV', map_to='COLOR')
def texface_to_mat():
# editmode check here!
editmode = False
ob = bpy.context.object
if ob.mode =='EDIT':
editmode = True
bpy.ops.object.mode_set()
for ob in bpy.context.selected_editable_objects: for ob in bpy.context.selected_editable_objects:
img = 0
m = 0 faceindex = []
#get Image from active_uv_texture unique_images = []
if (ob.data.uv_textures
and ob.data.active_uv_texture.data[0].image):
img = ob.data.active_uv_texture.data[0].image
#if not look in other uv_textures, take first one
else:
for uv_tex in ob.data.uv_textures:
if uv_tex.data[0].image:
img = uv_tex.data[0].image
break
#get material to assign image to if object has an image in uv_textures # get the texface images and store indices
if img: if (ob.data.uv_textures):
#get active material for f in ob.data.active_uv_texture.data:
if ob.active_material: if f.image:
m = ob.active_material img = f.image
#if not look if there are others, take first one #build list of unique images
if not m and ob.material_slots: if img not in unique_images:
ms = ob.material_slots.values() unique_images.append(img)
for matslot in ms: faceindex.append(unique_images.index(img))
if matslot.material:
m = matslot.material else:
break img = None
faceindex.append(None)
#check materials for images exist; create if needed
matlist = []
for i in unique_images:
if i:
print(i.name)
try:
m = bpy.data.materials[i.name]
except:
m = bpy.data.materials.new(name = i.name)
continue
finally:
matlist.append(m.name)
# add textures if needed
check_texture(i,m)
#set up the object material slots
assignmatslots(ob, matlist)
#if ob has no material but we have an image create a material #set texface indices to material slot indices..
if not m and img: me = ob.data
m = bpy.data.materials.new(name=img.name)
ob.data.add_material(m) i = 0
for f in faceindex:
#if image is there create texture and apply to material if f != None:
if img: me.faces[i].material_index = f
tex = bpy.data.textures.new(name=img.name) i += 1
tex.type = 'IMAGE' if editmode:
tex = tex.recast_type() bpy.ops.object.mode_set(mode = 'EDIT')
tex.image = img
m.add_texture(tex, texture_coordinates='UV', map_to='COLOR')
#operator classes: #operator classes:
#--------------------------------------------------------------------- #---------------------------------------------------------------------
...@@ -458,8 +503,12 @@ class VIEW3D_OT_texface_to_material(bpy.types.Operator): ...@@ -458,8 +503,12 @@ class VIEW3D_OT_texface_to_material(bpy.types.Operator):
return context.active_object != None return context.active_object != None
def execute(self, context): def execute(self, context):
texface_to_mat() if context.selected_editable_objects:
return {'FINISHED'} texface_to_mat()
return {'FINISHED'}
else:
self.report({'WARNING'}, "No editable selected objects, could not finish")
return {'CANCELLED'}
class VIEW3D_OT_assign_material(bpy.types.Operator): class VIEW3D_OT_assign_material(bpy.types.Operator):
'''assign a material to the selection''' '''assign a material to the selection'''
...@@ -662,4 +711,4 @@ def unregister(): ...@@ -662,4 +711,4 @@ def unregister():
break break
if __name__ == "__main__": if __name__ == "__main__":
register() register()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment