Global events and break sequences?

Is it possible to get a script to listen to events outside it’s own window for example click events in the 3D window?

Also, is it possible to still listen for events when you are in a loop? I’m thinking about using the escape key as a break sequence but events seem to be ignored when the script is doing something. Would this need threading maybe?

Is it possible to get a script to listen to events outside it’s own window for example click events in the 3D window?

Short answer is no. This why SpaceHandlers were created.

You can think of each window ( a Space in Blender terms ) as its own little application displaying its own view of Blender’s internal database. These Spaces have their own state and event queues. It is considered very bad manners to mess with the events in another window. Hence, SpaceHandlers.

Yeah that makes sense but how for example do the shading, object and editing panels update when you click different objects in the 3D window? Are they constantly querying the internal database for a state change? I thought that maybe a selection action in the 3D window was performing a redraw but if so, it doesn’t redraw Python windows.

On the subject of the loop breaking, is there a function to read the event queue manually? I guess bevent tries to read the queue when it can but when a loop is running it doesn’t so can I explicitly force a loop to check the queue?

I tried threading but you can’t do anything to Blender from a separate thread - it just crashes Blender. For example, if you try to execute Blender.Window.RedrawAll() from a thread. I wasn’t expecting to be able to anyway since it wouldn’t make sense to be able to modify Blender via the GUI and via Python at the same time.

Can I put the bevent, event reader inside a thread?

Threading will let me check the internal database so I can get round the problem of reading actions in other windows and if I can read key events the same way then that would sort the problem.

how for example do the shading, object and editing panels update when you click different objects in the 3D window? Are they constantly querying the internal database for a state change?

Each of the panels is a separate editor or browser looking at the database. You can send a RedrawAll msg that tells everyone to update their view.

It has been a while since I looked, but IIRC, to enable threading in the python interpreter you need to do some special initialization and locking. (maybe compile with different setttings too)

Blender is essentially single-threaded. (yeah, I know about the rendering threads) If someone is going to read an event queue in the middle of a loop, it is going to be you.

There is event stuff in the Window module. Ton rightfully regards this module as Evil™. No guarantees it will survive the event recode. SpaceHandlers are the preferred way for dealing with events.

Ah, that’s probably why it doesn’t work properly. I was able to use thread code but even when it had an infinite run loop, it would drop out the thread when the code that initialized the thread dropped out.

I tried all sorts of ways but I still don’t have it yet. The API documentation isn’t all that helpful in places. How do you use Spacehandlers?

I tried using the Blender.event and the Blender.Window.QRead (I think this is one of the things you said not to use) and there was a point where the script was working sort of how I wanted. Whenever there was an event happening externally, it would notify the script but it seemed to be a glitch because I never got it to do it again.

Is there maybe an example script somewhere that uses spacehandlers?

Yeah Is There A SpaceHandler Documentation?

Is there space handler documentation? Anyone? How to get started developing them? Thanks!

click here and scroll down the page… that’s all there is. However, not all events are captured, as discussed in this thread