From e45804a3bb753006b6aeeef78327359fc724633c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= <sybren@blender.org> Date: Tue, 5 Oct 2021 16:27:33 +0200 Subject: [PATCH] Pose Library: assign catalog when creating new pose asset When creating a new pose asset (`POSELIB_OT_create_pose_asset`), assign it the the Asset Browser's active catalog. This ensures that the asset browser actually shows the newly created catalog. When there is no asset browser open, no catalog is assigned. When there are multiple asset browsers, the biggest one wins. --- pose_library/asset_browser.py | 7 ++++++- pose_library/operators.py | 21 +++++++++++++++++++-- pose_library/pose_creation.py | 16 ++++++++++++---- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/pose_library/asset_browser.py b/pose_library/asset_browser.py index 8bedd9682..036a271bf 100644 --- a/pose_library/asset_browser.py +++ b/pose_library/asset_browser.py @@ -68,9 +68,14 @@ def activate_asset( def active_catalog_id(asset_browser: bpy.types.Area) -> str: """Return the ID of the catalog shown in the asset browser.""" + return params(asset_browser).catalog_id + + +def params(asset_browser: bpy.types.Area) -> bpy.types.FileAssetSelectParams: + """Return the asset browser parameters given its Area.""" space_data = asset_browser.spaces[0] assert asset_utils.SpaceAssetInfo.is_asset_browser(space_data) - return space_data.params.catalog_id + return space_data.params def tag_redraw(screen: bpy.types.Screen) -> None: diff --git a/pose_library/operators.py b/pose_library/operators.py index ff958a1dc..9aa0333e7 100644 --- a/pose_library/operators.py +++ b/pose_library/operators.py @@ -82,6 +82,23 @@ class POSELIB_OT_create_pose_asset(PoseAssetCreator, Operator): pose_name: StringProperty(name="Pose Name") # type: ignore activate_new_action: BoolProperty(name="Activate New Action", default=True) # type: ignore + + @classmethod + def poll(cls, context: Context) -> bool: + # Make sure that if there is an asset browser open, the artist can see the newly created pose asset. + asset_browse_area: Optional[bpy.types.Area] = asset_browser.area_from_context(context) + if not asset_browse_area: + # No asset browser is visible, so there also aren't any expectations + # that this asset will be visible. + return True + + asset_space_params = asset_browser.params(asset_browse_area) + if asset_space_params.asset_library_ref != 'LOCAL': + cls.poll_message_set("Asset Browser must be set to the Current File library") + return False + + return True + def execute(self, context: Context) -> Set[str]: pose_name = self.pose_name or context.object.name asset = pose_creation.create_pose_asset_from_context(context, pose_name) @@ -107,13 +124,13 @@ class POSELIB_OT_create_pose_asset(PoseAssetCreator, Operator): This makes it possible to immediately check & edit the created pose asset. """ - asset_browse_area: Optional[bpy.types.Area] = asset_browser.biggest_asset_browser_area(context.screen) + asset_browse_area: Optional[bpy.types.Area] = asset_browser.area_from_context(context) if not asset_browse_area: return # After creating an asset, the window manager has to process the # notifiers before editors should be manipulated. - pose_creation.assign_tags_from_asset_browser(asset, asset_browse_area) + pose_creation.assign_from_asset_browser(asset, asset_browse_area) # Pass deferred=True, because we just created a new asset that isn't # known to the Asset Browser space yet. That requires the processing of diff --git a/pose_library/pose_creation.py b/pose_library/pose_creation.py index ac08b7762..ffebd22d8 100644 --- a/pose_library/pose_creation.py +++ b/pose_library/pose_creation.py @@ -23,13 +23,15 @@ Pose Library - creation functions. import dataclasses import functools import re + from typing import Optional, FrozenSet, Set, Union, Iterable, cast if "functions" not in locals(): - from . import functions + from . import asset_browser, functions else: import importlib + asset_browser = importlib.reload(asset_browser) functions = importlib.reload(functions) import bpy @@ -432,6 +434,12 @@ def find_keyframe(fcurve: FCurve, frame: float) -> Optional[Keyframe]: return None -def assign_tags_from_asset_browser(asset: Action, asset_browser: bpy.types.Area) -> None: - # TODO(Sybren): implement - return +def assign_from_asset_browser(asset: Action, asset_browser_area: bpy.types.Area) -> None: + """Assign some things from the asset browser to the asset. + + This sets the current catalog ID, and in the future could include tags + from the active dynamic catalog, etc. + """ + + cat_id = asset_browser.active_catalog_id(asset_browser_area) + asset.asset_data.catalog_id = cat_id -- GitLab