def __init__(self,varName,varType = None,value = None, defaultValue = None): """ Intializes an optionVar class handler Keyword arguments: varName(string) -- name for the optionVar varType(string) -- 'int','float','string' (default 'int') value() -- will attempt to set the optionVar with the value defaultValue() -- will ONLY use if the optionVar doesn't exist """ #Default to creation of a var as an int value of 0 ### input check self.name = varName self.form = '' self.value = '' #>>> If it doesn't exist, create, else update if not mc.optionVar(exists = self.name): if varType is not None: requestVarType = self.returnVarTypeFromCall(varType) elif defaultValue is not None: requestVarType = search.returnDataType(defaultValue) elif value is not None: requestVarType = search.returnDataType(value) else: requestVarType = 'int' if requestVarType: self.form = requestVarType self.create(self.form) if defaultValue is not None: self.initialStore(defaultValue) elif value is not None: self.initialStore(value) self.value = mc.optionVar(q=self.name) else: guiFactory.warning("'%s' is not a valid variable type"%varType) else: self.update(varType) #Attempt to set a value on call if value is not None: self.initialStore(value)
def update(self,varType = None): """ Update the data in case some other process has changed on optionVar """ dataBuffer = mc.optionVar(q=self.name) requestVarType = self.returnVarTypeFromCall(varType) if not mc.optionVar(exists = self.name): if requestVarType: self.form = requestVarType self.create(self.form) self.value = mc.optionVar(q=self.name) return else: return guiFactory.warning("'%s' is not a valid variable type"%varType) else: #If it exists, first check for data buffer typeBuffer = search.returnDataType(dataBuffer) or False if not typeBuffer: print'changing to int!' typeBuffer = 'int' if varType is not None: if typeBuffer == requestVarType: self.form = typeBuffer self.value = dataBuffer return else: self.form = requestVarType self.create(self.form) self.value = mc.optionVar(q=self.name) return else: self.form = typeBuffer self.value = mc.optionVar(q=self.name) return