[Bf-python] Using a Modal Operator and Nonblocking Sockets to Control Blender from an External Python Shell

Kabir Soorya phantom982002 at gmail.com
Thu Jul 5 05:37:04 CEST 2012


Hi,

I'm trying to find a good way to control Blender from an external Python
shell. I've spent a lot of time looking at past attempts and see that most
haven't gone anywhere, as Blender's architecture seems to make this
difficult. I tried to just have Python's InteractiveInterpreter class fed
from a socket, but unfortunately Blender locks up completely while
listening on the socket -- so it's not even able to come back into focus
when I Alt-Tab to it. That's not really ideal, and so after reading the
docs, it seems like the right way to do this is to have a modal operator on
an event timer repeatedly grab input from a nonblocking socket, and feed
*that* into InteractiveInterpreter. Unfortunately, the only code I've been
able to cobble together for event timers seems to make blender
unresponsive, and cause it to crash horribly. I've attached the code below,
if anyone knows what's going on -- or has a better way to connect Blender
to an external Python, I'd love to hear your thoughts.


import bpy

class ModalOperator(bpy.types.Operator):
    '''Operator which runs its self from a timer.'''
    bl_idname = "wm.modal_operator"
    bl_label = "Vertex Operator"

    _timer = None

    def modal(self, context, event):
        print(event.type)
        if event.type == 'ESC':
            print("FINISHED")
            context.window_manager.event_timer_remove(self._timer)
            return {'FINISHED'}

        if event.type == 'TIMER':
            self.execute(context)
            return {'RUNNING_MODAL'}

    def execute(self, context):
        print("EXC")
        bpy.ops.object.mode_set(mode='EDIT')
        context.window_manager.modal_handler_add(self)
        self._timer = context.window_manager.event_timer_add(0.1,
context.window)
bpy.utils.register_class(ModalOperator)
bpy.ops.wm.modal_operator('INVOKE_DEFAULT')
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20120704/1d05f33a/attachment.html>


More information about the Bf-python mailing list