Collection Manager: Improve filtering 1. Task: T69577

Improve filtering for new collections.
Prevents adding new collections when the selected collection
isn't visible and includes the new collection in the current
filter until the filtering changes.
This commit is contained in:
Ryan Inch 2020-10-20 01:56:26 -04:00
parent 860d8d7104
commit 52fb8e51ac
3 changed files with 71 additions and 6 deletions

View File

@ -22,7 +22,7 @@ bl_info = {
"name": "Collection Manager",
"description": "Manage collections and their objects",
"author": "Ryan Inch",
"version": (2, 15, 3),
"version": (2, 16, 0),
"blender": (2, 80, 0),
"location": "View3D - Object Mode (Shortcut - M)",
"warning": '', # used for warning icon and text in addons panel

View File

@ -69,6 +69,8 @@ from .operator_utils import (
set_exclude_state,
)
from . import ui
class SetActiveCollection(Operator):
'''Set the active collection'''
bl_label = "Set Active Collection"
@ -1227,9 +1229,23 @@ class CMNewCollectionOperator(Operator):
def execute(self, context):
global rto_history
new_collection = bpy.data.collections.new('Collection')
new_collection = bpy.data.collections.new("New Collection")
cm = context.scene.collection_manager
# prevent adding collections when collections are filtered
# and the selection is ambiguous
if cm.cm_list_index == -1 and ui.CM_UL_items.filtering:
send_report("Cannot create new collection.\n"
"No collection is selected and collections are filtered."
)
return {'CANCELLED'}
if cm.cm_list_index > -1 and not ui.CM_UL_items.visible_items[cm.cm_list_index]:
send_report("Cannot create new collection.\n"
"The selected collection isn't visible."
)
return {'CANCELLED'}
# if there are collections
if len(cm.cm_list_collection) > 0:
@ -1277,6 +1293,9 @@ class CMNewCollectionOperator(Operator):
layer_collection = layer_collections[new_collection.name]["ptr"]
context.view_layer.active_layer_collection = layer_collection
# show the new collection when collections are filtered.
ui.CM_UL_items.new_collections.append(new_collection.name)
global rename
rename[0] = True

View File

@ -483,20 +483,42 @@ class CollectionManager(Operator):
class CM_UL_items(UIList):
filtering = False
last_filter_value = ""
selected_objects = set()
active_object = None
visible_items = []
new_collections = []
filter_name: StringProperty(
name="Filter By Name",
default="",
description="Filter collections by name",
update=lambda self, context:
CM_UL_items.new_collections.clear(),
)
use_filter_invert: BoolProperty(
name="Invert",
default=False,
description="Invert filtering (show hidden items, and vice-versa)",
)
filter_by_selected: BoolProperty(
name="Filter By Selected",
default=False,
description="Filter collections by selected items"
description="Filter collections by selected items",
update=lambda self, context:
CM_UL_items.new_collections.clear(),
)
filter_by_qcd: BoolProperty(
name="Filter By QCD",
default=False,
description="Filter collections to only show QCD slots"
description="Filter collections to only show QCD slots",
update=lambda self, context:
CM_UL_items.new_collections.clear(),
)
def draw_item(self, context, layout, data, item, icon, active_data,active_propname, index):
@ -750,13 +772,14 @@ class CM_UL_items(UIList):
subrow.prop(self, "filter_by_qcd", text="", icon='EVENT_Q')
def filter_items(self, context, data, propname):
CM_UL_items.filtering = True
flt_flags = []
flt_neworder = []
list_items = getattr(data, propname)
if self.filter_name:
flt_flags = filter_items_by_name_insensitive(self.filter_name, self.bitflag_filter_item, list_items)
flt_flags = filter_items_by_name_custom(self.filter_name, self.bitflag_filter_item, list_items)
elif self.filter_by_selected:
flt_flags = [0] * len(list_items)
@ -768,6 +791,10 @@ class CM_UL_items(UIList):
if not set(context.selected_objects).isdisjoint(collection.objects):
flt_flags[idx] |= self.bitflag_filter_item
# add in any recently created collections
if item.name in CM_UL_items.new_collections:
flt_flags[idx] |= self.bitflag_filter_item
elif self.filter_by_qcd:
flt_flags = [0] * len(list_items)
@ -775,13 +802,28 @@ class CM_UL_items(UIList):
if item.qcd_slot_idx:
flt_flags[idx] |= self.bitflag_filter_item
# add in any recently created collections
if item.name in CM_UL_items.new_collections:
flt_flags[idx] |= self.bitflag_filter_item
else: # display as treeview
CM_UL_items.filtering = False
CM_UL_items.new_collections.clear()
flt_flags = [self.bitflag_filter_item] * len(list_items)
for idx, item in enumerate(list_items):
if not layer_collections[item.name]["visible"]:
flt_flags[idx] = 0
if self.use_filter_invert:
for idx, flag in enumerate(flt_flags):
flt_flags[idx] = 0 if flag else self.bitflag_filter_item
# update visible items list
CM_UL_items.visible_items.clear()
CM_UL_items.visible_items.extend(flt_flags)
return flt_flags, flt_neworder
@ -1027,7 +1069,7 @@ def update_icon(base, icon, theme_color):
icon.icon_pixels_float = colored_icon
def filter_items_by_name_insensitive(pattern, bitflag, items, propname="name", flags=None, reverse=False):
def filter_items_by_name_custom(pattern, bitflag, items, propname="name", flags=None, reverse=False):
"""
Set FILTER_ITEM for items which name matches filter_name one (case-insensitive).
pattern is the filtering pattern.
@ -1060,4 +1102,8 @@ def filter_items_by_name_insensitive(pattern, bitflag, items, propname="name", f
if bool(name and fnmatch.fnmatch(name, pattern)) is not bool(reverse):
flags[i] |= bitflag
# add in any recently created collections
if item.name in CM_UL_items.new_collections:
flags[i] |= bitflag
return flags