Skip to content

Commit

Permalink
Optimizing Circular measures (#15)
Browse files Browse the repository at this point in the history
* Adding bmesh optimization

* Bumping up version

* Cleaning up useless method
  • Loading branch information
UncleFirefox authored Mar 27, 2021
1 parent 2042a5c commit 663e2f5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Albert Rodriguez",
"description": "Tools to take measures for Avatar",
"blender": (2, 92, 0),
"version": (0, 5, 0),
"version": (0, 6, 0),
"location": "View3D > Toolshelf",
"warning": "This plugin is only compatible with Blender 2.92",
"category": "Add measures",
Expand Down
112 changes: 59 additions & 53 deletions addon/operator/measures_circular_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class MEASURES_CIRCULAR_OT(bpy.types.Operator):
def poll(cls, context):
# If you want to verify the conditions of your operator
# before it launches, put your code here
if context.object is None:
return False

return True

# Called after poll
Expand All @@ -41,6 +44,18 @@ def invoke(self, context, event):
self.height = 0
self.hit_point = None
self.total_length = 0

self.bm = bmesh.new()
self.bm.from_object(
context.object, # default to selected object
context.evaluated_depsgraph_get()
)
bmesh.ops.transform(
self.bm,
verts=self.bm.verts,
matrix=context.object.matrix_world
)

# Do some setup
self.draw_handle = bpy.types.SpaceView3D.draw_handler_add(
self.safe_draw_shader_2d, (context,), 'WINDOW', 'POST_PIXEL')
Expand Down Expand Up @@ -88,60 +103,51 @@ def draw(self, context):
layout.prop(self, 'normal_rotation')

def execute(self, context):
dg = context.evaluated_depsgraph_get()
ob = context.object # default to selected object

if ob:
rotation = Matrix()

if self.normal_rotation != Euler():
rotation = self.normal_rotation.to_matrix().to_4x4()

plane_co = self.hit_point
if self.height != 0:
plane_co.z = self.height

plane_no = rotation @ Vector((0, 0, 1))
bm = bmesh.new()
bm.from_object(ob, dg)
bmesh.ops.transform(
bm,
verts=bm.verts,
matrix=ob.matrix_world
)

# Perform bisection
bmesh.ops.bisect_plane(
bm,
geom=bm.faces[:] + bm.edges[:] + bm.verts[:],
clear_inner=True,
clear_outer=True,
plane_co=plane_co,
plane_no=plane_no
)

# Bisection cleanup
closest_edge = self.find_closest_edge(bm, self.hit_point)

# Could be that the angle of plane finds
# no vertices after bisection
if closest_edge is not None:
vertex_list = self.get_reachable_vertices(closest_edge)
for v in [v for v in bm.verts if v not in vertex_list]:
bm.verts.remove(v)

# Object creation and addition to scene
bisect_obj = bpy.data.objects.get("Bisect")
if bisect_obj is not None:
bpy.data.objects.remove(bisect_obj, do_unlink=True)

me = bpy.data.meshes.new("Bisect")
bm.to_mesh(me)
ob = bpy.data.objects.new("Bisect", me)
context.collection.objects.link(ob)
ob.select_set(True)

bm.free()
rotation = Matrix()

if self.normal_rotation != Euler():
rotation = self.normal_rotation.to_matrix().to_4x4()

plane_co = self.hit_point
if self.height != 0:
plane_co.z = self.height

plane_no = rotation @ Vector((0, 0, 1))
bm = self.bm.copy()

# Perform bisection
bmesh.ops.bisect_plane(
bm,
geom=bm.faces[:] + bm.edges[:] + bm.verts[:],
clear_inner=True,
clear_outer=True,
plane_co=plane_co,
plane_no=plane_no
)

# Bisection cleanup
closest_edge = self.find_closest_edge(bm, self.hit_point)

# Could be that the angle of plane finds
# no vertices after bisection
if closest_edge is not None:
vertex_list = self.get_reachable_vertices(closest_edge)
for v in [v for v in bm.verts if v not in vertex_list]:
bm.verts.remove(v)

# Object creation and addition to scene
bisect_obj = bpy.data.objects.get("Bisect")
if bisect_obj is not None:
bpy.data.objects.remove(bisect_obj, do_unlink=True)

me = bpy.data.meshes.new("Bisect")
bm.to_mesh(me)
ob = bpy.data.objects.new("Bisect", me)
context.collection.objects.link(ob)
ob.select_set(True)

bm.free()

return {'FINISHED'}

Expand Down

0 comments on commit 663e2f5

Please sign in to comment.