[feat] change function "selected by Virtools groups"

- remove ignore hidden property. because it is rarely used.
- support more select mode. like blender selection.
- merge function filter by group and select by group as a united function.
This commit is contained in:
yyc12345 2023-01-28 20:05:18 +08:00
parent b58f837a94
commit c9e51c9b6a
2 changed files with 49 additions and 78 deletions

View File

@ -7,32 +7,55 @@ class BALLANCE_OT_select_virtools_group(UTILS_virtools_prop.common_group_name_pr
bl_label = "Select by Virtools Group" bl_label = "Select by Virtools Group"
bl_options = {'UNDO'} bl_options = {'UNDO'}
merge_selection: bpy.props.BoolProperty( selection_type: bpy.props.EnumProperty(
name="Merge Selection", name="Mode",
description="Merge selection, rather than re-select them.", description="Selection mode",
default=False, items=(
) ('SET', "Set", "Sets a new selection.", "SELECT_SET", 0),
('EXTEND', "Extend", "Adds newly selected items to the existing selection.", "SELECT_EXTEND", 1),
ignore_hide: bpy.props.BoolProperty( ('SUBTRACT', "Subtract", "Removes newly selected items from the existing selection.", "SELECT_SUBTRACT", 2),
name="Ignore Hide Property", ('DIFFERENCE', "Invert", "Inverts the selection.", "SELECT_DIFFERENCE", 3),
description="Select objects without considering visibility.", ('INTERSECT', "Intersect", "Selects items that intersect with the existing selection.", "SELECT_INTERSECT", 4),
default=False, ),
default='SET'
) )
def execute(self, context): def execute(self, context):
# iterate object if self.selection_type == 'SET':
for obj in bpy.context.scene.objects: # iterate object
# ignore hidden objects for obj in bpy.context.scene.objects:
if (not self.ignore_hide) and obj.hide_get() == True: # check group and decide whether select this obj
continue obj.select_set(UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()))
elif self.selection_type == 'EXTEND':
# also iterate all objects
for obj in bpy.context.scene.objects:
# directly add if group matched. do not deselect anything
if UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()):
obj.select_set(True)
elif self.selection_type == 'SUBTRACT':
# subtract only involving selected item. so we get selected objest first
# and iterate it to reduce useless operations
selected = bpy.context.selected_objects[:]
for obj in selected:
# remove matched only
if UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()):
obj.select_set(False)
# check group elif self.selection_type == 'DIFFERENCE':
if UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()): # construct a selected obj set for convenient operations
# select object selected_set = set(bpy.context.selected_objects)
obj.select_set(True) # iterate all objects
else: for obj in bpy.context.scene.objects:
# if not in merge mode, deselect them # use xor to select
if not self.merge_selection: # in_selected XOR in_group
obj.select_set((obj in selected_set) ^ UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()))
elif self.selection_type == 'INTERSECT':
# like subtract, only iterate selected obj
selected = bpy.context.selected_objects[:]
for obj in selected:
# remove not matched
if not UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string()):
obj.select_set(False) obj.select_set(False)
return {'FINISHED'} return {'FINISHED'}
@ -40,63 +63,13 @@ class BALLANCE_OT_select_virtools_group(UTILS_virtools_prop.common_group_name_pr
def draw(self, context): def draw(self, context):
layout = self.layout layout = self.layout
row = layout.row() layout.label(text='Selection Parameters')
row.prop(self, 'ignore_hide') layout.prop(self, 'selection_type', expand=True, icon_only=True)
row.prop(self, 'merge_selection')
layout.separator() layout.separator()
layout.label(text='Group Parameters')
self.parent_draw(layout) self.parent_draw(layout)
class BALLANCE_OT_filter_virtools_group(UTILS_virtools_prop.common_group_name_props):
"""Filter objects by Virtools Group."""
bl_idname = "ballance.filter_virtools_group"
bl_label = "Filter by Virtools Group"
bl_options = {'UNDO'}
reverse_selection: bpy.props.BoolProperty(
name="Reverse",
description="Reverse operation. Remove matched objects.",
default=False,
)
ignore_hide: bpy.props.BoolProperty(
name="Ignore Hide Property",
description="Select objects without considering visibility.",
default=False,
)
def execute(self, context):
# make a copy for all objects, to ensure it is not viotile
# becuase we need deselect some objects in for statement
selected = bpy.context.selected_objects[:]
# iterate object
for obj in selected:
# ignore hidden objects
if (not self.ignore_hide) and obj.hide_get() == True:
continue
# check group and decide select
is_selected = UTILS_virtools_prop.check_virtools_group_data(obj, self.get_group_name_string())
if self.reverse_selection:
is_selected = not is_selected
# select object
obj.select_set(is_selected)
return {'FINISHED'}
def draw(self, context):
layout = self.layout
row = layout.row()
row.prop(self, 'ignore_hide')
row.prop(self, 'reverse_selection')
layout.separator()
self.parent_draw(layout)
class BALLANCE_OT_ctx_set_group(UTILS_virtools_prop.common_group_name_props): class BALLANCE_OT_ctx_set_group(UTILS_virtools_prop.common_group_name_props):
"""Grouping selected objects""" """Grouping selected objects"""
bl_idname = "ballance.ctx_set_group" bl_idname = "ballance.ctx_set_group"

View File

@ -159,7 +159,6 @@ classes = (
PROPS_virtools_material.BALLANCE_PT_virtools_material, PROPS_virtools_material.BALLANCE_PT_virtools_material,
OBJS_group_opers.BALLANCE_OT_select_virtools_group, OBJS_group_opers.BALLANCE_OT_select_virtools_group,
OBJS_group_opers.BALLANCE_OT_filter_virtools_group,
OBJS_group_opers.BALLANCE_OT_ctx_set_group, OBJS_group_opers.BALLANCE_OT_ctx_set_group,
OBJS_group_opers.BALLANCE_OT_ctx_unset_group, OBJS_group_opers.BALLANCE_OT_ctx_unset_group,
OBJS_group_opers.BALLANCE_OT_ctx_clear_group, OBJS_group_opers.BALLANCE_OT_ctx_clear_group,
@ -195,8 +194,7 @@ def menu_func_ballance_select(self, context):
layout = self.layout layout = self.layout
layout.separator() layout.separator()
layout.label(text="Ballance") layout.label(text="Ballance")
layout.operator(OBJS_group_opers.BALLANCE_OT_select_virtools_group.bl_idname, icon='SELECT_SET') layout.operator(OBJS_group_opers.BALLANCE_OT_select_virtools_group.bl_idname, icon='RESTRICT_SELECT_OFF')
layout.operator(OBJS_group_opers.BALLANCE_OT_filter_virtools_group.bl_idname, icon='FILTER')
def menu_func_ballance_grouping(self, context): def menu_func_ballance_grouping(self, context):
layout = self.layout layout = self.layout
layout.separator() layout.separator()