ayon-core icon indicating copy to clipboard operation
ayon-core copied to clipboard

Pre and post loader hooks

Open kalisp opened this issue 7 months ago • 10 comments

Changelog Description

This PR introduces new concept of Pre/Post Loader Hooks which could be used by to enhance or modify loading processes without need of modifying official loaders.

The idea is for custom addons to be able to implement their own PreLoadHookPlugin | PostLoadHookPlugin which would be triggered before/after Loader action.

Additional info

Each addon must register its folder with pre/post hooks like this:

    register_loader_pre_hook_plugin_path(loader_hook_dir)
    register_loader_post_hook_plugin_path(loader_hook_dir)

I am testing it with this dummy custom addon my_custom_addon-1.0.0.zip

Testing notes:

  1. start with this step
  2. follow this step

kalisp avatar Apr 03 '25 16:04 kalisp

I wonder if new hook plug-ins here make more sense than just an EventSystem 😕 Seems somewhat overkill here.

BigRoy avatar Apr 03 '25 21:04 BigRoy

I wonder if new hook plug-ins here make more sense than just an EventSystem 😕 Seems somewhat overkill here.

Had the same question, but they should be blocking, I dont think you can achieve that with events.

kalisp avatar Apr 04 '25 09:04 kalisp

I wonder if new hook plug-ins here make more sense than just an EventSystem 😕 Seems somewhat overkill here.

Had the same question, but they should be blocking, I dont think you can achieve that with events.

The Event system in AYON is blocking by default, right @iLLiCiTiT ?

BigRoy avatar Apr 04 '25 15:04 BigRoy

The Event system in AYON is blocking by default, right @iLLiCiTiT ?

Yes it is. But it matters what should happen during the hooks, and what is the reason/use-case to add the hooks?

Because adding hooks (2 classes) to the current crap load api means that we will have to add more backwards compatibility with more classes to be duplicated when the api changes, which will complicate it.

iLLiCiTiT avatar Apr 07 '25 08:04 iLLiCiTiT

I wanted to ask very similar questions to @BigRoy 's but I wanted to wait for this first https://github.com/ynput/ayon-core/pull/1222#issuecomment-2782443206 . We really need to know what is the use-case. It's all related to current crap api. Loader plugins are also "actions" (delivery etc.), so I guess the pre/post hook needs to know "some" context, but because current api does not actually tell you anything about "if", "where" or "what" actually happened it's just a random classes with functions that do get triggered in various occations that probably won't be important for the hooks.

iLLiCiTiT avatar Apr 07 '25 11:04 iLLiCiTiT

Summoning @antirotor to describe more details about https://github.com/ynput/ayon-core/issues/928

kalisp avatar Apr 07 '25 12:04 kalisp

Summoning @antirotor to describe more details about #928

Clients are changing some behaviour of some loaders. This should help them stay "vanilla" and still add some custom behaviour. For example - you load layout into your scene and you want to immediately lock all transforms so artist cannot accidentally move stuff around. All logic for the loader is the same, you just want to execute few lines of code after the load is complete.

There are also often customization about hierarchy where loaded stuff should end up - if you want to customize this - to add loaded model into very specific structure, again - the whole loader logic is the same, you just need to move loaded items somewhere.

antirotor avatar Apr 14 '25 15:04 antirotor

There are also often customization about hierarchy where loaded stuff should end up - if you want to customize this - to add loaded model into very specific structure, again - the whole loader logic is the same, you just need to move loaded items somewhere.

So how will these changes help if you don't know what happened in load?

iLLiCiTiT avatar Apr 23 '25 07:04 iLLiCiTiT

Also important note is that this PR is adding 2 new classes and at about 10 new functions just to register and discover them, changing api functions for load. All of that will make backwards compatibility even harder when we begin with new load api.

iLLiCiTiT avatar May 07 '25 08:05 iLLiCiTiT

We should move with this forward. So, what about one class for hooks (PrePostLoadHook) with pre_load and post_load methods - hook it on loader class name and monkey-patch loader .load() so it executes those hook methods before and after? Make it as simple as possible.

So how will these changes help if you don't know what happened in load?

You can go around by listing the scene in pre_load and post_load and you'll get what loader pulled in and then you can manipulate the result.

antirotor avatar Jun 09 '25 15:06 antirotor

Vastly simplified logic.

Currently monkey patched method is added to Loader classes who are compatible to any registered PrePostLoaderHookPlugin during discover_loader_plugins.

kalisp avatar Jun 13 '25 15:06 kalisp

@kalisp please re-request my review when you think it's ready for a hopefully final review.

I personally think having remove update and switch access would be very nice.


ALSO, have we considered making them context managers? So that we CAN also get to the post hook if e.g. update failed? E.g. there may definitely be cases where you may want to 'wrap' something up that you've done before - even if the core logic failed in-between?

(The question then is if the 'exit' should be ordered in reverse, so that the outer context manager exits last?) ❓ But maybe this is thinking too much of it.

BigRoy avatar Jun 17 '25 09:06 BigRoy

Implemented update, remove methods.

Solved all other comments.

kalisp avatar Jun 17 '25 12:06 kalisp

Implemented update, remove methods.

Yeah, you are right, it was broken, I misread log and got confused its working on my dummy test. I re tested (and this time double checked myself).

Thanks for the updates, they seem valid :).

kalisp avatar Jun 18 '25 09:06 kalisp

Ok, I might be just down beause I had to recently remove all uses of TypedDict from my code because we still support Python 3.7. I believe str | None is working only for python 3.10+ so I guess this will kill any older Maya than 2024 (Maya 2023 has 3.9)? Sorry. I would like to use newer syntax so much...

I didn't notice that. Good point.

iLLiCiTiT avatar Jun 19 '25 08:06 iLLiCiTiT

Reordered arguments in pre_, post_ methods.

kalisp avatar Jun 19 '25 09:06 kalisp

It is mergable, from my point of view the abstract methods are unnecessary, but I won't be the one who will use it.

iLLiCiTiT avatar Jun 19 '25 10:06 iLLiCiTiT