Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the ability to connect to multiple Nuke instances. Also ensured…

… that the thread is a daemon, to stop it being caught by Nuke 6.2 on quit
  • Loading branch information...
commit b5b6a8a97ce4565a71816e07d8d83f0bcc3f721e 1 parent 3bd6728
@HughMacdonald HughMacdonald authored
Showing with 39 additions and 13 deletions.
  1. +15 −6 nukeCommandClient.py
  2. +24 −7 nukeCommandServer.py
View
21 nukeCommandClient.py
@@ -9,21 +9,30 @@ class NukeConnectionError(StandardError):
pass
class NukeConnection:
- def __init__(self, host = "localhost"):
+ def __init__(self, host = "localhost", instance = 0):
self._objects = {}
self._functions = {}
self._host = host
+ start_port = 54200 + instance
+ end_port = 54300
+ self._port = self.find_connection_port(start_port, end_port)
- if not self.test_connection():
+ if self._port == -1:
raise NukeConnectionError("Connection with Nuke failed")
-
- def send(self, data):
- port = 54261
+
+ def find_connection_port(self, start_port, end_port):
+ for port in range(start_port, end_port + 1):
+ self._port = port
+ if self.test_connection():
+ return port
+ return -1
+
+ def send(self, data):#
size = 1024 * 1024
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((self._host, port))
+ s.connect((self._host, self._port))
s.send(data)
result = s.recv(size)
s.close()
View
31 nukeCommandServer.py
@@ -8,22 +8,39 @@
listTypes = [list, tuple, set, frozenset]
dictTypes = [dict]
-def nuke_command_server():
- t = threading.Thread(None, NukeInternal)
- t.start()
+class NukeConnectionError(StandardError):
+ pass
+def nuke_command_server():
+ t = threading.Thread(None, NukeInternal)
+ t.setDaemon(True)
+ t.start()
+
class NukeInternal:
def __init__(self):
self._objects = {}
self._next_object_id = 0
host = ''
- port = 54261
+ start_port = 54200
+ end_port = 54300
backlog = 5
size = 1024 * 1024
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind((host, port))
+ bound_port = False
+ for port in range(start_port, end_port + 1):
+ try:
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ print "Trying port %d" % port
+ s.bind((host, port))
+ bound_port = True
+ break
+ except Exception, e:
+ pass
+
+ if not bound_port:
+ raise NukeConnectionError("Cannot find port to bind to")
+
s.listen(backlog)
while 1:
@@ -84,7 +101,7 @@ def get(self, data_string):
elif data['action'] == "setattr":
setattr(obj, params[0], params[1])
elif data['action'] == "getitem":
- # If we're actually getting from vars(), then raise NameError instead of KeyError
+ # If we're actually getting from globals(), then raise NameError instead of KeyError
if data['id'] == -1 and params not in obj:
raise NameError("name '%s' is not defined" % params)
result = obj[params]
Please sign in to comment.
Something went wrong with that request. Please try again.