Pose Library: Make pose bone select operators work from the asset view

The operator in the context menu of the pose asset view to select and
deselect bones now works fine there. 8750f355b32b was needed for this.

Note that there are some TODOs on the asset-handle design,
see 8750f355b32b.
This commit is contained in:
Julian Eisel 2021-04-02 19:21:57 +02:00
parent 7c751bab2a
commit b2accf5f7c
2 changed files with 34 additions and 29 deletions

View File

@ -27,7 +27,11 @@ from typing import Any, List, Set, cast, Iterable
Datablock = Any
import bpy
from bpy.types import Context
from bpy.types import (
FileSelectEntry,
AssetLibraryReference,
Context,
)
def asset_mark(context: Context, datablock: Any) -> Set[str]:
@ -90,30 +94,22 @@ def has_assets(filepath: Path) -> bool:
return False
def active_asset_library_path(context: Context) -> Path:
asset_library_name: str = context.space_data.params.asset_library
paths = context.preferences.filepaths
asset_lib = paths.asset_libraries[asset_library_name]
return Path(asset_lib.path)
@dataclasses.dataclass
class AssetLoadInfo:
"""Everything you need to temp-load an asset."""
file_path: Path
file_path: str
asset_name: str
id_type: str # As familiar from the file browser, so 'Actions' for Actions.
id_type: str
def active_asset_load_info(context: Context) -> AssetLoadInfo:
asset_lib_path = active_asset_library_path(context)
file_and_datablock_name: str = context.space_data.params.filename
filename, id_type, id_name = file_and_datablock_name.split("/", 2)
def active_asset_load_info(asset_library: AssetLibraryReference, asset: FileSelectEntry) -> AssetLoadInfo:
asset_lib_path = bpy.types.AssetHandle.get_full_library_path(asset, asset_library)
if asset_lib_path == "":
return None
return AssetLoadInfo(
asset_lib_path / filename,
id_name,
id_type,
asset_lib_path,
asset.name,
asset.id_type,
)

View File

@ -41,6 +41,7 @@ from bpy.props import BoolProperty, StringProperty
from bpy.types import (
Action,
Context,
FileSelectEntry,
Object,
Operator,
)
@ -266,18 +267,16 @@ class PoseAssetUser:
@classmethod
def poll(cls, context: Context) -> bool:
return bool(
context.space_data.type == "FILE_BROWSER"
and context.space_data.browse_mode == "ASSETS"
and context.space_data.params.asset_library
and context.space_data.params.filename
and context.object
context.object
and context.object.mode == "POSE" # This condition may not be desired.
and asset_utils.SpaceAssetInfo.get_active_asset(context) is not None
and context.asset_library
and context.asset_file_handle
)
def execute(self, context: Context) -> Set[str]:
if context.space_data.params.asset_library == "LOCAL":
return self.use_pose(context, context.id)
asset: FileSelectEntry = context.asset_file_handle
if asset.local_id:
return self.use_pose(context, asset.local_id)
return self._load_and_use_pose(context)
def use_pose(self, context: Context, asset: bpy.types.ID) -> Set[str]:
@ -285,8 +284,18 @@ class PoseAssetUser:
pass
def _load_and_use_pose(self, context: Context) -> Set[str]:
asset_load_info = functions.active_asset_load_info(context)
if asset_load_info.id_type != "Action":
asset_library = context.asset_library
asset = context.asset_file_handle
asset_load_info = functions.active_asset_load_info(asset_library, asset)
if not asset_load_info:
self.report( # type: ignore
{"ERROR"},
# TODO: Add some way to get the library name from the library reference (just asset_library.name?).
f"Selected asset {asset_load_info.asset_name} could not be located inside the asset library",
)
return {"CANCELLED"}
if asset_load_info.id_type != 'ACTION':
self.report( # type: ignore
{"ERROR"},
f"Selected asset {asset_load_info.asset_name} is not an Action",
@ -294,7 +303,7 @@ class PoseAssetUser:
return {"CANCELLED"}
with bpy.types.BlendData.temp_data() as temp_data:
str_path = str(asset_load_info.file_path)
str_path = asset_load_info.file_path
with temp_data.libraries.load(str_path) as (data_from, data_to):
data_to.actions = [asset_load_info.asset_name]