I want to receive real time data into blender based on which I would control some animation. To receive the real time data I am using a datagram socket with a modal operator. But for some reason the modal function of the operator is never getting called

Here is the code for my operator

import bpy
import socket
import os
import sys

absolutePath = "/Users/pulkitjuneja/Documents/projects/EVA/scripts"

from animationController import main

class socketModal(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Lip synch Operator"

    def __init__(self):
        print("Listen Start")
        self.port = 1301

    def __del__(self):
        print("Listen End")

    def execute(self, context):
        return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.bind(("", self.port))
        print('socket listening on port', self.port)
        return {'RUNNING_MODAL'}

    def modal(self, context, event):
        data = None
            data = self.socket.recv(1024 * 1024)
         except socket.error:
            print('no data')
        if event.type == 'BACK_SLASH':
            return {'FINISHED'}
        return {'RUNNING_MODAL'}


I am running this script inside blender to register and run the operator but only the invoke function seems to be called. Is there anything I am doing wrong ?

  • 1
    $\begingroup$ The invoke function have to set the operator as modal adding a handler for it, with something like 'context.window_manager.modal_handler_add(self)' (see the corresponding code template in the UI) $\endgroup$
    – lemon
    Commented Apr 19, 2017 at 10:37
  • $\begingroup$ Thanks a lot ,That fixes the issue. But now the operator freezes blender. why is that happening ? $\endgroup$
    – B0rn2C0de
    Commented Apr 20, 2017 at 8:25
  • $\begingroup$ Where have you add the handler exactly? and what is traced on the console? $\endgroup$
    – lemon
    Commented Apr 20, 2017 at 8:33
  • $\begingroup$ i am just running this script through the python text editor in blender and then starting the operator using space bar in blender. The modal function runs repeatedly printing "here" but the blender interface freezes $\endgroup$
    – B0rn2C0de
    Commented Apr 20, 2017 at 8:48
  • $\begingroup$ Catch exception if there is no data. Something like "try: xxx.recv(size) etc" then "except: no data to receive". A code template here whitepythons.blogspot.fr/2014/05/… $\endgroup$
    – lemon
    Commented Apr 20, 2017 at 8:55

1 Answer 1


This is a bit late, but, at present, the modal operator will be invoked at every(!) blender event. This is perhaps not what you want, and could be the source of your problems. A better idea for such operators is to be restricted to particular events. For example, you could start a timer that triggers every 1 second:

bpy.context.window_manager.event_timer_add(1, bpy.context.window)

And, in your modal function, have:

def modal(self, context, event):
    if event.type == 'TIMER':
        # do something
    return {'RUNNING_MODAL'}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .