From 0fb53f6827649919d23abf6f99a0fa8238109646 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sat, 10 Oct 2020 17:00:31 +0800 Subject: [PATCH] add flatten uv. still have bug --- ballance_blender_plugin/__init__.py | 6 +- ballance_blender_plugin/add_floor.py | 2 +- ballance_blender_plugin/flatten_uv.py | 121 ++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 ballance_blender_plugin/flatten_uv.py diff --git a/ballance_blender_plugin/__init__.py b/ballance_blender_plugin/__init__.py index 9fe330f..a652741 100644 --- a/ballance_blender_plugin/__init__.py +++ b/ballance_blender_plugin/__init__.py @@ -36,7 +36,9 @@ if "bpy" in locals(): importlib.reload(add_elements) if "add_floor" in locals(): importlib.reload(add_floor) -from . import config, utils, bm_import_export, rail_uv, preferences, threedsmax_align, no_uv_checker, add_elements, add_floor + if "flatten_uv" in locals(): + importlib.reload(flatten_uv) +from . import config, utils, bm_import_export, rail_uv, preferences, threedsmax_align, no_uv_checker, add_elements, add_floor, flatten_uv # ============================================= menu system @@ -51,6 +53,7 @@ class BALLANCE_MT_ThreeDViewerMenu(bpy.types.Menu): layout.operator("ballance.super_align") layout.operator("ballance.rail_uv") layout.operator("ballance.no_uv_checker") + layout.operator("ballance.flatten_uv") class BALLANCE_MT_AddFloorMenu(bpy.types.Menu): """Add Ballance floor""" @@ -83,6 +86,7 @@ classes = ( rail_uv.BALLANCE_OT_rail_uv, threedsmax_align.BALLANCE_OT_super_align, no_uv_checker.BALLANCE_OT_no_uv_checker, + flatten_uv.BALLANCE_OT_flatten_uv, BALLANCE_MT_ThreeDViewerMenu, add_elements.BALLANCE_OT_add_elements, diff --git a/ballance_blender_plugin/add_floor.py b/ballance_blender_plugin/add_floor.py index 3877c6b..2e1b11e 100644 --- a/ballance_blender_plugin/add_floor.py +++ b/ballance_blender_plugin/add_floor.py @@ -447,10 +447,10 @@ def load_basic_floor(mesh, floor_type, rotation, height_multiplier, d1, d2, side mesh.vertices.add(len(vecList)) mesh.loops.add(len(faceList)) mesh.polygons.add(len(faceMatList)) + mesh.create_normals_split() if mesh.uv_layers.active is None: # if no uv, create it mesh.uv_layers.new(do_init=False) - mesh.create_normals_split() virtual_foreach_set(mesh.vertices, "co", global_offset_vec, vecList) virtual_foreach_set(mesh.loops, "vertex_index", global_offset_loops, faceList) diff --git a/ballance_blender_plugin/flatten_uv.py b/ballance_blender_plugin/flatten_uv.py new file mode 100644 index 0000000..752555a --- /dev/null +++ b/ballance_blender_plugin/flatten_uv.py @@ -0,0 +1,121 @@ +import bpy,mathutils +import bmesh +from . import utils + +class BALLANCE_OT_flatten_uv(bpy.types.Operator): + """Flatten selected face UV. Only works for convex face""" + bl_idname = "ballance.flatten_uv" + bl_label = "Flatten UV" + bl_options = {'UNDO'} + + reference_edge : bpy.props.IntProperty( + name="Reference_edge", + description="The references edge of UV. It will be placed in V axis.", + min=0, + soft_min=0, + soft_max=3, + default=0, + ) + + @classmethod + def poll(self, context): + obj = bpy.context.active_object + if obj == None: + return False + if obj.type != 'MESH': + return False + if obj.mode != 'EDIT': + return False + return True + + def invoke(self, context, event): + wm = context.window_manager + return wm.invoke_props_dialog(self) + + def execute(self, context): + no_processed_count = real_flatten_uv(bpy.context.active_object.data, self.reference_edge) + if no_processed_count != 0: + utils.ShowMessageBox(("{} faces may not be processed correctly because they have problem.".format(no_processed_count), ), "Warning", 'ERROR') + return {'FINISHED'} + + def draw(self, context): + layout = self.layout + layout.prop(self, "reference_edge") + +def real_flatten_uv(mesh, reference_edge): + no_processed_count = 0 + + if mesh.uv_layers.active is None: + # if no uv, create it + mesh.uv_layers.new(do_init=True) + uv_layer = mesh.uv_layers.active + + selectedFace = [] + bm = bmesh.from_edit_mesh(mesh) + for face, index in ((face, index) for index, face in enumerate(bm.faces)): + if face.select: + selectedFace.append(index) + + vecList=mesh.vertices[:] + for ind in selectedFace: + face = mesh.polygons[ind] + allPoint = face.loop_total + + if allPoint <= reference_edge: + no_processed_count+=1 + continue + + # get correct new corrdinate system + p1Relative = reference_edge + p2Relative = reference_edge + 1 + p3Relative = reference_edge + 2 + if p2Relative >= allPoint: + p2Relative -= allPoint + if p3Relative >= allPoint: + p3Relative -= allPoint + + p1=mathutils.Vector(tuple(vecList[mesh.loops[face.loop_start + p1Relative].vertex_index].co[x] for x in range(3))) + p2=mathutils.Vector(tuple(vecList[mesh.loops[face.loop_start + p2Relative].vertex_index].co[x] for x in range(3))) + p3=mathutils.Vector(tuple(vecList[mesh.loops[face.loop_start + p3Relative].vertex_index].co[x] for x in range(3))) + + new_y_axis = p2 - p1 + new_y_axis.normalize() + vec1 = p3 - p2 + vec1.normalize() + + new_z_axis = new_y_axis.cross(vec1) + new_z_axis.normalize() + new_x_axis = new_y_axis.cross(new_z_axis) + new_x_axis.normalize() + + # construct transition matrix + origin_base = mathutils.Matrix(( + (1.0, 0, 0), + (0, 1.0, 0), + (0, 0, 1.0) + )) + origin_base.invert() + new_base = mathutils.Matrix(( + (new_x_axis.x, new_y_axis.x, new_z_axis.x), + (new_x_axis.y, new_y_axis.y, new_z_axis.y), + (new_x_axis.z, new_y_axis.z, new_z_axis.z) + )) + transition_matrix = origin_base @ new_base + transition_matrix.invert() + + # process each face + for loop_index in range(face.loop_start, face.loop_start + face.loop_total): + pp = mathutils.Vector(tuple(vecList[mesh.loops[loop_index].vertex_index].co[x] for x in range(3))) + vec = pp-p1 + new_vec = transition_matrix @ vec + + uv_layer.data[0].uv = ( + (new_vec.x if new_vec.x >=0 else -new_vec.x) / 5, + (new_vec.y) / 5 + ) + + mesh.validate(clean_customdata=False) + mesh.update(calc_edges=False, calc_edges_loose=False) + + return no_processed_count +