Scene Based Event Plugins

March 18th, 2011 by Patrick Boucher - Viewed 5511 times -




Those of you who are long time readers of Softimage Blog (or XSIBlog way back when) might remember a 2006 article by Homam Bahnassi. In the article Homam describes a method to run code stored in annotations in the scene at arbitrary moments by selecting an object and manually invoking code execution.

Recently we had an issue where a single scene was acting wonky and for it to render we needed to coax it a bit into cooperating with a pre-render script. The production guys fixed their issue but it got me thinking and I decided it might be good to revisit Homam’s ideas but make it work with the siOnBeginSequence event… Or maybe even any event.

The idea is to have a single plugin that is extremely simple and that registers into the requested events. When the event is triggered, the plugin will inspect the scene and find any relevant annotation and execute it’s code. The plugin will find annotations who’s name matches the name of the event prefixed with run, runOnEndSceneSave for example.

The scene root is inspected for annotations as well as any other model no matter how deeply nested. Annotations’ code is executed depth first so if you had a scene who’s assets’ code needs to be run before scene code, you’re good to go. Finally, if any annotation’s execution fails, execution of all remaining annotations is halted; please debug your code.

The explorer screenshot shows an example where the scene contains code that will run when the scene is saved. The Model annotations that allows the runOnEndFileImport code to be run only once on initial model import. Let’s break that second example down a bit starting with the runOnEndFileImport code:

The flag1 parameter of the runOnEndFileImport is used to know if the code has already run once. The problem though, is that if you export the asset, it will export with the flag on and thus would not run on subsequent import. You could rely on the user to remove the flag on export but you could also implement two other event annotations: runOnBeginFileExport to unset the flag and runOnEndFileExport to switch it back on.

Have you noticed the Annotation object? Well, I figured that to add some context to the code in the annotation, on top of the standard Softimage objects (like Application), we could probably provide the actual annotation object that is being executed so you can then start rummaging in your scene at the appropriate location if needed.

Finally here is the Gist that implements the actual plugin that does the event registrations and annotation finding and execution. If you need this functionality on more or fewer events, just add or remove the appropriate ones in the ACTIVE_EVENTS list.

There are most certainly performance limitations to this that I haven’t investigated or measured. For instance, activating the siOnValueChange event and having tons of annotations in a large scene might not be a good idea.

Finally, if you’ve got a smarter way to find annotations under an X3DObject, please share!

Comments are closed.