[Bf-python] User interaction - method-based script links?

Jonathan Merritt j.merritt at pgrad.unimelb.edu.au
Sat Oct 30 03:24:44 CEST 2004


Hi Everyone,

Are there currently any thoughts about adding method-based callbacks as 
script links, in addition to the current script-based script links?

To explain what I mean (and why the heck I'm asking :-), I'll illustrate 
with an example...

I'm currently working on a script to interface Blender with a camera 
calibration kit (written in C by a guy called Reg Willson) that I've 
been using.  The script is coming along well, and should be releasable 
soon.  The general mode of operation is as follows:
    1. The user places known calibration empties in the Blender scene.
    2. User invokes the script.
    3. A button allows loading and display of an image from the camera 
to be calibrated.
    4. As the user selects empties from the scene, another button allows 
addition or removal of a calibration point in the image for the selected 
empty.  The calibration points are first placed on the image by 
cross-hairs and can then be translated using the standard G-drag-LMB 
process.
    5. If the user selects a camera, a button offering to calibrate it 
comes up.  This button exports data to the external calibration program, 
which is then invoked by Python, and the camera calibration data is read 
back and the camera adjusted.

The main problem in this script (quite unbelievably! :-) lies in 
detecting when the user selects empties and cameras in the scene.  
Currently, I am doing this as part of the event loop in the script by 
ensuring that the script window is re-drawn every time the 3D window is 
redrawn.  I accomplish this by creating my own new script-within-a-script:
    import Blender
    from Blender import Text
    from Blender import Scene
    txt = Text.New()
    txt.write('import Blender\n')
    txt.write('from Blender import Window\n')
    txt.write('Window.Redraw(Window.Types.SCRIPT)\n')
    Scene.GetCurrent().addScriptLink(txt.getName(), 'Redraw')
This approach has several obvious drawbacks (the new script is visible 
to the user, it can be left behind if the main script terminates 
abnormally, communication between the scripts is difficult, etc...).

What I basically need is a way to detect when the main selection set 
changes.  What is the preferred way to do this?  For the sake of speed 
and usability, I don't want the user to have to press yet another button 
to "acquire current empty", as I've seen in other scripts - I think this 
should be automatic.  If the script links method is the preferred way, 
then I think it should be possible to attach a method callback as a 
script link in addition to being able to attach entire scripts.  These 
method callbacks obviously wouldn't be visible to the user in the same 
way that normal script links are, but would solve many of the problems 
I'm having.  Something like this:
    import Blender
    from Blender import Scene
    def sceneRedrawn():
       """Do stuff here..."""
    Scene.GetCurrent().addCallback(sceneRedrawn, 'Redraw')

What do people think?

Thanks,

Jonathan Merritt.




More information about the Bf-python mailing list