Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created December 23, 2017 12:11
Show Gist options
  • Save Santarh/155dbf822e64e540abd8ebbeadf72288 to your computer and use it in GitHub Desktop.
Save Santarh/155dbf822e64e540abd8ebbeadf72288 to your computer and use it in GitHub Desktop.
# This example assumes we have a mesh object in edit-mode
import bpy
import bmesh
from mathutils import Vector
# Get the active mesh
obj = bpy.context.edit_object
me = obj.data
# Get a BMesh representation
bm = bmesh.from_edit_mesh(me)
# Modify the BMesh, can do anything here...
faces = [f for f in bm.faces if f.select]
if len(faces) != 2:
raise Exception("Invalid selection")
verts = [v for v in bm.verts if v.select]
if len(verts) != 6:
raise Exception("Invalid selection")
central_verts = [v for v in verts if len([e for e in v.link_edges if e.select]) == 3]
if len(central_verts) != 2:
raise Exception("Invalid selection")
new_verts = {}
for v in central_verts:
edges = [e for e in v.link_edges if e.select]
pos = v.co.copy()
for e in edges:
for ev in e.verts:
if not (ev in central_verts):
pos += ev.co - v.co
new_v = bm.verts.new(pos)
new_verts[v] = new_v
for f in faces:
new_vs = [new_verts[v] if v in new_verts else v for v in f.verts]
bm.faces.new(new_vs)
bm.faces.remove(f)
remove_edges = []
for e in central_verts[0].link_edges:
if not e.is_valid:
continue
flag = False
if central_verts[1] in e.verts:
flag = True
if flag:
remove_edges.append(e)
for e in remove_edges:
bm.edges.remove(e)
for v in central_verts:
if not v.is_valid:
continue
if len([e for e in v.link_edges if not e.select]) == 0:
bm.verts.remove(v)
# Show the updates in the viewport
# and recalculate n-gon tessellation.
bmesh.update_edit_mesh(me, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment