Created
July 23, 2016 14:54
-
-
Save nutti/d38728a44c95432b54fdf54a7c2f4687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wahooney_copy_mirror_uvs.py Copyright (C) 2009-2010, Keith "Wahooney" Boshoff | |
# ***** BEGIN GPL LICENSE BLOCK ***** | |
# | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software Foundation, | |
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
# | |
# ***** END GPL LICENCE BLOCK ***** | |
bl_info = { | |
'name': 'Copy UVs from Mirror', | |
'author': 'Keith (Wahooney) Boshoff, Nutti', | |
'version': (0, 4, 0), | |
'blender': (2, 75, 0), | |
'location': 'UVs > Copy UVs from Mirror', | |
'url': 'http://www.wahooney.net/', | |
'description': 'Copies the mirror side UVs to the current uv face selection', | |
'category': 'UV'} | |
import bpy | |
from bpy.props import * | |
import bmesh | |
from mathutils import Vector | |
# function checks if two vectors are similar, within an error threshold | |
def vecSimilar(v1, v2, error): | |
return abs(v2.x - v1.x) < error and abs(v2.y - v1.y) < error and abs(v2.z - v1.z) < error | |
# copy uv coordinates from one uv face to another | |
def copyUVs(uv_layer, src, dst, axis, error): | |
for l in src.loops: | |
uv = l[uv_layer].uv.copy() | |
co = l.vert.co.copy() | |
for ll in dst.loops: | |
coo = ll.vert.co.copy() | |
if axis == 'X': | |
coo.x = -coo.x | |
elif axis == 'Y': | |
coo.y = -coo.y | |
elif axis == 'Z': | |
coo.z = -coo.z | |
if vecSimilar(co, coo, error): | |
ll[uv_layer].uv = uv.copy() | |
def get_face_center(face): | |
center = Vector((0.0, 0.0, 0.0)) | |
for v in face.verts: | |
center = center + v.co | |
return center / len(face.verts) | |
def main(context, operator): | |
obj = context.active_object | |
bm = bmesh.from_edit_mesh(obj.data) | |
error = operator.properties.error | |
axis = operator.properties.axis | |
bm.faces.ensure_lookup_table() | |
uv_layer = bm.loops.layers.uv.verify() | |
# cycle through the mesh faces | |
faces = [f for f in bm.faces if f.select] | |
for f_dst in faces: | |
count = len(f_dst.verts) | |
for f_src in bm.faces: | |
if f_src.index == f_dst.index: | |
continue | |
if count != len(f_src.verts): | |
continue | |
dst = get_face_center(f_dst) | |
src = get_face_center(f_src) | |
# test if the vertices x values are the same sign, continue if they are | |
if (dst.x > 0 and src.x > 0) or (dst.x < 0 and src.x < 0): | |
continue | |
# axis invert source | |
if axis == 'X': | |
src.x = -src.x | |
elif axis == 'Y': | |
src.y = -src.z | |
elif axis == 'Z': | |
src.z = -src.z | |
if vecSimilar(dst, src, error): | |
copyUVs(uv_layer, f_src, f_dst, axis, error) | |
bmesh.update_edit_mesh(obj.data) | |
class CopyMirrorUVs(bpy.types.Operator): | |
'''''' | |
bl_idname = "uv.copy_mirror_uvs" | |
bl_label = "Copy UVs from Mirror" | |
bl_options = {'REGISTER', 'UNDO'} | |
axis = EnumProperty(items=( | |
('X', "X", "Mirror Along X axis"), | |
('Y', "Y", "Mirror Along Y axis"), | |
('Z', "Z", "Mirror Along Z axis")), | |
name="Axis", | |
description="Mirror Axis", | |
default='X') | |
error = FloatProperty(name="Error1", description="Error threshold", | |
default=0.001, | |
min=0.0, | |
max=100.0, | |
soft_min=0.0, | |
soft_max=1.0) | |
@classmethod | |
def poll(cls, context): | |
obj = context.active_object | |
return (obj and obj.type == 'MESH') | |
def execute(self, context): | |
main(context, self) | |
return {'FINISHED'} | |
def menu_func(self, context): | |
self.layout.operator(CopyMirrorUVs.bl_idname) | |
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__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment