Asset Browser: Disable metadata editing for external asset libraries

Buttons to edit asset metadata are now disabled for assets from an
external library (i.e. assets not stored in the current .blend file).
Their tooltips explain why they are disabled.

Had to do some RNA trickery to disable the metadata properties at RNA
level, not at UI script level.
The basic idea is:
* Local data-block assets set the data-block as owning ID for the asset
  metadata RNA pointer now.
* That way we can use the owner ID to see where the metadata belongs to
  and decide if it's editable that way.
* Additionaly, some Python operators needed better polling so they show
  as grayed out, and don't just fail.

One important thing: Custom properties of the metadata can still be
edited. The edits won't be saved however. Would be nice to disable that,
but it's currently not supported on BPY/IDProperty/RNA level.

Addresses T82943.

Differential Revision: https://developer.blender.org/D12127
This commit is contained in:
2021-09-23 14:43:21 +02:00
parent 059d01d42e
commit 222fd1abf0
4 changed files with 123 additions and 31 deletions

View File

@@ -27,17 +27,28 @@ from bpy_extras.asset_utils import (
)
class ASSET_OT_tag_add(Operator):
class AssetBrowserMetadataOperator:
@classmethod
def poll(cls, context):
if not SpaceAssetInfo.is_asset_browser_poll(context) or not context.asset_file_handle:
return False
if not context.asset_file_handle.local_id:
Operator.poll_message_set(
"Asset metadata from external asset libraries can't be "
"edited, only assets stored in the current file can"
)
return False
return True
class ASSET_OT_tag_add(AssetBrowserMetadataOperator, Operator):
"""Add a new keyword tag to the active asset"""
bl_idname = "asset.tag_add"
bl_label = "Add Asset Tag"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return SpaceAssetInfo.is_asset_browser_poll(context) and SpaceAssetInfo.get_active_asset(context)
def execute(self, context):
active_asset = SpaceAssetInfo.get_active_asset(context)
active_asset.tags.new("Unnamed Tag")
@@ -45,7 +56,7 @@ class ASSET_OT_tag_add(Operator):
return {'FINISHED'}
class ASSET_OT_tag_remove(Operator):
class ASSET_OT_tag_remove(AssetBrowserMetadataOperator, Operator):
"""Remove an existing keyword tag from the active asset"""
bl_idname = "asset.tag_remove"
@@ -54,21 +65,20 @@ class ASSET_OT_tag_remove(Operator):
@classmethod
def poll(cls, context):
if not SpaceAssetInfo.is_asset_browser_poll(context):
if not super().poll(context):
return False
active_asset = SpaceAssetInfo.get_active_asset(context)
if not active_asset:
return False
return active_asset.active_tag in range(len(active_asset.tags))
active_asset_file = context.asset_file_handle
asset_metadata = active_asset_file.asset_data
return asset_metadata.active_tag in range(len(asset_metadata.tags))
def execute(self, context):
active_asset = SpaceAssetInfo.get_active_asset(context)
tag = active_asset.tags[active_asset.active_tag]
active_asset_file = context.asset_file_handle
asset_metadata = active_asset_file.asset_data
tag = asset_metadata.tags[asset_metadata.active_tag]
active_asset.tags.remove(tag)
active_asset.active_tag -= 1
asset_metadata.tags.remove(tag)
asset_metadata.active_tag -= 1
return {'FINISHED'}