3
$\begingroup$

I started writing a plugin for Blender that accepts Open Sound Control messages for direct manipulation of data in realtime. I ran into a snag that made me uneasy about releasing it: when I close Blender, it crashes because the thread is still open and running. So I have to either force quit Blender or Ctrl-C from the command line to kill it.

How do I correctly catch Blender closing so I can cleanly run the command to stop the process running on another thread?

Here's a sample of the code:

def register():
    bpy.utils.register_class(OSCPanel)

    disp = dispatcher.Dispatcher()
    disp.map("/*", oscHandler)

    BpyOSCServer = osc_server.ThreadingOSCUDPServer(("0.0.0.0", 60004), disp)
    server_thread = threading.Thread(target=BpyOSCServer.serve_forever)
    server_thread.start()
    print('BpyOSC registered!')

def unregister():
    bpy.utils.unregister_class(OSCPanel)
    print('Stopping server')
    BpyOSCServer.shutdown()
    print('BpyOSC unregistered!')
$\endgroup$
8
  • $\begingroup$ I would try and share some code so others can see how you're doing things. $\endgroup$ May 18 '16 at 14:27
  • $\begingroup$ Maybe kill the thread using the methods outlined here blender.stackexchange.com/questions/39768/… $\endgroup$
    – batFINGER
    May 18 '16 at 15:57
  • 1
    $\begingroup$ @batFINGER Oh, maybe I misunderstood that register an unregister execute when you start and quit blender respectively. $\endgroup$ May 19 '16 at 13:49
  • 1
    $\begingroup$ Oh, just noticed your code, register and unregister methods are used to make the addon and its classes known to blender, and to clean up when unregistered. IMO not the place to put in server code. Are you familiar with the update callback on a say BoolProperty? giving an easy way to make a simple button to START server when True, and STOP when False? $\endgroup$
    – batFINGER
    May 19 '16 at 14:08
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because this is a programming question about threading, and it's not specific to Blender. This question is an EXCELLENT candidate for migration to StackOverflow $\endgroup$
    – Matt
    May 19 '16 at 19:26

Your Answer

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

Browse other questions tagged or ask your own question.