How to remotely run a Python script in an existing Blender instance?

by mipmip   Last Updated July 12, 2018 06:15 AM

I don't want to use /Application.../blender -P my_script.py but I want to tell a running Blender process to execute a script.

Is there an existing method to tell a running Blender process to execute a python script?

For example using http://localhost:123/run_script=myscript.py

Tags : python scripting


Answers 3


I think this is a simple solution and avoids dependencies entirely.

https://github.com/zeffii/bpy_externall

Blender (Server)

Here's a small modal operator addon that works like this:

enter image description here

  • Adds a panel to the TextEditor, with a start / end button
  • when Start is pressed it
    • reads a file located at /path/whatever.io every n seconds.
      • If the file is empty we do nothing,
      • If the file contains a filepath (for instance to a python file) then the filepath is executed with Blender's python (as if it was running from the TextEditor). Once completed the operator will erase the contents of the file to indicate that it executed the path.
    • When End is pressed the modal operator stops reading that file location.

External Text Editor (Client)

The add-on includes an example of a SublimeText3 plugin that acts as a Client. All this 'Sender' plugin needs to do is write the filepath of the python file you want Blender to execute to /path/whatever.io

zeffii
zeffii
November 14, 2015 15:26 PM

This can be done with sockets, using the following examples you can run from the command line:

From the first terminal:

blender --python blender_server.py

From a second terminal:

python blender_client.py /path/to/myscript.py

This will execute /path/to/myscript.py in the first Blender instance.

You can send multiple scripts or run the client multiple times.

Note that this is a simple example, to get return codes in the client or make the port configurable... etc, this would have to be extended.

It could even be made to sent the entire script, or compressed Python byte-code over a network, none of this is especially hard. It just depends what you're after.


blender_server.py

# Script to run from blender:
#   blender --python blender_server.py

PORT = 8081
HOST = "localhost"
PATH_MAX = 4096


def execfile(filepath):
    import os
    global_namespace = {
        "__file__": filepath,
        "__name__": "__main__",
    }
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), global_namespace)


def main():
    import socket

    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind((HOST, PORT))
    serversocket.listen(1)

    print("Listening on %s:%s" % (HOST, PORT))
    while True:
        connection, address = serversocket.accept()
        buf = connection.recv(PATH_MAX)

        for filepath in buf.split(b'\x00'):
            if filepath:
                print("Executing:", filepath)
                try:
                    execfile(filepath)
                except:
                    import traceback
                    traceback.print_exc()


if __name__ == "__main__":
    main()

blender_client.py

#!/usr/bin/env python
# Script to send paths to run in blender:
#   blender_client.py script1.py script2.py

PORT = 8081
HOST = "localhost"

def main():
    import sys
    import socket

    clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    clientsocket.connect((HOST, PORT))

    for arg in sys.argv[1:]:
        clientsocket.sendall(arg.encode("utf-8") + b'\x00')


if __name__ == "__main__":
    main()
ideasman42
ideasman42
November 16, 2015 21:33 PM

I did ask a similar question, but as this one is way more popular so I guess that answer should be here. Question on command port

This usually is done with command port that listens for commands, but there's apparently no such thing in Blender. I've created an addon that implements this functionality. It can be found on github:

Kowalski Paweł
Kowalski Paweł
July 12, 2018 05:40 AM

Related Questions



How to create a Folder Dialog?

Updated April 04, 2015 21:06 PM

Mesh's material_index is an index into what?

Updated April 17, 2015 20:06 PM


OS cursor location in Blender Windows

Updated March 29, 2015 06:05 AM