2
$\begingroup$

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"
sys.path.append(absolutePath)

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")
        os.chdir(absolutePath)
        self.port = 1301

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

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

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

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


bpy.utils.register_class(socketModal)
bpy.ops.object.modal_operator('INVOKE_DEFAULT')

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 ?

$\endgroup$
  • 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 Apr 19 '17 at 10:37
  • $\begingroup$ Thanks a lot ,That fixes the issue. But now the operator freezes blender. why is that happening ? $\endgroup$ – B0rn2C0de Apr 20 '17 at 8:25
  • $\begingroup$ Where have you add the handler exactly? and what is traced on the console? $\endgroup$ – lemon Apr 20 '17 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 Apr 20 '17 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 Apr 20 '17 at 8:55
0
$\begingroup$

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'}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.