[Bf-committers] bpy callback patch (replace scriptlink like functionality from 2.4x)

Campbell Barton ideasman42 at gmail.com
Fri Jun 24 18:46:12 CEST 2011


@Bartius
The kinds of callbacks to implement is a tricky subject which is why I
didn't add many to begin with.
2.4x had a FrameChange scriptlink which people used to do all sorts of
things - but Ton would like to use the depsgraph instead but this
needs to be looked into further and means no FrameChange callback for
now (besides there is a driver hack you can use :S).

Brecht was worried about OnLoad callbacks being abused when really we
want to know if the scene is changed or updated, so he suggests to
have a scene update callback which seems like a good idea.

The are a lot more fine grained callbacks we _could_ have like on
adding/removing datablocks, or on library linking, on object update.
but I'd like to start with a basic set and make sure its ok with other
devs before adding more because if this is poorly done I think it can
be a big problem for us later on.

@Lorenzo,
I was thinking of this too, basically how you remove or identify them
once added.

There are a few ways this could work, a dict would be nice except they
are unordered and its nice to have callbacks execute in the order they
are added.
Python provides OrderedDict but this is written in python and quite
high level, it can work but I rather not. There may also be 2 scripts
that happen to add a callback of the same name so its not necessarily
a good thing.

* note, renamed bpy.app.callbacks to bpy.app.handlers *

The functions themselves have a name so you can always do..
bpy.app.handlers.render_post[:] = filter(lambda f: f.__name__ !=
"func_to_remove", bpy.app.handlers.render_post)

if having an elegant way to do this is important we can subclass the
list in C and add some methods
bpy.app.handlers.render_post.remove_func("func_to_remove")
bpy.app.handlers.render_post.append_func(new_func)  # replaces existing


@Bassam,
It would most likely work but I'm not sure about this now.

On Fri, Jun 24, 2011 at 4:12 PM, Bassam Kurdali
<bkurdali at freefactory.org> wrote:
> Will it be possible to set callbacks e.g. for transforms? i.e. if a
> bone/face/vert moves in edit mode?
>
> On Fri, 2011-06-24 at 09:02 -0700, Lorenzo Pierfederici wrote:
>> Hi Campbell,
>>
>> maybe the callbacks could have a name associated with them when registered,
>> like:
>> bpy.app.callbacks.render_post.append('my_callback', func)
>>
>> That would make it easier to remove them from a script, and would probably
>> make things easier in a gui, too.
>>
>> cheers,
>> lorenzo
>>
>> On Fri, Jun 24, 2011 at 6:27 AM, Campbell Barton <ideasman42 at gmail.com>wrote:
>>
>> > This is something thats been requested to be brought back from 2.4x
>> > for quite a while,
>> > however I wasn't that happy with scriptlinks having to be setup per
>> > datablock, then scanning every datablock on frame change to see if any
>> > have frame change scriptlinks for example - not too efficient and
>> > cumbersome to manage scripts attached to each datablock.
>> >
>> > The method I'm proposing is to have scripts register functions with an
>> > event, example.
>> >
>> > def func(context, scene): print("Render", scene)
>> > bpy.app.callbacks.render_post.append(func)
>> >
>> > This way text's with 'Register' enabled can setup the event handlers
>> > when loading the file.
>> >
>> > There are are of course cases where you might want to have a callbacks
>> > associated with a specific datablock which this method doesn't deal
>> > with.
>> > instead you'd need to filter the data you want to operate on within
>> > the callback - IMHO its just a trade-off between the 2 methods.
>> >
>> > Heres the patch which has some more details, only render and load
>> > pre/post callbacks are done.
>> > I'll add more if this general method is acceptable.
>> >
>> >
>> > http://projects.blender.org/tracker/index.php?func=detail&aid=27752&group_id=9&atid=127
>> >
>> > --
>> > - Campbell
>> > _______________________________________________
>> > Bf-committers mailing list
>> > Bf-committers at blender.org
>> > http://lists.blender.org/mailman/listinfo/bf-committers
>> >
>> _______________________________________________
>> Bf-committers mailing list
>> Bf-committers at blender.org
>> http://lists.blender.org/mailman/listinfo/bf-committers
>
>
> _______________________________________________
> Bf-committers mailing list
> Bf-committers at blender.org
> http://lists.blender.org/mailman/listinfo/bf-committers
>



-- 
- Campbell


More information about the Bf-committers mailing list