Using KX_Scene.pre_draw or post_draw?

Hi All,

How do I use the KX_Scene.pre_draw and post_draw?
I assume these are the equivalent of handlers in Blender Internal that let python code run before the scene is drawn and after the scene is drawn.

The documentation states that this is just a list. How do I add the function I want to run to the list?


One can append the draw function once to the list, set the list to a list of the callback or (untested) make the list a set, and then add the callback (more expensive but you can’t add it more than once)

You would make a list consisting of the functions that you want to run. You can also use the OpenGL functions built into Blender’s Python API to draw things to the screen yourself. See Moguri’s BGUI module to see it in action, so to speak. As an example:

def Test():
    return 1

sce = logic.getCurrentScene()
sce.post_draw = [Test]

I think it’s better to do something like this:

def draw_callback():


By appending, your code will work better alongside any other code that wants to register a post or pre_draw callback. In other words, your code is less likely to break other people’s code. However, make sure you only append the function once! If you don’t, you’ll just build up a list full of your callback and call it a bunch of times every frame.

So, in short, appending plays nicer with other code, but can get you into trouble easier.

I finally got a change to play with these callbacks and they did not fire off as I expected.

I have a module running every frame. What I am looking for is a pre-module, post-module event intercept.
Or, considering SCA->, a pre-S, post-A style event system.

I have a print statement in each callback and here is what i see in my console.


The event flow I am looking for is…


I know there are reasons for the existing SCA flow and render loop, but is there any other way to get events in the proposed order I have listed above?

This is my understanding, but a pre- and post-module event intercept is not the purpose of pre_draw and post_draw, as far as I know. I believe one is supposed to use OpenGL (bgl) functions to draw things to the screen with these callbacks. Things drawn with pre_draw are drawn before the game scenes are drawn, and objects drawn with post_draw should be drawn afterward. At least, that is my understanding of it.

I agree with SolarLune. As far as I remember the purpose of these callbacks are to allow OpenGL draws to remain on screen as they were replaced by the render.

To set a “first running controller” you an use the priority switch (left of the [x]) of the controller.

To set a “last running” controller you can use the above callbacks. Be aware the callbacks are executed AFTER the actuators!

Thanks for the explanation. I am looking for a way to balance the load on my screenshot render engine.
For fullscreen renders I spend about 90%+ time in Logic. I am using GLSL shaders and I wondered if there was a way to make them run in a different space like rasterize? I thought callbacks might be just the thing.

true O_O!

I always thought that only increases the priority for that controller on the other controller of that same object

I was trying to use pre_draw / post_draw for this (but need some workaround)
this is really cool
(“discovery channel” to update :D)