def __init__(self, **kwargs): """Creates a new instance of NimbleResponseData.""" NimbleData.__init__(self, **kwargs) self.response = kwargs.get('response', NimbleResponseData.SUCCESS_RESPONSE) self.error = kwargs.get('error', None) self.warnings = kwargs.get('warnings', None)
def __init__(self, **kwargs): """Creates a new instance of NimbleResponseData.""" NimbleData.__init__(self, **kwargs) if 'response' in kwargs: self._response = kwargs['response'] else: self._response = NimbleResponseData.SUCCESS_RESPONSE if 'error' in kwargs: self._error = kwargs['error'] else: self._error = None
def runPythonScript(self, script, **kwargs): return self._send( NimbleData(kind=DataKindEnum.PYTHON_SCRIPT, payload={ 'script': script, 'kwargs': kwargs }))
def runPythonScriptFile(self, path, **kwargs): return self._send( NimbleData(kind=DataKindEnum.PYTHON_SCRIPT_FILE, payload={ 'path': path, 'kwargs': kwargs }))
def runMayaCommand(self, command, *args, **kwargs): return self._send( NimbleData(kind=DataKindEnum.MAYA_COMMAND, payload={ 'command': str(command), 'kwargs': kwargs, 'args': args }))
def _createMessage(self): """Doc...""" d = NimbleData._createMessage(self) d['response'] = self.response if self.error: d['error'] = self.error return d
def runCommand(self, command, *args, **kwargs): return self._send( NimbleData(kind=DataKindEnum.COMMAND, payload={ 'command': command.toDict() if isinstance( command, ImportedCommand) else str(command), 'kwargs': kwargs, 'args': args }))
def _createMessage(self): """Doc...""" d = NimbleData._createMessage(self) d['response'] = self.response if self.error: d['error'] = self.error if self.warnings: d['warnings'] = self.warnings return d
def runPythonImport(self, modulePackage, methodName=None, className=None, runInMaya=None, **kwargs): """ Executes the specified import through Nimble in the specified run mode. modulePackage: (String) An absolute (dot-syntax) formatted import to the module you wish to be executed. This module will be imported by Maya and must be on its sys.path. [methodName]: (String) An optional function name to be executed within the module. If a class name is specified this method will be called on an instance of the specified class. If no class name is specified the method will be called directly on the module. [className]: (String) An optional class name of a class to import within the specified module. The class will be imported from the module and instantiated. [runInMaya]: If True the import will be executed within Maya. If False the import will be executed outside of Maya on the remote end of the Nimble connection. The default value of None will use the current global setting, which can be set by the nimble.enablePythonTestMode() top-level function and defaults to runInMaya = True, i.e. test mode is disabled. Returns a NimbleResponseData object with the results of the script execution. """ payload = { 'module': modulePackage, 'method': methodName, 'class': className, 'kwargs': kwargs } if NimbleEnvironment.inMaya(): return MayaRouter.runPythonImport(payload) if (not NimbleEnvironment.TEST_REMOTE_MODE ) if runInMaya is None else runInMaya: return self._send( NimbleData(kind=DataKindEnum.PYTHON_IMPORT, payload=payload)) else: return MayaRouter.runPythonImport(payload)
def echo(self, message): return self._send( NimbleData(kind=DataKindEnum.ECHO, payload={'echo': message}))
def _parseData(self, message, logLevel): cd = NimbleData.fromMessage(message) self._logData(cd, logLevel) return cd
def _sendRemote(self, nimbleData): responseFlags = 0 message = u'' retry = NimbleEnvironment.REMOTE_RETRY_COUNT while retry > 0: try: self.open() except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to open connection', err ] retry -= 1 if retry == 0: if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None continue try: serialData = nimbleData.serialize() except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to serialize data for transmission', err ] if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None try: self._chunk.clear() self._chunk.writeUint32(NimbleEnvironment.CONNECTION_FLAGS) self._chunk.writeString(serialData + NimbleEnvironment.TERMINATION_IDENTIFIER) self._socket.sendall(self._chunk.byteArray) except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to send data', err ] self.close() retry -= 1 if retry == 0: if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None continue try: self._chunk.clear() b = SocketUtils.receiveInChunks( self._socket, chunkSize=NimbleEnvironment.SOCKET_RESPONSE_CHUNK_SIZE) self._chunk.writeString(b) self._chunk.position = 0 responseFlags = self._chunk.readUint32() message = StringUtils.strToUnicode(self._chunk.read(-1)) # Break while loop on successful reading of the result if message is not None: break except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Unable to read response', err) self.close() return None try: if not (responseFlags & ConnectionFlags.KEEP_ALIVE): self.close() except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Unable to close connection', err) try: return NimbleData.fromMessage(message) except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Response data parsing failure', err) return None
def runMayaCommandBatch(self, commandList): return self._send( NimbleData(kind=DataKindEnum.MAYA_COMMAND_BATCH, payload={'commands': commandList}))
def runMelScript(self, script): return self._send( NimbleData(kind=DataKindEnum.MEL_SCRIPT, payload={'script': script}))
def _sendRemote(self, nimbleData): responseFlags = 0 message = u'' retry = NimbleEnvironment.REMOTE_RETRY_COUNT while retry > 0: try: self.open() except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to open connection', err ] retry -= 1 if retry == 0: if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None continue try: serialData = nimbleData.serialize() except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to serialize data for transmission', err ] if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None try: self._chunk.clear() self._chunk.writeUint32(NimbleEnvironment.CONNECTION_FLAGS) self._chunk.writeString( serialData + NimbleEnvironment.TERMINATION_IDENTIFIER) self._socket.sendall(self._chunk.byteArray) except Exception as err: failure = [ '[ERROR | NIMBLE COMMUNICATION] Unable to send data', err ] self.close() retry -= 1 if retry == 0: if not nimble.quietFailure: NimbleEnvironment.logError(failure[0], failure[1]) return None continue try: self._chunk.clear() b = SocketUtils.receiveInChunks( self._socket, chunkSize=NimbleEnvironment.SOCKET_RESPONSE_CHUNK_SIZE) self._chunk.writeString(b) self._chunk.position = 0 responseFlags = self._chunk.readUint32() message = StringUtils.strToUnicode(self._chunk.read(-1)) # Break while loop on successful reading of the result if message is not None: break except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Unable to read response', err) self.close() return None try: if not (responseFlags & ConnectionFlags.KEEP_ALIVE): self.close() except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Unable to close connection', err) try: return NimbleData.fromMessage(message) except Exception as err: if not nimble.quietFailure: NimbleEnvironment.logError( '[ERROR | NIMBLE COMMUNICATION] Response data parsing failure', err) return None
def _parseData(self, message, logLevel): data = NimbleData.fromMessage(message) self._logData(data, logLevel) return data
def ping(self, message=None): """Doc...""" return self._send( NimbleData(kind=DataKindEnum.PING, payload={'msg': message}))
def addToMayaPythonPath(self, path): return self._send( NimbleData(kind=DataKindEnum.ADD_SYSTEM_PATH, payload={'path': path}))
break except Exception, err: print '[ERROR] Nimble communication failure: Unable to read response' print err self.close() return None try: self.close() except Exception, err: print '[ERROR] Nimble communication failure: Unable to close connection' print err try: return NimbleData.fromMessage(message) except Exception, err: print 'Nimble communication data failure.' print err return None #=================================================================================================== # I N T R I N S I C #___________________________________________________________________________________________________ __del__ def __del__(self): try: self._socket.close() except Exception, err: pass