Assets: add function to copy asset data to another ID #108547

Manually merged
Sybren A. Stüvel merged 7 commits from dr.sybren/blender:pr/asset-data-copy-to-id into main 2023-06-08 10:15:03 +02:00

Add a function some_id.asset_data.copy_to_id(other_id) to copy the asset
data from one data-block to another. This is intended to be used in the
pose library, when updating a pose by simply creating a new asset and
having that replace the old one.

This is intentionally taking a copy, even though the above use case could
have sufficed with a higher-level 'move' function. By exposing this as a
copy, it can be used in a wider range of situations, from whatever Python
code wants to use it. This could include copying the asset data from the
active asset to all the other selected ones.

Any pre-existing asset data is freed before the copy is assigned. The
target ID MUST be marked as asset for this function to work. Clearing
the asset status by assigning None is not allowed. This will keep the
API clear, and ensures that marking & clearing is done via their respective
functions.

In the attached blend file, just press the 'Play' button in the text editor to see the code in effect. And then see the intentional error, comment out the line, see another intentional error, etc. This is the script in that blend file:

import bpy

D = bpy.data
src = D.objects['Suzanne']
dst = D.objects['Cube']

# This will fail, as assigning None is not allowed.
# Comment out to move forward with the test:
src.asset_data = None

# This will fail. Comment out to move forward with the test:
dst.asset_data = src.asset_data

# Destination datablock should already be an asset:
dst.asset_mark()
dst.asset_data = src.asset_data

# Also copy the preview image, which is not part of the asset data but nicer to see in the asset browser:
dst_preview = dst.preview_ensure()
dst_preview.image_size = src.preview.image_size
dst_preview.image_pixels = src.preview.image_pixels
Add a function `some_id.asset_data.copy_to_id(other_id)` to copy the asset data from one data-block to another. This is intended to be used in the pose library, when updating a pose by simply creating a new asset and having that replace the old one. This is intentionally taking a copy, even though the above use case could have sufficed with a higher-level 'move' function. By exposing this as a copy, it can be used in a wider range of situations, from whatever Python code wants to use it. This could include copying the asset data from the active asset to all the other selected ones. Any pre-existing asset data is freed before the copy is assigned. The target ID MUST be marked as asset for this function to work. Clearing the asset status by assigning `None` is not allowed. This will keep the API clear, and ensures that marking & clearing is done via their respective functions. In the attached blend file, just press the 'Play' button in the text editor to see the code in effect. And then see the intentional error, comment out the line, see another intentional error, etc. This is the script in that blend file: ```python import bpy D = bpy.data src = D.objects['Suzanne'] dst = D.objects['Cube'] # This will fail, as assigning None is not allowed. # Comment out to move forward with the test: src.asset_data = None # This will fail. Comment out to move forward with the test: dst.asset_data = src.asset_data # Destination datablock should already be an asset: dst.asset_mark() dst.asset_data = src.asset_data # Also copy the preview image, which is not part of the asset data but nicer to see in the asset browser: dst_preview = dst.preview_ensure() dst_preview.image_size = src.preview.image_size dst_preview.image_pixels = src.preview.image_pixels ```
Sybren A. Stüvel added the
Module
Pipeline, Assets & IO
label 2023-06-02 16:08:26 +02:00
Sybren A. Stüvel added 1 commit 2023-06-02 16:08:36 +02:00
2cd1343af0 Assets: add function to copy asset data to another ID
Add a function `some_id.asset_data.copy_to_id(other_id)` to copy the asset
data from one data-block to another. This is intended to be used in the
pose library, when updating a pose by simply creating a new asset and
having that replace the old one.

This is intentionally taking a copy, even though the above use case could
have sufficed with a higher-level 'move' function. By exposing this as a
copy, it can be used in a wider range of situations, from whatever Python
code wants to use it. This could include copying the asset data from the
active asset to all the other selected ones.

Any pre-existing asset data is freed before the copy is assigned. The
target ID doesn't have to be marked as asset yet for this function to work.
Note that using this function will make the destination ID an asset,
without going through the regular operator.
Sybren A. Stüvel requested review from Julian Eisel 2023-06-02 16:08:46 +02:00
Julian Eisel reviewed 2023-06-02 16:29:11 +02:00
Julian Eisel left a comment
Member

I wonder if some_id.asset_data.copy_to_id(other_id) is "the right way" around for this (not strongly against it though). I'd expect that I call a function on the ID I want to modify, not the one I want to get the value from. Esp given that this is also how I mark/clear an asset.

We could also make ID.asset_data editable, and do a deep copy in the setter callback. I guess that would be the expected behavior when doing one_id.asset_data = other_id.asset_data?

