Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fold triangular corners
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
You can’t perform that action at this time.