def load(filename): '''Load the scene contained in the file with the provided name. The root node is returned Currently the supported formats are psl, pslx, pslp. ''' filename = os.path.abspath(filename) dirname = os.path.dirname(filename) with SetPath(dirname): f = open(filename).read() ast=[] if filename.endswith(".psl"): ast = pslparserhjson.parse(f) elif filename.endswith(".pslx"): ast = pslparserxml.parse(f) elif filename.endswith(".pslp"): ast = pslparserpickled.parse(f) if len(ast) == 0: rootNode = Sofa.createNode("root") Sofa.msg_error(rootNode, "The file '"+filename+"' does not contains valid PSL content.") return rootNode directives = preProcess(ast[0][1]) if not directives["version"] in ["1.0"]: rootNode = Sofa.createNode("root") Sofa.msg_error(rootNode, "Unsupported PSLVersion"+str(directives["version"])) return rootNode g=globals() g["__file__"]=filename ret = pslengine.processTree(ast, directives, g) return ret return None
def load(filename): '''Load the scene contained in the file with the provided name. The root node is returned Currently the supported formats are psl, pslx, pslp. ''' filename = os.path.abspath(filename) dirname = os.path.dirname(filename) with SetPath(dirname): f = open(filename).read() ast = [] if filename.endswith(".psl"): ast = pslparserhjson.parse(f) elif filename.endswith(".pslx"): ast = pslparserxml.parse(f) elif filename.endswith(".pslp"): ast = pslparserpickled.parse(f) if len(ast) == 0: rootNode = Sofa.createNode("root") Sofa.msg_error( rootNode, "The file '" + filename + "' does not contains valid PSL content.") return rootNode directives = preProcess(ast[0][1]) if not directives["version"] in ["1.0"]: rootNode = Sofa.createNode("root") Sofa.msg_error( rootNode, "Unsupported PSLVersion" + str(directives["version"])) return rootNode g = globals() g["__file__"] = filename ret = pslengine.processTree(ast, directives, g) return ret return None
def processNode(parent, key, kv, stack, frame, doCreate=True): global templates, aliases stack.append(frame) populateFrame(key, frame, stack) if doCreate: if parent == None: tself = Sofa.createNode("undefined") else: tself = parent.createChild("undefined") frame["self"] = tself ### Force all the data field into a non-persistant state. for datafield in tself.getListOfDataFields(): datafield.setPersistant(False) for link in tself.getListOfLinks(): link.setPersistant(False) else: tself = frame["self"] = parent try: if isinstance(kv, list): for key, value in kv: sofaAliasInitialName = None if isinstance(key, unicode): key = str(key) if key in sofaAliases: sofaAliasInitialName = key key = sofaAliases[key] if key in aliases: key = aliases[key] if key == "Import": n = processImport(tself, key, value, stack, {}) elif key == "Node": n = processNode(tself, key, value, stack, {}) elif key == "Python": processPython(tself, key, value, stack, {}) elif key == "properties": processProperties(tself, key, value, stack, {}) elif key == "Template": tself.addObject( processTemplate(tself, key, value, stack, {})) elif key == "Using": processAlias(tself, key, value, stack, frame) elif key in sofaComponents: o = processObject(tself, key, value, stack, {}) if o != None: if isinstance(sofaAliasInitialName, str): Sofa.msg_warning( o, pslprefix + "'" + key + " was created using the hard coded alias '" + str(sofaAliasInitialName) + "'" + ". \nUsing hard coded aliases is a confusing practice and we advise you to use scene specific alias with the Alias keyword." ) elif key in templates: instanciateTemplate(tself, key, value, stack, frame) else: ## we are on a cache hit...so we refresh the list. refreshComponentListFromFactory() if key in sofaComponents: o = processObject(tself, key, value, stack, {}) if o != None: tself.addObject(o) processParameter(tself, key, value, stack, frame) else: raise Exception("This shouldn't happen, expecting only list") except Exception, e: s = SofaPython.getSofaFormattedStringFromException(e) Sofa.msg_error(tself, "Problem while loading file. <br>" + s)
def processNode(parent, key, kv, stack, frame, doCreate=True): global templates, aliases stack.append(frame) populateFrame(key, frame, stack) if doCreate: if parent == None: tself = Sofa.createNode("undefined") else: tself = parent.createChild("undefined") frame["self"] = tself ### Force all the data field into a non-persistant state. for datafield in tself.getListOfDataFields(): datafield.setPersistant(False) for link in tself.getListOfLinks(): link.setPersistant(False) else: tself = frame["self"] = parent try: if isinstance(kv, list): for key,value in kv: sofaAliasInitialName = None if isinstance(key, unicode): key = str(key) if key in sofaAliases: sofaAliasInitialName = key key = sofaAliases[key] if key in aliases: key = aliases[key] if key == "Import": n = processImport(tself, key, value, stack, {}) elif key == "Node": n = processNode(tself, key, value, stack, {}) elif key == "Python": processPython(tself, key, value, stack, {}) elif key == "properties": processProperties(tself, key, value, stack, {}) elif key == "Template": tself.addObject( processTemplate(tself, key, value, stack, {}) ) elif key == "Using": processAlias(tself, key,value, stack, frame) elif key in sofaComponents: o = processObject(tself, key, value, stack, {}) if o != None: if isinstance(sofaAliasInitialName, str): Sofa.msg_warning(o, pslprefix+"'"+key+" was created using the hard coded alias '"+str(sofaAliasInitialName)+"'"+". \nUsing hard coded aliases is a confusing practice and we advise you to use scene specific alias with the Alias keyword.") elif key in templates: instanciateTemplate(tself, key,value, stack, frame) else: ## we are on a cache hit...so we refresh the list. refreshComponentListFromFactory() if key in sofaComponents: o = processObject(tself, key, value, stack, {}) if o != None: tself.addObject(o) processParameter(tself, key, value, stack, frame) else: raise Exception("This shouldn't happen, expecting only list") except Exception,e: s=SofaPython.getSofaFormattedStringFromException(e) Sofa.msg_error(tself, "Problem while loading file. <br>"+s)