Skip to content

Commit

Permalink
fix: add poll method to operators to ensure all conditions are correc…
Browse files Browse the repository at this point in the history
…t before they can be invoked
  • Loading branch information
tristan-hm committed Jan 23, 2022
1 parent f0c6632 commit 4faad28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions bolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def execute(self, context):
return self.handle_modal(context)


@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'


def handle_modal(self, context):
self.add_object(context)
self.add_screw_x_modifier()
Expand Down
6 changes: 6 additions & 0 deletions faux_bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def execute(self, context):
return self.handle_modal(context)


@classmethod
def poll(cls, context):
if context.mode == 'OBJECT':
return len(context.selected_objects) == 1


def handle_modal(self, context):
self.add_bevel_modifier(context)
self.add_weighted_normal_modifer(context)
Expand Down
20 changes: 11 additions & 9 deletions sketch_bevel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bpy
import bmesh


class ND_OT_sketch_bevel(bpy.types.Operator):
Expand Down Expand Up @@ -37,17 +38,18 @@ def invoke(self, context, event):
def execute(self, context):
return self.handle_modal(context)

@classmethod
def poll(cls, context):
if context.mode == 'EDIT_MESH':
mesh = bmesh.from_edit_mesh(context.object.data)
return len([vert for vert in mesh.verts if vert.select]) >= 1

def handle_modal(self, context):
if context.object and context.object.type == 'MESH' and context.mode == 'EDIT_MESH' and context.object.data.total_vert_sel > 0:
self.add_vertex_group(context)
self.add_bevel_modifier(context)

context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
else:
self.report({'WARNING'}, "No active object, could not finish")
return {'CANCELLED'}
self.add_vertex_group(context)
self.add_bevel_modifier(context)

context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}


def add_vertex_group(self, context):
Expand Down

0 comments on commit 4faad28

Please sign in to comment.