Page MenuHome

Bake callback events and python handlers
Needs RevisionPublic

Authored by Élie Michel (elie) on Jul 12 2018, 1:55 AM.

Details

Summary

As pointed out by felixg.david on Right-Click Select, there is no mean to attach python callbacks to baking events, while it is possible for render jobs using bpy.app.handlers.

This patch introduces and exposes a bake_init, a bake_complete and a bake_cancel handler lists.

I did not add the other handlers available for render because I am not really sure of which are still relevant for baking. I'd like to be sure at first that I fired the events from the right place (namely RT_Database_Baking, in render/intern/source/convertblender.c).

PS: This is my first submission, so any methodological advice is more than welcome. :)

Diff Detail

Event Timeline

Élie Michel (elie) edited the summary of this revision. (Show Details)Jul 12 2018, 10:57 AM
Brecht Van Lommel (brecht) requested changes to this revision.Jul 12 2018, 11:29 AM

I'm not sure how this will help with baking a sequence of images, I would expect that requires calling the bake operator multiple times instead. But it's useful functionality regardless.

If you add init and complete then cancel is needed as well, so that cleanup can be performed even if the bake is cancelled.

This is a patch for Blender Internal which is being removed in blender2.8, instead it should be implemented for Cycles and multires baking. See source/blender/editors/object/object_bake.c, (multires)bake_startjob and (multires)bake_freejob.

This revision now requires changes to proceed.Jul 12 2018, 11:29 AM

Added support for multires baking and CANCEL event.

I initially tried to fire events (ie call BLI_callback_exec) only from the RE_* functions, because that's how it is done for rendering-related events. And I found it cleaner since it would ensure that even if baking functions are called from somewhere else than the object bake operator, the events would still be fired. At least it ensures that they are fired both when executing and invoking the bake operators.

But a lot of the baking mechanism, especially for multires baking, is coded directly within the operator, so it became easier to put the calls to BLI_callback_exec. I also had to add a pointer to bmain, scene and stop in the MultiresBakeJob struct. Events are fired from (multires)bake_startjob and (multires)bake_freejob, as you advised, as well as from multiresbake_image_exec_locked and bake_image_exec for when the operator is executed (startjob/freejob are only called in the case of invoked operator).

Now, you right: this is for blender internal only. I just understood that cycles baking is handled by the object_bake_api.c file, and not object_bake.c where I've been working so far. I'm going to address this in my next update.

Élie Michel (elie) edited the summary of this revision. (Show Details)Jul 13 2018, 2:18 AM

And here is the support for cycles renders! Much more straight forward once I got how all of this is organized. :)

So, at this point the main thing I am not so confident in is the way I detect whether the job has been canceled or not. Is seems to be hacky sometimes, and a different hack for all cases.

PS: As for felixg.david, I believe he intends to shift to the next frame and call the bake() op every time the previous bake completed

Félix (Miadim) accepted this revision.Jul 13 2018, 3:51 PM
Félix (Miadim) added a subscriber: Félix (Miadim).
This comment was removed by Félix (Miadim).
Félix (Miadim) requested changes to this revision.Jul 17 2018, 2:59 PM
This comment was removed by Félix (Miadim).
This revision now requires changes to proceed.Jul 17 2018, 2:59 PM

Hi, I did some mistakes with revisions, I'm not used to Phabricator.

Anyway, as I resquested this implementation, I've done my add-on based on the diff @Élie Michel (elie) has submitted, you can find it here : https://gitlab.com/Tilix4/bake_sequence

It's very useful for me and I hope it'll be added in the 2.8 realease !