Page 1 of 2

Drop callbacks to execute Python function

Unread postPosted: Mon Dec 10, 2018 8:27 pm
by Braden99
It would be great if Clarisse had drop callbacks to execute Python code, for example the user dropped a string on to the Clarisse Image or 3d viewport.

This is pseudo code

Code: Select all
def my_drop_handler(data, *args, **kwargs):
    if data and isinstance(data, basestring):
        import my_asset_module
        result = my_asset_module.add_asset(str(data))
        return bool(result)
    return False

ix.api.callbacks.Callbacks.imageViewportDrop.connect(my_drop_handler)
ix.api.callbacks.Callbacks.3dViewportDrop.connect(my_drop_handler)


The string dropped could be for example some custom pipeline identifier of object and version (or just a file path).

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 8:23 am
by Braden99
It would also be very useful to have callbacks to run after a particular item creation.

This is pseudo code

Code: Select all
def image_created_callback(image_item, *args, **kwargs):
    if not image_item:
        return False
    import image_setup
    image_item.setup(image_item)
    return True

ix.api.callbacks.Callbacks.addCallback('ImageCreated', image_created_callback)

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 9:49 am
by sam
Hi there,

This has been already discussed in the past. While I understand how cool that can be for a TD, I'm personally against this feature which is why it hasn't been implemented yet. One main reason besides the fact that there no persistent Python host in Clarisse:

Attaching Python callbacks to Clarisse evaluation engine would cripple Clarisse because of the GIL (Python can't be executed in parallel YET so that would potentially slowdown dramatically Clarisse dependency graph) Ultimately, attaching any Python code to things that are called many times in Clarisse is a very bad idea. This is the reason why for example we went down the road of SEExpr for Clarisse Expression language coming in 4.0. Honestly I wasn't a big fan of integrating a completely new language to Clarisse (we have Python, OSL and now SEExpr) but for performance reasons we had no other choices. Moreover, SEExpr is pretty simple to learn while very powerful.

All this to say that until the GIL issue isn't solved in Python, this is not likely to happen.

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 10:52 am
by Braden99
Thanks Sam. Obviously any major slow down in Clarisse evaluation engine should be avoided.

However I'm not really sure how they get around exposing Python callbacks in other software. For example.
https://help.autodesk.com/cloudhelp/201 ... backs.html
https://learn.foundry.com/nuke/develope ... backs.html
https://learn.foundry.com/katana/dev-gu ... vents.html

The callbacks I'm envisaging would run rarely and it would be up to the TD to be responsible for performance implications.

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 12:16 pm
by sam
They do because I guess the dependency graph is not evaluated in parallel ;). If they are, then each time there's a python code attached then there's a big global lock to avoid having Python interpreter executing code in parallel (it would crash otherwise).

For example in Clarisse even attributes values (driven by expression) are lazily evaluated in parallel.

But I guess that if the callbacks are limited to object creation and destruction we could get our way around. Indeed, as the evaluation DG is the same as the interactive one we already solved this case in way. The thing is what kind of callbacks would you like to see? If they are very limited to a few things (such as object creation,destruction, renaming etc...) then we may do something about it.

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 6:58 pm
by Braden99
For me running a callback at object creation is all that is required. But yes at deletion and rename could also be useful.

Also providing a mechanism of triggering some Python code when a string identifier is dropped on to Clarisse app. For example a user might drag and drop a pipeline asset or texture on to various native Clarisse windows, which would be encoded as identifier string. The callback would use this to execute some Python code (create Clarisse item and set attrs for example).

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 7:47 pm
by sam
I see. Well adding these kind of callbacks could be feasible in the future.

I think the callback to execute python code on drop is already a feature request but can't find the tracking ID.

Re: Drop callbacks to execute Python function

Unread postPosted: Tue Dec 11, 2018 8:36 pm
by Braden99
Also on a somewhat related note. Is it possible to currently add a custom button to any Item (for example Image or Layer3d), and for it to execute some Python code. This could be used for example to add a custom pipeline 'Render' button to these items, or 'Register' and so fourth. I had a look a while ago, and it doesn't seem possible, even though there is some API that sounds promising.

Re: Drop callbacks to execute Python function

Unread postPosted: Wed Dec 12, 2018 7:27 am
by sam
Unfortunately this is only possible in C++ for now. While you can add new attributes in Python, you can't add an attribute of type action to an existing class. This is the feature request #6138 FYI

Re: Drop callbacks to execute Python function

Unread postPosted: Thu May 09, 2019 12:37 am
by mati
Hi. Was a feature request submitted for the Python callbacks idea? I don't see it in the new feature request tracker. Wanted to express support for it or create a request.