Skip to content

Commit

Permalink
Feature #78 : Add option to remove empty catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorgious56 committed Jul 21, 2022
1 parent 51041b2 commit 4f2e525
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 47 deletions.
2 changes: 2 additions & 0 deletions core/cache/prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from asset_browser_utilities.module.catalog.operator.move_from_a_to_b import CatalogMoveFromAToBOperatorProperties
from asset_browser_utilities.module.catalog.operator.move_to import CatalogMoveOperatorProperties
from asset_browser_utilities.module.catalog.operator.remove_from import CatalogRemoveFromOperatorProperties
from asset_browser_utilities.module.catalog.operator.remove_empty import CatalogRemoveEmptyOperatorProperties
from asset_browser_utilities.module.custom_property.operator.set import CustomPropertySetOperatorProperties
from asset_browser_utilities.module.custom_property.operator.remove import RemoveCustomPropertyOperatorProperties
from asset_browser_utilities.module.description.set import DescriptionSetOperatorProperties
Expand Down Expand Up @@ -50,6 +51,7 @@ class Cache(PropertyGroup):
op_catalog_move_from_a_to_b: PointerProperty(type=CatalogMoveFromAToBOperatorProperties)
op_catalog_move: PointerProperty(type=CatalogMoveOperatorProperties)
op_catalog_remove: PointerProperty(type=CatalogRemoveFromOperatorProperties)
op_catalog_remove_empty: PointerProperty(type=CatalogRemoveEmptyOperatorProperties)
op_catalog_sort_like_folders: PointerProperty(type=CatalogSortLikeFoldersOperatorProperties)
op_author_set: PointerProperty(type=AuthorSetOperatorProperties)
op_description_set: PointerProperty(type=DescriptionSetOperatorProperties)
Expand Down
5 changes: 3 additions & 2 deletions core/filter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class AssetFilterSettings(PropertyGroup):
filter_types_allow: BoolProperty(default=True)
filter_types: PointerProperty(type=FilterTypes)
filter_name: PointerProperty(type=FilterName)
filter_selection: PointerProperty(type=FilterSelection)
Expand All @@ -32,6 +31,7 @@ def init_asset_filter_settings(
filter_assets=False,
filter_selection_allow_view_3d=True,
filter_selection_allow_asset_browser=True,
filter_types=True,
):
self.filter_selection.init(
allow=filter_selection and get_from_cache(LibraryExportSettings).source == LibraryType.FileCurrent.value,
Expand All @@ -41,6 +41,7 @@ def init_asset_filter_settings(
self.filter_assets_allow = filter_assets
self.filter_assets = filter_assets
self.filter_catalog.allow = filter_assets
self.filter_types.allow = filter_types

def get_objects_that_satisfy_filters(self):
data_containers = (
Expand All @@ -67,7 +68,7 @@ def get_objects_that_satisfy_filters(self):

def draw(self, layout, context):
self.filter_selection.draw(layout)
if self.filter_types_allow:
if self.filter_types.allow:
self.filter_types.draw(layout)
self.filter_name.draw(layout, name_override="Assets")
if self.filter_catalog.allow:
Expand Down
1 change: 1 addition & 0 deletions core/filter/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def filter(self, test):

@staticmethod
def filter_static(test, method, value, case_sensitive):
# Returns True when the input text matches the filter
if not case_sensitive:
test = test.lower()
value = value.lower()
Expand Down
1 change: 1 addition & 0 deletions core/filter/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def get_object_types():


class FilterTypes(PropertyGroup):
allow: BoolProperty(default=True)
types_global_filter: BoolProperty(default=True, name="Filter By Type")
types: EnumProperty(
options={"ENUM_FLAG"},
Expand Down
36 changes: 29 additions & 7 deletions core/operator/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,22 @@ def do_on_asset(self, asset):
pass


def update_asset_filter_allow(filter_assets=False, allow_asset_browser_selection=True):
filter_selection = not get_from_cache(LibraryExportSettings).source in (
LibraryType.FolderExternal.value,
LibraryType.FileExternal.value,
def update_asset_filter_allow(
filter_assets=False, allow_asset_browser_selection=True, filter_types=True, filter_selection=True
):
filter_selection = (
not get_from_cache(LibraryExportSettings).source
in (
LibraryType.FolderExternal.value,
LibraryType.FileExternal.value,
)
and filter_selection
)
get_from_cache(AssetFilterSettings).init_asset_filter_settings(
filter_selection=filter_selection,
filter_assets=filter_assets,
filter_selection_allow_asset_browser=allow_asset_browser_selection,
filter_types=filter_types,
)


Expand All @@ -112,7 +119,9 @@ def update_preset(self, context):
setting.copy_from(default_setting)
else:
copy_simple_property_group(default_setting, setting)
update_asset_filter_allow(self.filter_assets)
update_asset_filter_allow(
self.filter_assets, filter_types=self.filter_type, filter_selection=self.filter_selection
)


class BatchFolderOperator(ImportHelper):
Expand All @@ -131,8 +140,20 @@ class BatchFolderOperator(ImportHelper):
directory: StringProperty()
logic_class = BatchExecute

def _invoke(self, context, remove_backup=True, filter_assets=False, enforce_filebrowser=False):
def _invoke(
self,
context,
remove_backup=True,
filter_assets=False,
filter_type=True,
filter_selection=True,
custom_operation=True,
enforce_filebrowser=False,
):
self.filter_assets = filter_assets
self.filter_type = filter_type
self.filter_selection = filter_selection
self.custom_operation = custom_operation

update_preset(self, context)
self.init_operation_settings()
Expand Down Expand Up @@ -194,4 +215,5 @@ def draw(self, context):
if hasattr(self, "operator_settings") and self.operator_settings and hasattr(self.operator_settings, "draw"):
get_from_cache(self.operator_settings.__class__).draw(layout)
get_from_cache(AssetFilterSettings).draw(layout, context)
get_from_cache(OperationSettings).draw(layout, context)
if self.custom_operation:
get_from_cache(OperationSettings).draw(layout, context)
35 changes: 0 additions & 35 deletions core/test/files/blender_assets.cats.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,3 @@ VERSION 1
a5495e97-b32f-4899-a1ba-4f455068f040:Catalog:Catalog
6a287106-5984-4db7-ae6d-b02ff955b0ac:catalog_from:catalog_from
02c809ff-cf34-473b-b348-1eadcdd87472:catalog_to:catalog_to
29ec07be-2b68-4332-8f13-93c6d13a37b6:Test:Test
d207d226-c4f4-4eb7-ac62-404ab0b4dcb6:source:source
90f7a036-69e4-4da6-91f6-10b60e886245:collection_asset:collection_asset
7e3f20e4-6093-4518-a0ed-9da276c35c9e:material_asset:material_asset
f13ef426-48df-4f9e-aeec-0e873893d57c:node_tree_asset:node_tree_asset
deec1f0f-df95-48dd-abe3-c9407a0f53ec:asset_copy_from:asset_copy_from
4eeaae59-5c51-4f69-bab7-f3f879961193:object_armature_asset:object_armature_asset
fa86c369-f650-4408-9e39-8b767758cdbf:object_camera_asset:object_camera_asset
fbae1c49-06f4-4fa9-b2af-1de3b272c6ad:object_curve_asset:object_curve_asset
358c7891-804e-45de-9be5-8c93ba3f4bf5:object_empty_asset:object_empty_asset
213624ec-648e-444d-98a0-e77eb369f580:object_gp_asset:object_gp_asset
77efd9dd-6e49-4a77-9db9-d82a5087341f:object_lattice_asset:object_lattice_asset
976e95ee-62c3-4879-94f3-15d093072803:object_light_asset:object_light_asset
1df60321-5bc1-4435-a0fb-f547fd1e4aa0:object_lightprobe_asset:object_lightprobe_asset
2d5e243e-a867-476d-825e-e296306c7ed0:object_mesh_asset:object_mesh_asset
8f31e6da-34be-4332-a82a-bcb32b0e7a29:object_meta_asset:object_meta_asset
4af69a29-d5a1-4ecf-9e3d-2cb2cf67a179:object_nurbs_asset:object_nurbs_asset
6f6b84ee-129b-4702-a0d4-3f804bd1c10d:object_text_asset:object_text_asset
c542b614-56f3-4f81-8dec-6e4c68e84446:object_volume_asset:object_volume_asset
85db7b23-44e7-46ec-a0bb-55f2f878fd43:world_asset:world_asset
239bf6b1-7018-4c89-b154-813724f5303f:copy_from:copy_from
78c876b8-cfef-4ac0-8dd2-5312def2cc5a:asset_mark_unmark:asset_mark_unmark
3cdaae37-57a4-444e-991e-50753f7f6dfc:Black.001:Black.001
40c8fc4e-540c-47e9-afd5-8c484822fac0:Armature:Armature
acbcf6fd-2fe8-44c0-a60d-3ff882ac2242:Camera.001:Camera.001
e79cd49d-1564-476f-8d39-aca54a1e3b1b:BezierCurve:BezierCurve
67b799b9-da09-4bc6-a4da-2cea4a7b31ac:Suzanne.002:Suzanne.002
48aed2ae-76d2-48d9-90a2-cd00698db191:Lattice:Lattice
5fe5e6e1-05bb-46eb-bf08-a3ff2eaac2fb:Point.001:Point.001
0b59e84b-67d1-45b9-a684-853e49b4efe4:ReflectionCubemap.001:ReflectionCubemap.001
f83ed4ff-057d-4200-90da-73376c0e5347:Cube:Cube
c9ca6e49-a46a-4916-9495-838f565450dd:Mball:Mball
ebee6817-6158-4fa6-a71e-eab6d098f23c:SurfCircle:SurfCircle
9a758cac-8383-4162-a220-46a54a6e6be3:Text.001:Text.001
5c54a4e3-8303-41c6-b688-821963478c37:Volume:Volume
3 changes: 2 additions & 1 deletion core/test/scripts/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from asset_browser_utilities.module.asset.test import mark, unmark, copy
from asset_browser_utilities.module.author.test import set as set_author
from asset_browser_utilities.module.description.test import set as set_description
from asset_browser_utilities.module.catalog.test import move_from_a_to_b, move_to, remove_from
from asset_browser_utilities.module.catalog.test import move_from_a_to_b, move_to, remove_from, remove_empty
from asset_browser_utilities.module.preview.test import extract, import_

from inspect import getmembers, isfunction
Expand All @@ -17,6 +17,7 @@
move_from_a_to_b,
move_to,
remove_from,
remove_empty,
extract,
import_,
)
Expand Down
2 changes: 1 addition & 1 deletion core/ui/menu/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def draw(self, context):

def setup_ops(self, layout, context):
for op in self.ops_cmd:
self.add_op(layout, context, op[0], op[1], op[2] if len(op) > 2 else None)
self.add_op(layout, context, *op)

def add_op(self, layout, context, command, text="", icon=None):
library_source = LibraryType.get_library_type_from_context(context)
Expand Down
47 changes: 47 additions & 0 deletions module/catalog/operator/remove_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from bpy.types import Operator, PropertyGroup
from bpy.props import PointerProperty

from asset_browser_utilities.core.operator.tool import BatchExecute, BatchFolderOperator
from asset_browser_utilities.core.cache.tool import get_from_cache
from asset_browser_utilities.core.filter.main import AssetFilterSettings
from asset_browser_utilities.core.log.logger import Logger
from asset_browser_utilities.module.asset.tool import all_assets
from asset_browser_utilities.module.catalog.tool import CatalogsHelper


class CatalogRemoveEmptyBatchExecute(BatchExecute):
def execute_one_file_and_the_next_when_finished(self):
asset_uuids = set(asset.asset_data.catalog_id for asset in all_assets())
helper = CatalogsHelper()

asset_filter_settings = get_from_cache(AssetFilterSettings)
filter_name = asset_filter_settings.filter_name
filter_func = lambda name: filter_name.filter(name) if filter_name.active else lambda: True

catalogs = {cat[0]: cat[1] for cat in helper.get_catalogs() if filter_func(cat[1])}
catalog_uuids = set(catalogs.keys())

empty_catalogs = catalog_uuids.difference(asset_uuids)
for empty_catalog_uuid in empty_catalogs:
helper.remove_catalog_by_uuid(empty_catalog_uuid)
Logger.display(f"Removed empty catalog '{catalogs[empty_catalog_uuid]}'")

self.save_file()
self.execute_next_blend()


class CatalogRemoveEmptyOperatorProperties(PropertyGroup):
pass


class ABU_OT_catalog_remove_empty(Operator, BatchFolderOperator):
bl_idname = "abu.catalog_remove_empty"
bl_label = "Batch Remove Empty Catalogs"

operator_settings: PointerProperty(type=CatalogRemoveEmptyOperatorProperties)
logic_class = CatalogRemoveEmptyBatchExecute

def invoke(self, context, event):
return self._invoke(
context, filter_assets=False, filter_type=False, filter_selection=False, custom_operation=False
)
31 changes: 31 additions & 0 deletions module/catalog/test/remove_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import bpy
from asset_browser_utilities.core.test.prop import TestOperator

from asset_browser_utilities.module.catalog.operator.remove_empty import CatalogRemoveEmptyBatchExecute

from asset_browser_utilities.module.catalog.tool import all_catalogs
from asset_browser_utilities.module.asset.tool import all_assets


def test_removing_all_empty_catalogs(filepath):
test_op = TestOperator(
filepath=filepath,
filter_assets=False,
op_name="op_catalog_remove_empty",
logic_class=CatalogRemoveEmptyBatchExecute,
)

catalogs_uuids = set(cat[0] for cat in all_catalogs())
# /!\ Assets with no catalog have a catalog uuid of '00000000-0000-0000-0000-000000000000'
catalogs_uuids_assets = set(asset.asset_data.catalog_id for asset in all_assets())
assert len(catalogs_uuids) + 1 > len(catalogs_uuids_assets)

test_op.execute()

catalogs_uuids = [cat[0] for cat in all_catalogs()]
assert len(catalogs_uuids) + 1 == len(catalogs_uuids_assets)


def test_removing_an_empty_catalog_by_name(filepath):
# TODO
pass
14 changes: 13 additions & 1 deletion module/catalog/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from asset_browser_utilities.core.file.path import read_lines_sequentially


def all_catalogs():
return CatalogsHelper().get_catalogs(None, None)


class CatalogsHelper:
CATALOGS_FILENAME = "blender_assets.cats.txt"

Expand Down Expand Up @@ -117,8 +121,16 @@ def catalog_line_from_uuid(self, uuid):
if this_uuid == uuid:
return line

def remove_catalog_by_uuid(self, uuid):
lines = list(read_lines_sequentially(self.catalog_filepath))
with open(self.catalog_filepath, "w") as catalog_file:
for line in lines:
if line.startswith(str(uuid)):
continue
catalog_file.write(line)

@staticmethod
def get_catalogs(filter_catalog, context): # Keep both arguments even if not used. It's a callback !
def get_catalogs(filter_catalog=None, context=None): # Keep both arguments even if not used. It's a callback !
helper = CatalogsHelper()
catalogs = []
if helper.has_catalogs:
Expand Down
1 change: 1 addition & 0 deletions module/catalog/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ class ABU_MT_catalogs(Menu, ABUOperatorsMenu):
("abu.catalog_move", "Move To Catalog", "ADD"),
("abu.catalog_remove_from", "Remove From Catalog", "REMOVE"),
("abu.catalog_move_from_a_to_b", "Move From A to B", "FOLDER_REDIRECT"),
("abu.catalog_remove_empty", "Remove Empty", "BRUSH_DATA"),
("abu.sort_catalogs_like_folders", "Create From Folders", "FILE_FOLDER"),
]

0 comments on commit 4f2e525

Please sign in to comment.