2
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Sorry but I couldn't get a right way to display our code, please find it in the next comment, sorry about that. $\endgroup$
    – rabicula
    Commented Jan 21, 2019 at 14:36
  • $\begingroup$ Perhaps before starting the server, start a client. In a 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 the except clause, start the server. $\endgroup$
    – unutbu
    Commented Jan 21, 2019 at 16:06
  • $\begingroup$ We already have a function that tests if the server has started. The thing we are after is to have the service starting one single time. The way it's written, it keeps starting over and over and keeps spawning servers on available ports. Thank you, -H. $\endgroup$
    – rabicula
    Commented Jan 21, 2019 at 16:13
  • $\begingroup$ To make it simple, we are after a python formulation that would trigger a function one time upon startup and that doesn't keep looping afterwards. Thank you. $\endgroup$
    – rabicula
    Commented Jan 21, 2019 at 16:16

1 Answer 1

1
$\begingroup$

Spawn the server from the register function, not inside ServerPanel.draw:

import multiprocessing as mp
import socket
import sys
import bpy
import time
import threading
from xmlrpc.server import SimpleXMLRPCServer
import xmlrpc.client
bl_info = {
    "name": "XMLRPC Server",
    "author": "KristenH",
    "version": (0, 5, 0, 1),
    "blender": (2, 79, 0),
    "category": "System"
}


HOST = 'localhost'
PORT = 8000

def command(com):
    com = 'import bpy\n'+com
    exec(com)
    return com

def server_data():
    return bpy.app.version_string, bpy.context.blend_data.filepath, socket.gethostname()


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"
    proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")

    def draw(self, context):
        layout = self.layout
        version_string, filepath, hostname = self.proxy.server_data()
        print('found server on', 'localhost:', hostname)
        layout.row().label(text="Server found on: {}:{}".format(hostname,PORT))
        layout.row().label(text="Press Ctrl-C in terminal to exit".format(hostname,PORT))        

def register():
    global thread
    proc = mp.Process(target=maybe_launch_server)
    proc.daemon = True
    proc.start()
    bpy.utils.register_class(ServerPanel)


def unregister():
    bpy.utils.unregister_class(ServerPanel)


def maybe_launch_server():
    proxy = xmlrpc.client.ServerProxy("http://localhost:8000/")
    try:
        result = proxy.server_data()
    except ConnectionRefusedError:
        server = SimpleXMLRPCServer((HOST, PORT))
        server.register_introspection_functions()
        server.register_function(command, "command")
        server.register_function(server_data, "server_data")
        version_string = bpy.app.version_string
        print("Started the server with:", HOST, ":", PORT)
        server.serve_forever()


if __name__ == "__main__":
    register()

This spawns the server as a separate process. proc.daemon = True causes the process to terminate when the calling process terminates. On the plus side, this means that the server will terminate when you quit blender. On the negative side, it also means you have to pay attention to which blender started the server (in case you launch multiple blenders).

$\endgroup$
0

You must log in to answer this question.

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