WIP: Python API: Add link/append pre/post handlers #122365
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
9 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#122365
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "lichtwerk/blender:122357"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
These can be useful in many scenarios (e.g. handling local unique
identifiers).
This PR adds these handlers, whereas the PRE gives us the ID name that
is tried and the POST gives us the actual ID that was processed.
NOTE: !113658 is very similar (but tied to asset drag & drop), whereas
this PR is more general (these could probably live hand-in-hand / side-
by-side)
Resolves #122357
Code to test:
Can you kick off package build please?
CC @Mets
CC @Laurens-Corijn
CC @GregZaal
CC @iluvblender
@blender-bot package
Package build started. Download here when ready.
One thing to note is that when appending, the linking callback will also fire (at least in the current state of the PR) as internally linking and appending are very similar (appending is more or less just linking followed by making local...).
I guess it would be possible to change that (so the linking callback does not fire when appending, not totally sure if we want this though).
Quite sure this is useful, and long requested.
The main question for me is when exactly the callbacks should fire. I guess the post callbacks should fire only after data is instantiated, and after versioning. So really at the end of
BKE_blendfile_append()
/BKE_blendfile_link()
. Technically operators may still perform further work as part of the linking/appending, but practically I think this would be fine.Might also be worth noting in the API documentation that users can link/append multiple IDs at once, and that this will fire the callbacks for each.
Awesome, will use this for sure.
Now .. for handlers on_delete_pre and on_delete_post ;)
It's great to see somebody working on this, I think this could already result in a lot of nice QoL stuff for productions. But I did run into some issues pretty quickly! I hope they won't be too much of a pain to iron out.
Run per operation vs per ID
I think the former is better. If user wants to loop through all the dependent IDs, they can use some extra code:
Pass context?
Can and should bpy.context be passed to the handler? I don't really know when it is and when it isn't safe to reach out for the global variable. I just know that global variables make me uneasy.
For example, to make an add-on that saves you the hassle of overriding linked assets, one needs to pass a desired scene and view layer to the
override_hierarchy_create
function, which are pretty much only possible to grab from the context, or by looping and searching all scenes and viewlayers for ourid_linked
.Crash on override_hierarchy_create
One obvious use case would be to immediately create overrides after linking an asset (a collection), which would be neat since each team could create overrides the way they want. But for me, this is crashing, even with a simple asset and without referencing bpy.context:
Asset (Cube with 1 bone in a collection): cube_asset.blend
Reference to the instancer empty?
When linking in a collection, Blender automatically creates an Empty for us in the active scene and view layer, and sets its
instance_collection
to the linked collection. Right now, this seems to happen afterlink_post
, which makes it impossible to operate on this instancer, eg. to delete it. I think that's bad.Reviewing this implementation however I think a different approach is worth investigating (I'll propose this separately).
Generally useful functionality although I think it would be good to have concrete examples of what this would be used for, otherwise it's difficult to evaluate different solutions.
Checking on this PR.
The 2 letter ID codes are part of the ID name (used in the "pre" callback). As far as I'm aware this is currently an internal detail that is never exposed to the Python API and intentionally so. Including these in names means we'll have to document them as part of the Python API. Avoiding this likely involves extending callbacks.
A possible "simple" solution could be to construct a string that replaced the ID-code with the plural of the ID type (see
BKE_idtype_idcode_to_name_plural
). e.g."collections:Collection Name"
instead of"GRCollecton Name"
, then the first part of the name can be used to do look-ups onbpy.data
for example.The post callbacks look to run too early (before versioning for example). As far as I can see these should run last to avoid handlers interfering with link/append logic as well as potentially not having access to the "final" state which scripts may require - depending on the use-case.
There should be a
{append/link}_post_fail
handler, meaning apre
callback may run without any post callback which can make the state difficult to manage.Having
pre/post
callbacks run on an each ID means script authors can't deal with with the "result" of appending ID's (at least it's quite difficult).This could be resolved with handlers for the append/link action:
Although it still seems clunky, a workaround for only supporting primitives arguments to handlers.
At least it allows script authors to collect data and operate on it.
Suggestion for an alternative approach
BlendfileLinkAppendContext
to{append/link}_{pre/post}
which are only called once (not for each ID).BlendfileLinkAppendContextItem
.scene
,view_layer
,3D viewport
... (seeLibraryLink_Params
).Pros:
id_type
&name
).Cons:
This addresses some of the concerns by @Mets, I'd be interested in what @mont29 thinks of this.
Thank you for checking on this and for the very helpful feedback!
On first sight, the suggested alternative approach looks very attractive, will investigate this first (since it would solve most of the other issues mentioned).
I also like @ideasman42 suggestion a lot. I don't think exposing the
BlendfileLinkAppendContext
system is a bad thing, this has been very stable and reliable since its introduction many years ago, so I would not expect any drastic changes in it any time soon.Bonus point, the
BlendfileLinkAppendContextItem
items know whether they were explicitly linked/appended, or as a dependency (LINK_APPEND_TAG_INDIRECT
tag).My list of use cases that I'd like to implement one day:
Sorry this has been on hold (it might be on hold a bit longer even, but still targeting 4.3..., will mark as WIP for now)
(just a note to myself: since
83373002d2
anda473107489
, #124794 might be relevant?)Python API: Add link/append pre/post handlersto WIP: Python API: Add link/append pre/post handlersIf you go through RNA system, #124794 should not be relevant here. We already have quite a lot of non-POD C++ data exposed through RNA.
Just wanted to chime in that with this being treated as a prereq for the asset browser pre+post drop hooks, having both of these features would unlock asset browser integration for a couple different addon projects I'm involved in, which so far have been stuck with custom rolled asset browsing experiences which predate the native Blender asset browser.
Most of it is to do with addon-managed objects/materials loaded from disk, and needing the addon to modify properties or configurations post import based on current scene settings, or to enable special redo last settings specific to the addon/asset (which could be triggered an operator call directly from the post import hook, even though that's a little hacky).
Checkout
From your project repository, check out a new branch and test the changes.