We need to run an xml rpc server on each blender session started. Blender communicates with our python 2.7 based pipeline through xml rpc. The xml rpc server will receive python 3.0 commands as strings set to the xml rpc server.
the wall we've been hitting are: -We need the script to be started from the same location on each blender workstation. -When we load the script as an addon, it attemps to spawn several servers on each session when we just need a single one.
Any help would be very welcome on: -How we can load a startup script for blender from a shared location for all users. -How we can make sure one single server is started within each session.
Please bellow the snippet code of what we are trying to get up and running on each blender session:
A Python 3.7 XMLRPC Server program in Blender
bl_info = {
"name": "XMLRPC Server",
"author": "KristenH",
"version": (0, 5, 0, 1),
"blender": (2, 79, 0),
"category": "System"
}
from xmlrpc.server import SimpleXMLRPCServer
import threading, time
from socketserver import ThreadingMixIn
import bpy
import sys
import socket
print()
from queue import Queue
# Simple server class
class SimpleServer(ThreadingMixIn, SimpleXMLRPCServer):
pass # The same statement as Null in C++, Java, C# etc.
# The command function
def command(com):
com='import bpy\n'+com
exec(com) # Trigger command to server
return com
# The server data
def server_data():
return bpy.app.version_string, bpy.context.blend_data.filepath, socket.gethostname()
def pscan(host='localhost', port=8000):
# Setting up a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxy = (host, port)
try:
con = sock.connect(proxy)
return True
except:
pass
class ServerThread(threading.Thread):
def __init__(self, host='localhost', port=8000):
self.proxy = (host, port)
self.server = SimpleServer(self.proxy)
self.port = port
self.host = host
threading.Thread.__init__(self)
# self.server_thread.setDaemon(True)
self.server.register_introspection_functions()
self.server.register_function(command, "command")
self.server.register_function(server_data, "server_data")
self.version_string = bpy.app.version_string
self., bpy.context.blend_data.filepath, socket.gethostname()
# A class shows a server panel in Blender
class ServerPanel(bpy.types.Panel):
bl_label = "Server Panel"
bl_idname = "Object_PT_server"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_category = "Shortcuts"
def draw(self, context):
layout = self.layout
row = layout.row()
server = None
x = 8000
attempts = 0
while(server is None):
con = pscan(port=x)
if con is not None:
print(con, 'found server on', 'localhost:',x)
row.label(text="Server found on: localhost:" + str(x) + "\n")
x+=1
else:
# time.sleep(5.0)
server = ServerThread(port=x)
# Make sure the first port is the start up port.
# row.label(text="Started the server with:" + str(server.host) + ":" + str(server.port) + "\n")
print(attempts, "Started the server with:", server.host, ":", server.port)
server.start()
# row.label(text="... Press Ctrl+C to exit\n")
#print("... Press Ctrl+C to exit")
attempts+=1
break
# Blender code below
def register():
bpy.utils.register_class(ServerPanel)
#bpy.utils.register_class(SimpleServer)
#bpy.utils.register_class(ServerThread)
def unregister():
bpy.utils.unregister_class(ServerPanel)
#bpy.utils.unregister_class(SimpleServer)
#bpy.utils.unregister_class(ServerThread)
if __name__ == "__main__":
register()
Many Thanks,
-H.
try..except
statement, use the client to test if the server is already running. If it succeeds, (obviously) don't start another server. If you reach theexcept
clause, start the server. $\endgroup$