2017-06-22 01:43:08 -07:00
|
|
|
import bpy
|
2017-06-29 17:46:08 -07:00
|
|
|
import logging
|
|
|
|
from multiprocessing import Process, Pipe
|
2017-06-22 01:43:08 -07:00
|
|
|
from . import blenderpack
|
|
|
|
|
2017-06-29 17:46:08 -07:00
|
|
|
class SubprocessOperatorMixin:
|
|
|
|
timer = None
|
|
|
|
|
|
|
|
# run once in invoke
|
|
|
|
def setup(self):
|
|
|
|
pass
|
|
|
|
# run on receipt of data from subprocess
|
|
|
|
def handle_response(self, resp):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.pipe = Pipe()
|
|
|
|
self.subprocess = None
|
|
|
|
|
|
|
|
def execute(self, context):
|
|
|
|
return self.invoke(context, None)
|
|
|
|
|
|
|
|
def invoke(self, context, event):
|
|
|
|
self.subprocess.start()
|
|
|
|
# we have to explicitly close the end of the pipe we are NOT using,
|
|
|
|
# otherwise no exception will be generated when the other process closes its end.
|
|
|
|
self.pipe[1].close()
|
|
|
|
|
|
|
|
wm = context.window_manager
|
|
|
|
wm.modal_handler_add(self)
|
|
|
|
self.timer = wm.event_timer_add(.01, context.window)
|
|
|
|
|
|
|
|
self.setup()
|
|
|
|
|
|
|
|
return {'RUNNING_MODAL'}
|
|
|
|
|
|
|
|
def modal(self, context, event):
|
|
|
|
print("polling")
|
|
|
|
try:
|
|
|
|
if self.pipe[0].poll():
|
|
|
|
newdata = self.pipe[0].recv()
|
|
|
|
else:
|
|
|
|
newdata = None
|
|
|
|
except EOFError:
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
if newdata is not None:
|
|
|
|
self.handle_response(newdata)
|
|
|
|
return {'PASS_THROUGH'}
|
|
|
|
|
|
|
|
class PACKAGE_OT_fetch(SubprocessOperatorMixin, bpy.types.Operator):
|
|
|
|
bl_idname = "package.fetch"
|
2017-06-22 01:43:08 -07:00
|
|
|
bl_label = "Update package list(s)"
|
|
|
|
|
2017-06-29 17:46:08 -07:00
|
|
|
def __init__(self):
|
|
|
|
SubprocessOperatorMixin.__init__(self)
|
|
|
|
settings = bpy.context.window_manager.package_manager_settings
|
|
|
|
self.subprocess = Process(target=blenderpack.fetch, args=(settings.url, self.pipe))
|
|
|
|
|
|
|
|
def handle_response(self, resp):
|
|
|
|
print("your response:", resp)
|
|
|
|
|
2017-06-22 01:43:08 -07:00
|
|
|
def execute(self, context):
|
|
|
|
return {'FINISHED'}
|
|
|
|
|
|
|
|
|
|
|
|
def register():
|
2017-06-29 17:46:08 -07:00
|
|
|
bpy.utils.register_class(PACKAGE_OT_fetch)
|
2017-06-22 01:43:08 -07:00
|
|
|
|
|
|
|
def unregister():
|
2017-06-29 17:46:08 -07:00
|
|
|
bpy.utils.unregister_class(PACKAGE_OT_fetch)
|
2017-06-22 01:43:08 -07:00
|
|
|
|