I wonder if `some_id.asset_data.copy_to_id(other_id)` is "the right way" around for this (not strongly against it though). I'd expect that I call a function on the ID I want to modify, not the one I want to get the value from. Esp given that this is also how I mark/clear an asset. We could also make `ID.asset_data` editable, and do a deep copy in the setter callback. I guess that would be the expected behavior when doing `one_id.asset_data = other_id.asset_data`?
@ -340,1 +343,4 @@
void rna_AssetMetaData_copy_to_id(AssetMetaData *self, ReportList *reports, ID *destination)
{
if (destination == NULL) {
Member

For an RNA callback this has a bit too much knowledge/logic for my taste, I'd prefer having this in some ED_asset_xxx() function. Similar to how we have ED_asset_mark_id()/ED_asset_clear_id().

For an RNA callback this has a bit too much knowledge/logic for my taste, I'd prefer having this in some `ED_asset_xxx()` function. Similar to how we have `ED_asset_mark_id()`/`ED_asset_clear_id()`.
Author
Member

"the right way" around

The advantage of this approach is that source.asset_data.copy_to_id(dest) works regardless of whether dest is an asset or not, and it can only work when source already has asset data. This seemed like good preconditions to me. The alternative dest.asset_data.copy_from(source.asset_data) implies that dest would have to be marked as asset first, which might also involve rendering the preview image. That implication falls away when using direct assignment, dest.asset_data = source.asset_data. I have no strong feelings about this either, except that it would be a bit more work ;-)

For an RNA callback this has a bit too much knowledge/logic for my taste

Fair, I'll move the code to an ED_xxx function.

> "the right way" around The advantage of this approach is that `source.asset_data.copy_to_id(dest)` works regardless of whether `dest` is an asset or not, and it can only work when `source` already has asset data. This seemed like good preconditions to me. The alternative `dest.asset_data.copy_from(source.asset_data)` implies that `dest` would have to be marked as asset first, which might also involve rendering the preview image. That implication falls away when using direct assignment, `dest.asset_data = source.asset_data`. I have no strong feelings about this either, except that it would be a bit more work ;-) > For an RNA callback this has a bit too much knowledge/logic for my taste Fair, I'll move the code to an `ED_xxx` function.
Member

except that it would be a bit more work ;-)

Is it though? I think it's pretty much the same thing, just in a RNA setter for ID.asset_data.

> except that it would be a bit more work ;-) Is it though? I think it's pretty much the same thing, just in a RNA setter for `ID.asset_data`.
Sybren A. Stüvel added 1 commit 2023-06-05 14:19:21 +02:00
e7a21c6eda Review feedback
- Move functionality from RNA to ED
- Change from `src.asset_data.copy_to_id(dst)` function to direct
  assignment `dst.asset_data = src.asset_data`
Sybren A. Stüvel added 2 commits 2023-06-05 14:23:33 +02:00
9e1bcc75e2 Allow 'clear asset' by assigning `None`
`id.asset_data = None` is now a way to clear the 'is asset' status. This
makes the assignment symmetrical, as `id.asset_data = some_other_data` can
also be used to actually mark the ID as asset.
Sybren A. Stüvel added 1 commit 2023-06-05 14:24:25 +02:00
Member

Thinking some more about this, what I'm unsure about is if this way of "marking" or "clearing" an asset is future proof. What if this process involves more than just setting/getting the metadata? E.g. I could imagine we'd want to call some callback or app handler when marking/clearing an asset.

So we could say setting it to None is not valid, or setting it on an ID that is not marked as asset.

Thinking some more about this, what I'm unsure about is if this way of "marking" or "clearing" an asset is future proof. What if this process involves more than just setting/getting the metadata? E.g. I could imagine we'd want to call some callback or app handler when marking/clearing an asset. So we could say setting it to `None` is not valid, or setting it on an ID that is not marked as asset.
Sybren A. Stüvel added 2 commits 2023-06-06 17:47:52 +02:00
Author
Member

Agreed, I've updated the code, PR description, and attached blend file for this.

Agreed, I've updated the code, PR description, and attached blend file for this.
Julian Eisel approved these changes 2023-06-06 17:53:35 +02:00
Julian Eisel left a comment
Member

Nice, I think this is the way to go.

Nice, I think this is the way to go.
Sybren A. Stüvel manually merged commit 42fe8de6f9 into main 2023-06-08 10:15:03 +02:00
Sybren A. Stüvel deleted branch pr/asset-data-copy-to-id 2023-06-08 10:15:14 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#108547
No description provided.