Author Topic: Scripting : jsonServerPort  (Read 2068 times)

I'm building some tools for my pipeline. I've started with the main.qml template. How do I send commands to jsonServerPort? I've set the port number, but I'm not sure how to send requests.

Good Question I'm facing the same issue here. Would be nice if the whole API could be more documented.

I'm afraid I don't know.
I will reach out to the dev and see with them if we can put in place more documentation about it.

--------

Meanwhile I can share a few insight :

1 - You need to open a TCP port on the jsonServerPort (example : 5000) and then send your commands as JSON with the following format :
Code: [Select]
{
  js: "alg.log.info('Hello World');"
}


2 - Here is an example of python script that dialog with the remote system :
Code: [Select]
import base64
import json
import sys
if sys.version_info >= (3, 0):
    import http.client as http
else:
    import httplib as http

###############################################################################################
# Const data
###############################################################################################

# Json server connection
_PAINTER_ROUTE = '/run.json'
_HEADERS = {'Content-type': 'text/plain', 'Accept': 'application/json'}


###############################################################################################
# Exceptions
###############################################################################################

# Generic exception on the Painter class
class PainterError(Exception):
    def __init__(self, message):
        super(PainterError, self).__init__(message)


class ExecuteScriptError(PainterError):
    def __init__(self, data):
        super(PainterError, self).__init__('An error occured when executing script: {0}'.format(data))


###############################################################################################
# Remote Substance Painter control
###############################################################################################

class Painter:
    def __init__(self, port=60041, host='localhost'):
        self._host = host
        self._port = port

    # Execute a HTTP POST request to the Substance Painter server and send/receive JSON data
    def _jsonPostRequest(self, route, body):
        connection = http.HTTPConnection(self._host, self._port, timeout=3600)
        connection.request('POST', route, body, _HEADERS)
        response = connection.getresponse()

        data = json.loads(response.read().decode('utf-8'))
        connection.close()

        if 'error' in data:
            raise ExecuteScriptError(data['error'])
        return data

    def checkConnection(self):
        connection = http.HTTPConnection(self._host, self._port)
        connection.connect()

    # Execute a JavaScript script
    def execScript(self, script):
        main = base64.b64encode(script.encode('utf-8'))
        return self._jsonPostRequest(_PAINTER_ROUTE, ('{"js":"' + main.decode('utf-8') + '"}').encode('utf-8'))
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Froyok aka Fabrice Piquet, Technical Artist and Product Designer at Allegorithmic.

There it is!! WooHoo! Thanks @Froyok!

+1 Thanks Froyok.  It would be great to get this example in the docs--would have saved me a bunch of time digging.