def export_lpy_controls(controls, filename): """ :param controls: [('name', interface, value)] ex: [('i', IInt(min=0, max=100), 1)] """ from openalea.lpy.gui.objectmanagers import get_managers # from openalea.plantgl.oaplugins.interfaces import IMaterialList, IPatch, IQuantisedFunction, ICurve2D managers = get_managers() interface2manager = { "IPatch": managers["NurbsPatch"], "IQuantisedFunction": managers["Function"], "ICurve2D": managers["Curve2D"], } visuobjects = [] for name, interface, value in controls: manager = interface2manager.get(interface.__class__.__name__, None) if manager: print "Export " + name + " ..." value.name = str(name) visuobjects.append((manager, value)) visualparameters = ({}, visuobjects) from openalea.lpy.simu_environ import getInitialisationCode code = getInitialisationCode(visualparameters=[visualparameters]) f = open(filename, "w") f.write(code) f.close()
def export_lpy_controls(controls, filename): """ :param controls: [('name', interface, value)] ex: [('i', IInt(min=0, max=100), 1)] """ from openalea.lpy.gui.objectmanagers import get_managers #from openalea.plantgl.oaplugins.interfaces import IMaterialList, IPatch, IQuantisedFunction, ICurve2D managers = get_managers() interface2manager = { 'IPatch': managers['NurbsPatch'], 'IQuantisedFunction': managers['Function'], 'ICurve2D': managers['Curve2D'] } visuobjects = [] for name, interface, value in controls: manager = interface2manager.get(interface.__class__.__name__, None) if manager: print 'Export ' + name + ' ...' value.name = str(name) visuobjects.append((manager, value)) visualparameters = ({}, visuobjects) from openalea.lpy.simu_environ import getInitialisationCode code = getInitialisationCode(visualparameters=[visualparameters]) f = open(filename, 'w') f.write(code) f.close()
def load(self): """ Get control from project and put them into widgets """ self.clear() proj = self.session.project if not proj.control.has_key("color map"): proj.control["color map"] = self.colormap_editor.getTurtle( ).getColorList() if not proj.control["color map"]: proj.control["color map"] = self.colormap_editor.getTurtle( ).getColorList() i = 0 logger.debug("Load Control color map: %s " % str(proj.control["color map"])) for color in proj.control["color map"]: self.colormap_editor.getTurtle().setMaterial(i, color) i += 1 managers = get_managers() geom = [] scalars = [] for control in proj.control: logger.debug(str(proj.control[control])) if hasattr(proj.control[control], "__module__"): if proj.control[ control].__module__ == "openalea.oalab.control.picklable_curves": typename = proj.control[control].typename proj.control[control].name = str(control) manager = managers[typename] geom.append((manager, proj.control[control])) elif str(control) != "color map": scalars.append(proj.control[control]) elif str(control) != "color map": scalars.append(proj.control[control]) if geom is not list(): logger.debug("Load Control Geom: %s " % str(geom)) self.geometry_editor.setObjects(geom) if scalars is not list(): logger.debug("Load Control Scalars: %s " % str(scalars)) self.scalars_editor.setScalars(scalars)
def load(self): """ Get control from project and put them into widgets """ self.clear() proj = self.session.project if not proj.control.has_key("color map"): proj.control["color map"] = self.colormap_editor.getTurtle().getColorList() if not proj.control["color map"]: proj.control["color map"] = self.colormap_editor.getTurtle().getColorList() i = 0 logger.debug("Load Control color map: %s " % str(proj.control["color map"])) for color in proj.control["color map"]: self.colormap_editor.getTurtle().setMaterial(i, color) i += 1 managers = get_managers() geom = [] scalars = [] for control in proj.control: logger.debug(str(proj.control[control])) if hasattr(proj.control[control], "__module__"): if proj.control[control].__module__ == "openalea.oalab.control.picklable_curves": typename = proj.control[control].typename proj.control[control].name = str(control) manager = managers[typename] geom.append((manager, proj.control[control])) elif str(control) != "color map": scalars.append(proj.control[control]) elif str(control) != "color map": scalars.append(proj.control[control]) if geom is not list(): logger.debug("Load Control Geom: %s " % str(geom)) self.geometry_editor.setObjects(geom) if scalars is not list(): logger.debug("Load Control Scalars: %s " % str(scalars)) self.scalars_editor.setScalars(scalars)
def translate_obj(fname): import os from os.path import join, splitext, basename from openalea.lpy.gui.objectmanagers import get_managers if os.path.isdir(fname): project = fname files = os.listdir(project) infos = {} for f in files: ext = splitext(f)[1] infos[ext] = infos.get(ext, []) + [f] lfile = infos.get('.l')[0] # lsystem file afile = infos.get('.a') # animation file vfiles = infos.get('.v') # visualisation file sfiles = infos.get('.s') # surface file fsetfiles = infos.get('.fset') # functions file funcfiles = infos.get('.func') # functions file csetfiles = infos.get('.cset') # contours file mapfiles = infos.get('.map') # color file matfiles = infos.get('.mat') # material file descfile = 'description.txt' # description file if not descfile in files: descfile = None if not lfile: return # read visual objects managers = get_managers() panels = [] # read surface file groupofpatches = {} if sfiles: for sfile in sfiles: sworkspace = splitext(basename(sfile))[0] surfaces = [] for manager in managers.itervalues(): fname = join(project, sfile) if manager.canImportData(fname): try: objects = manager.importData(fname) surfaces += [(manager, i) for i in objects] groupofpatches[sworkspace] = [ i.name for i in objects ] except Exception, e: import sys, traceback exc_info = sys.exc_info() traceback.print_exception(*exc_info) print 'Cannot import file ' + repr(sfile) break panels += [({'name': sworkspace}, surfaces)] # read func file if funcfiles: functions = [] for funcfile in funcfiles: for manager in managers.itervalues(): fname = join(project, funcfile) if manager.canImportData(fname): try: objects = manager.importData(fname) functions += [(manager, i) for i in objects] except Exception, e: import sys, traceback exc_info = sys.exc_info() traceback.print_exception(*exc_info) print 'Cannot import file ' + repr(funcfile) break panels += [({'name': 'functions'}, functions)]
def get_graphicalparameter_managers(self): from openalea.lpy.gui.objectmanagers import get_managers return get_managers()
def import_lpy_controls(filepath): if not path(filepath).isfile(): return control = dict() f = open(filepath, "r") script = f.read() f.close() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key("__lpy_code_version__"): lpy_code_version = context["__lpy_code_version__"] if context.has_key("__scalars__"): scalars_ = context["__scalars__"] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key("__functions__") and lpy_code_version <= 1.0: functions = context["__functions__"] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers["Function"] geoms += [(funcmanager, func) for func in functions] if context.has_key("__curves__") and lpy_code_version <= 1.0: curves = context["__curves__"] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers["Curve2D"] geoms += [(curvemanager, curve) for curve in curves] if context.has_key("__parameterset__"): for panelinfo, objects in context["__parameterset__"]: for typename, obj in objects: visualparameters.append((managers[typename], obj)) for scalar in scalars: control[unicode(scalar.name)] = scalar.value for (manager, geom) in geoms: if geom != list(): control[geom.getName()] = geom for (manager, geom) in visualparameters: if geom != list(): control[geom.getName()] = geom new_controls = [] for name, value in control.items(): interfaces = guess_interface(value) if interfaces: new_controls.append(Control(name, interfaces[0], value)) try: control["color map"] = to_color(context.turtle.getColorList()) except AttributeError: pass else: new_controls.append(Control("color map", "IColorList", control["color map"])) for control in new_controls: register_control(control)
def translate_obj(fname): import os from os.path import join, splitext, basename from openalea.lpy.gui.objectmanagers import get_managers if os.path.isdir(fname): project = fname files = os.listdir(project) infos = {} for f in files: ext = splitext(f)[1] infos[ext] = infos.get(ext, []) + [f] lfile = infos.get(".l")[0] # lsystem file afile = infos.get(".a") # animation file vfiles = infos.get(".v") # visualisation file sfiles = infos.get(".s") # surface file fsetfiles = infos.get(".fset") # functions file funcfiles = infos.get(".func") # functions file csetfiles = infos.get(".cset") # contours file mapfiles = infos.get(".map") # color file matfiles = infos.get(".mat") # material file descfile = "description.txt" # description file if not descfile in files: descfile = None if not lfile: return # read visual objects managers = get_managers() panels = [] # read surface file groupofpatches = {} if sfiles: for sfile in sfiles: sworkspace = splitext(basename(sfile))[0] surfaces = [] for manager in managers.itervalues(): fname = join(project, sfile) if manager.canImportData(fname): try: objects = manager.importData(fname) surfaces += [(manager, i) for i in objects] groupofpatches[sworkspace] = [i.name for i in objects] except Exception, e: import sys, traceback exc_info = sys.exc_info() traceback.print_exception(*exc_info) print "Cannot import file " + repr(sfile) break panels += [({"name": sworkspace}, surfaces)] # read func file if funcfiles: functions = [] for funcfile in funcfiles: for manager in managers.itervalues(): fname = join(project, funcfile) if manager.canImportData(fname): try: objects = manager.importData(fname) functions += [(manager, i) for i in objects] except Exception, e: import sys, traceback exc_info = sys.exc_info() traceback.print_exception(*exc_info) print "Cannot import file " + repr(funcfile) break panels += [({"name": "functions"}, functions)]
def import_lpy_controls(filepath): if not path(filepath).isfile(): return control = dict() f = open(filepath, 'r') script = f.read() f.close() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key('__lpy_code_version__'): lpy_code_version = context['__lpy_code_version__'] if context.has_key('__scalars__'): scalars_ = context['__scalars__'] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key('__functions__') and lpy_code_version <= 1.0: functions = context['__functions__'] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers['Function'] geoms += [(funcmanager, func) for func in functions] if context.has_key('__curves__') and lpy_code_version <= 1.0: curves = context['__curves__'] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers['Curve2D'] geoms += [(curvemanager, curve) for curve in curves] if context.has_key('__parameterset__'): for panelinfo, objects in context['__parameterset__']: for typename, obj in objects: visualparameters.append((managers[typename], obj)) for scalar in scalars: control[unicode(scalar.name)] = scalar.value for (manager, geom) in geoms: if geom != list(): control[geom.getName()] = geom for (manager, geom) in visualparameters: if geom != list(): control[geom.getName()] = geom new_controls = [] for name, value in control.items(): interfaces = guess_interface(value) if interfaces: new_controls.append(Control(name, interfaces[0], value)) try: control["color map"] = to_color(context.turtle.getColorList()) except AttributeError: pass else: new_controls.append( Control("color map", 'IColorList', control["color map"])) for control in new_controls: register_control(control)
def import_lpy_file(script): """ Extract from an "old style" LPy file script part (str) and associated control (dict). Permit compatibility between LPy and OALab. :param: script to filter (str) :return: lpy script (str) without end begining with "###### INITIALISATION ######" and a dict which contain the control (dict) """ control = dict() if script is None: script = "" beginTag = LpyParsing.InitialisationBeginTag if not beginTag in script: return str(script), control else: txts = str(script).split(beginTag) new_script = txts[0] context_to_translate = txts[1] context = Lsystem().context() context.initialiseFrom(beginTag + context_to_translate) managers = get_managers() visualparameters = [] scalars = [] functions = [] curves = [] geoms = [] lpy_code_version = 1.0 if context.has_key('__lpy_code_version__'): lpy_code_version = context['__lpy_code_version__'] if context.has_key('__scalars__'): scalars_ = context['__scalars__'] scalars = [ProduceScalar(v) for v in scalars_] if context.has_key('__functions__') and lpy_code_version <= 1.0: functions = context['__functions__'] for n, c in functions: c.name = n functions = [c for n, c in functions] funcmanager = managers['Function'] geoms += [(funcmanager, func) for func in functions] if context.has_key('__curves__') and lpy_code_version <= 1.0: curves = context['__curves__'] for n, c in curves: c.name = n curves = [c for n, c in curves] curvemanager = managers['Curve2D'] geoms += [(curvemanager, curve) for curve in curves] if context.has_key('__parameterset__'): for panelinfo, objects in context['__parameterset__']: for typename, obj in objects: visualparameters.append((managers[typename], obj)) control["color map"] = context.turtle.getColorList() for scalar in scalars: control[unicode(scalar.name)] = scalar for (manager, geom) in geoms: if geom != list(): new_obj, new_name = geometry_2_piklable_geometry(manager, geom) control[new_name] = new_obj for (manager, geom) in visualparameters: if geom != list(): new_obj, new_name = geometry_2_piklable_geometry(manager, geom) control[new_name] = new_obj return new_script, control