Skip to content

Commit

Permalink
fix: redirect some reference of bpy.context to context passed by oper…
Browse files Browse the repository at this point in the history
…ator argument
  • Loading branch information
yyc12345 committed Jan 3, 2025
1 parent 89a5e63 commit 4181096
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
27 changes: 14 additions & 13 deletions bbp_ng/OP_OBJECT_virtools_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def invoke(self, context, event):

def execute(self, context):
_select_object_by_virtools_group(
context,
self.general_get_group_name(),
_g_EnumHelper_SelectMode.get_selection(self.selection_mode)
)
Expand All @@ -65,17 +66,17 @@ def draw(self, context):
layout.label(text='Group Parameters')
self.draw_group_name_input(layout)

def _select_object_by_virtools_group(group_name: str, mode: SelectMode) -> None:
def _select_object_by_virtools_group(context: bpy.types.Context, group_name: str, mode: SelectMode) -> None:
match(mode):
case SelectMode.Set:
# iterate all objects and directly set
for obj in bpy.context.scene.objects:
for obj in context.scene.objects:
# check group and decide whether select this obj
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
obj.select_set(gp.contain_group(group_name))
case SelectMode.Extend:
# also iterate all objects
for obj in bpy.context.scene.objects:
for obj in context.scene.objects:
# but only increase selection, for selected object, skip check
if obj.select_get(): continue
# if not selected, check whether add it.
Expand All @@ -86,24 +87,24 @@ def _select_object_by_virtools_group(group_name: str, mode: SelectMode) -> None:
# subtract only involving selected item. so we get selected objest first
# and copy it (because we need modify it)
# and iterate it to reduce useless operations
selected = bpy.context.selected_objects[:]
selected = context.selected_objects[:]
for obj in selected:
# remove matched only
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
if gp.contain_group(group_name):
obj.select_set(False)
case SelectMode.Difference:
# construct a selected obj set for convenient operations
selected_set = set(bpy.context.selected_objects)
selected_set = set(context.selected_objects)
# iterate all objects
for obj in bpy.context.scene.objects:
for obj in context.scene.objects:
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
# use xor to select
# in_selected XOR in_group
obj.select_set((obj in selected_set) ^ gp.contain_group(group_name))
case SelectMode.Intersect:
# like subtract, only iterate selected obj
selected = bpy.context.selected_objects[:]
selected = context.selected_objects[:]
for obj in selected:
# but remove not matched
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
Expand All @@ -124,15 +125,15 @@ class BBP_OT_add_objects_virtools_group(bpy.types.Operator, PROP_virtools_group.

@classmethod
def poll(cls, context):
return len(bpy.context.selected_objects) != 0
return len(context.selected_objects) != 0

def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)

def execute(self, context):
group_name: str = self.general_get_group_name()
for obj in bpy.context.selected_objects:
for obj in context.selected_objects:
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
gp.add_group(group_name)
self.report({'INFO'}, "Grouping objects successfully.")
Expand All @@ -149,15 +150,15 @@ class BBP_OT_rm_objects_virtools_group(bpy.types.Operator, PROP_virtools_group.S

@classmethod
def poll(cls, context):
return len(bpy.context.selected_objects) != 0
return len(context.selected_objects) != 0

def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)

def execute(self, context):
group_name: str = self.general_get_group_name()
for obj in bpy.context.selected_objects:
for obj in context.selected_objects:
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
gp.remove_group(group_name)
self.report({'INFO'}, "Ungrouping objects successfully.")
Expand All @@ -174,15 +175,15 @@ class BBP_OT_clear_objects_virtools_group(bpy.types.Operator):

@classmethod
def poll(cls, context):
return len(bpy.context.selected_objects) != 0
return len(context.selected_objects) != 0

def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_confirm(self, event)

def execute(self, context):
# iterate object
for obj in bpy.context.selected_objects:
for obj in context.selected_objects:
with PROP_virtools_group.VirtoolsGroupsHelper(obj) as gp:
gp.clear_groups()
self.report({'INFO'}, "Clear objects groups successfully.")
Expand Down
4 changes: 2 additions & 2 deletions bbp_ng/OP_UV_flatten_uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class BBP_OT_flatten_uv(bpy.types.Operator):

@classmethod
def poll(cls, context):
obj = bpy.context.active_object
obj = context.active_object
if obj is None:
return False
if obj.type != 'MESH':
Expand All @@ -162,7 +162,7 @@ def execute(self, context):
return {'CANCELLED'}

# do flatten uv and report
failed: int = _flatten_uv_wrapper(bpy.context.active_object.data, flatten_param_)
failed: int = _flatten_uv_wrapper(context.active_object.data, flatten_param_)
if failed != 0:
print(f'[Flatten UV] {failed} faces are not be processed correctly because process failed.')
return {'FINISHED'}
Expand Down

0 comments on commit 4181096

Please sign in to comment.