Skip to content

Instantly share code, notes, and snippets.

@sambler
Created July 25, 2017 13:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sambler/59068765005928e526c33e836f84474f to your computer and use it in GitHub Desktop.
Save sambler/59068765005928e526c33e836f84474f to your computer and use it in GitHub Desktop.
Simplify a mesh to be close to viewport preview drawing
# made for https://blender.stackexchange.com/q/86248/935
# duplicate a mesh to get the edges that define the shape
# skip any extra edges that are part of a flat section
import bpy
import bmesh
# source object
obj = bpy.context.object
# minimum angle between faces that will be kept.
tolerance = 0.05
src_mesh = bmesh.new()
src_mesh.from_mesh(obj.data)
flat_mesh = bmesh.new()
for e in src_mesh.edges:
if e.calc_face_angle(200.0) > tolerance:
v1 = flat_mesh.verts.new(e.verts[0].co)
v2 = flat_mesh.verts.new(e.verts[1].co)
flat_mesh.edges.new((v1,v2))
me = bpy.data.meshes.new('FlatMesh')
flat_mesh.to_mesh(me)
flat_mesh.free()
src_mesh.free()
obj = bpy.data.objects.new('FlatMesh', me)
bpy.context.scene.objects.link(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment