Created
December 10, 2016 19:01
-
-
Save batFINGER/dec833554fd7682bcf6621760389ab7a to your computer and use it in GitHub Desktop.
Fold triangular corners
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
import bpy | |
import bmesh | |
from bpy.props import FloatProperty | |
from math import radians | |
from mathutils.geometry import intersect_point_line | |
from mathutils import Matrix | |
class BMeshTriangularFold(bpy.types.Operator): | |
"""Fold Triangle on edge""" | |
bl_idname = "mesh.triangle_fold" | |
bl_label = "Fold Triangle" | |
bl_options = {'REGISTER', 'UNDO'} | |
angle = FloatProperty(name="angle", | |
default=0.0, | |
subtype='ANGLE', | |
unit='ROTATION', | |
min=radians(-180), | |
step=30, | |
max=radians(180)) | |
dic = {} | |
@classmethod | |
def bmesh(cls, context): | |
obj = context.edit_object | |
if obj is None: | |
cls.dic.clear() | |
return None | |
if obj.mode == 'EDIT' and obj.type == 'MESH': | |
bm = cls.dic.setdefault(obj.name, bmesh.from_edit_mesh(obj.data)) | |
if bm.is_valid: | |
return bm | |
print("Dead bmesh") | |
bm.free() | |
cls.dic.clear() | |
return None | |
@classmethod | |
def poll(cls, context): | |
if context.active_operator: | |
if context.active_operator.bl_idname == "MESH_OT_triangle_fold": | |
return True | |
edit_object = context.edit_object | |
if edit_object is None: | |
return False | |
bm = cls.bmesh(context) | |
if bm is None: | |
return False | |
verts = [v.index for v in bm.verts | |
if v.select and | |
len(v.link_faces) == 1 and | |
len(v.link_faces[0].verts) == 3] | |
return len(verts) | |
def execute(self, context): | |
#bm = self.bmesh(context) | |
obj = context.edit_object | |
mesh = obj.data | |
bm = bmesh.from_edit_mesh(mesh) | |
verts = [v for v in bm.verts | |
if v.select and | |
len(v.link_faces) == 1 and | |
len(v.link_faces[0].verts) == 3] | |
for v in verts: | |
e0, e1 = [q for q in v.link_faces[0].verts if q != v] | |
# pivot point | |
pivot_point, pc = intersect_point_line(v.co, e0.co, e1.co) | |
radius = v.co - pivot_point | |
rotmat = Matrix.Rotation(self.angle, 3, (e1.co - e0.co)) | |
v.co = pivot_point + rotmat * radius | |
bmesh.update_edit_mesh(mesh) | |
return {'FINISHED'} | |
def register(): | |
bpy.utils.register_class(BMeshTriangularFold) | |
def unregister(): | |
bpy.utils.unregister_class(BMeshTriangularFold) | |
if __name__ == "__main__": | |
register() | |
# test call | |
bpy.ops.mesh.triangle_fold() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment