diff options
Diffstat (limited to 'release/scripts/op/uv.py')
-rw-r--r-- | release/scripts/op/uv.py | 89 |
1 files changed, 48 insertions, 41 deletions
diff --git a/release/scripts/op/uv.py b/release/scripts/op/uv.py index d01ef070d87..83282924b5b 100644 --- a/release/scripts/op/uv.py +++ b/release/scripts/op/uv.py @@ -21,11 +21,12 @@ import bpy from bpy.props import * + def write_svg(fw, mesh, image_width, image_height, face_iter): # for making an XML compatible string from xml.sax.saxutils import escape from os.path import basename - + fw('<?xml version="1.0" standalone="no"?>\n') fw('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n') fw(' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n') @@ -113,7 +114,7 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): filepath = fw.__self__.name fw.__self__.close() - material_solids = [bpy.data.materials.new("uv_temp_solid") for i in range(len(mesh_source.materials))] + material_solids = [bpy.data.materials.new("uv_temp_solid") for i in range(max(1, len(mesh_source.materials)))] material_wire = bpy.data.materials.new("uv_temp_wire") scene = bpy.data.scenes.new("uv_temp") @@ -126,27 +127,37 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): for f in mesh_source.faces: tot_verts += len(f.vertices) + faces_source = mesh_source.faces + + # get unique UV's incase there are many overlapping which slow down filling. + face_hash_3 = set() + face_hash_4 = set() + for i, uv in face_iter: + material_index = faces_source[i].material_index + if len(uv) == 3: + face_hash_3.add((uv[0][0], uv[0][1], uv[1][0], uv[1][1], uv[2][0], uv[2][1], material_index)) + else: + face_hash_4.add((uv[0][0], uv[0][1], uv[1][0], uv[1][1], uv[2][0], uv[2][1], uv[3][0], uv[3][1], material_index)) + # now set the faces coords and locations # build mesh data mesh_new_vertices = [] mesh_new_materials = [] mesh_new_face_vertices = [] - - + current_vert = 0 - faces_source = mesh_source.faces - for i, uv in face_iter: - if len(uv) == 3: - mesh_new_vertices.extend([uv[0][0], uv[0][1], 0.0, uv[1][0], uv[1][1], 0.0, uv[2][0], uv[2][1], 0.0]) - mesh_new_face_vertices.extend([current_vert, current_vert + 1, current_vert + 2, 0]) - current_vert += 3 - else: - mesh_new_vertices.extend([uv[0][0], uv[0][1], 0.0, uv[1][0], uv[1][1], 0.0, uv[2][0], uv[2][1], 0.0, uv[3][0], uv[3][1], 0.0]) - mesh_new_face_vertices.extend([current_vert, current_vert + 1, current_vert + 2, current_vert + 3]) - current_vert += 4 - mesh_new_materials.append(faces_source[i].material_index) - + for face_data in face_hash_3: + mesh_new_vertices.extend([face_data[0], face_data[1], 0.0, face_data[2], face_data[3], 0.0, face_data[4], face_data[5], 0.0]) + mesh_new_face_vertices.extend([current_vert, current_vert + 1, current_vert + 2, 0]) + mesh_new_materials.append(face_data[6]) + current_vert += 3 + for face_data in face_hash_4: + mesh_new_vertices.extend([face_data[0], face_data[1], 0.0, face_data[2], face_data[3], 0.0, face_data[4], face_data[5], 0.0, face_data[6], face_data[7], 0.0]) + mesh_new_face_vertices.extend([current_vert, current_vert + 1, current_vert + 2, current_vert + 3]) + mesh_new_materials.append(face_data[8]) + current_vert += 4 + mesh.vertices.add(len(mesh_new_vertices) // 3) mesh.faces.add(len(mesh_new_face_vertices) // 4) @@ -155,7 +166,7 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): mesh.faces.foreach_set("material_index", mesh_new_materials) mesh.update(calc_edges=True) - + obj_solid = bpy.data.objects.new("uv_temp_solid", mesh) obj_wire = bpy.data.objects.new("uv_temp_wire", mesh) base_solid = scene.objects.link(obj_solid) @@ -165,11 +176,10 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): # place behind the wire obj_solid.location = 0, 0, -1 - + obj_wire.material_slots[0].link = 'OBJECT' obj_wire.material_slots[0].material = material_wire - - + # setup the camera cam = bpy.data.cameras.new("uv_temp") cam.type = 'ORTHO' @@ -181,7 +191,9 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): # setup materials for i, mat_solid in enumerate(material_solids): - mat_solid.diffuse_color = mesh_source.materials[i].diffuse_color + if mesh_source.materials and mesh_source.materials[i]: + mat_solid.diffuse_color = mesh_source.materials[i].diffuse_color + mat_solid.use_shadeless = True mat_solid.use_transparency = True mat_solid.alpha = 0.25 @@ -190,7 +202,6 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): material_wire.use_shadeless = True material_wire.diffuse_color = 0, 0, 0 - # scene render settings scene.render.use_raytrace = False scene.render.alpha_mode = 'STRAIGHT' @@ -203,22 +214,17 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): if image_width > image_height: scene.render.pixel_aspect_y = image_width / image_height elif image_width < image_height: - scene.render.pixel_aspect_x = image_height /image_width - + scene.render.pixel_aspect_x = image_height / image_width + scene.frame_start = 1 scene.frame_end = 1 - + scene.render.file_format = 'PNG' scene.render.filepath = filepath data_context = {"blend_data": bpy.context.blend_data, "scene": scene} - bpy.ops.render.render(data_context, animation=True) - - # stupid - import os - animpath = scene.render.frame_path(1) - os.rename(animpath, os.path.abspath(filepath)) - + bpy.ops.render.render(data_context, write_still=True) + # cleanup bpy.data.scenes.remove(scene) bpy.data.objects.remove(obj_cam) @@ -227,15 +233,14 @@ def write_png(fw, mesh_source, image_width, image_height, face_iter): bpy.data.cameras.remove(cam) bpy.data.meshes.remove(mesh) - + bpy.data.materials.remove(material_wire) for mat_solid in material_solids: bpy.data.materials.remove(mat_solid) - class ExportUVLayout(bpy.types.Operator): - '''Export the Mesh as SVG''' + """Export UV layout to file""" bl_idname = "uv.export_layout" bl_label = "Export UV Layout" @@ -251,16 +256,16 @@ class ExportUVLayout(bpy.types.Operator): name="Format", description="File format to export the UV layout to", default='PNG') - size = IntVectorProperty(size=2, default=(1024, 1024), min=8, max=32768) + size = IntVectorProperty(size=2, default=(1024, 1024), min=8, max=32768, description="Dimensions of the exported file") @classmethod def poll(cls, context): obj = context.active_object - return (obj and obj.type == 'MESH') + return (obj and obj.type == 'MESH' and obj.data.uv_textures) def _space_image(self, context): space_data = context.space_data - if type(space_data) == bpy.types.SpaceImageEditor: + if isinstance(space_data, bpy.types.SpaceImageEditor): return space_data else: return None @@ -319,7 +324,6 @@ class ExportUVLayout(bpy.types.Operator): if is_editmode: bpy.ops.object.mode_set(mode='OBJECT', toggle=False) - mesh = obj.data mode = self.mode @@ -351,11 +355,12 @@ class ExportUVLayout(bpy.types.Operator): else: return False - def invoke(self, context, event): + import os self.size = self._image_size(context) + self.filepath = os.path.splitext(bpy.data.filepath)[0] wm = context.window_manager - wm.add_fileselect(self) + wm.fileselect_add(self) return {'RUNNING_MODAL'} @@ -364,10 +369,12 @@ def menu_func(self, context): def register(): + bpy.utils.register_module(__name__) bpy.types.IMAGE_MT_uvs.append(menu_func) def unregister(): + bpy.utils.unregister_module(__name__) bpy.types.IMAGE_MT_uvs.remove(menu_func) if __name__ == "__main__": |