def colorListCode(colorlist = None, referencedir = None, indentation = '\t'): init_txt = '' if not colorlist is None and len(colorlist) > 0: defaultlist = PglTurtle().getColorList() nbdefault = len(defaultlist) nbcurrent = len(colorlist) firstcol = True defaultmat = Material('default') printer = PyStrPrinter() printer.pglnamespace = 'pgl' printer.indentation = indentation printer.indentation_increment = '\t' printer.line_between_object = 0 if referencedir: printer.reference_dir = referencedir for i in range(nbcurrent): cmat = colorlist[i] if ( (i >= nbdefault) or (cmat.isTexture()) or (not cmat.isSimilar(defaultlist[i])) or (cmat.name != defaultlist[i].name)): if cmat.isTexture() or not cmat.isSimilar(defaultmat): if firstcol : init_txt += indentation+"import openalea.plantgl.all as pgl\n" firstcol = False cmat.name = 'Color_'+str(i) cmat.apply(printer) init_txt += printer.str() printer.clear() init_txt += indentation+'context.turtle.setMaterial('+repr(i)+','+str(cmat.name)+')\n' return init_txt
def interpret(self, inputmtg=None, turtle=None): if inputmtg is None: if self.currentmtg is None: self.init() mtg = self.currentmtg else: mtg = inputmtg if turtle is None: from openalea.plantgl.all import PglTurtle turtle = PglTurtle() turtle.start() rules = self.__get_rules('__isinterpretation__') def preorder(node): if node.edge_type() == '+': turtle.push() turtle.setId(node._vid) if rules.has_key(node.label): rules[node.label](node, turtle) def postorder(node): if node.edge_type() == '+': turtle.pop() nodes_forward_traversal(mtg, preorder, postorder) turtle.stop() return turtle
def sample(self): """ Reinitialize control to default value """ from openalea.plantgl.all import Material, PglTurtle value = PglTurtle().getColorList() return value
def sweepSymbol(path, section, length, dlength, radius=1, radiusvariation=None): from openalea.plantgl.all import PglTurtle t = PglTurtle() t.start() return t.startGC().sweep(path, section, length, dlength, radius, radiusvariation).stopGC().getScene()[0].geometry
def plot(self): g = self.g frames = self.frame edge_type = g.property('edge_type') p = PglTurtle() # p.startGC() root = g.roots_iter(scale=2).next() min_length = min([ f.length for f in frames.itervalues() if f.length and f.length > 0 ]) for vid in pre_order_turtle(g, root, p): down = frames[vid].down down = 0 if down is None else down l = max(frames[vid].length, min_length) if edge_type.get(vid) == '+': # 1. phi / # 2. insertion angle (+, -) # 3. F pid = g.parent(vid) radius = frames[vid].radius phi = frames[pid].rotate phi = 0 if phi is None else phi roll = frames[vid].roll roll = 0 if roll is None else roll p.push() p.scale(radius) p.rollL(phi) p.rollL(roll) p.down(down) p.F(l) else: radius = frames[vid].radius p.scale(radius) p.down(down) p.F(l) # p.stopGC() # Viewer.display(p.getScene()) return p.getScene()
def generate_json_parameter_dict(self): import openalea.plantgl.algo.jsonrep as jrep parameters = OrderedDict() for category in self.categories.values(): panel = category.info.copy() panel['name'] panel.setdefault('enabled',True) panel['scalars'] = [scalar.todict() for scalar in category.scalars.values()] items = [] for manager,obj in category.items.values(): items.append(manager.to_json(obj)) panel['items'] = items parameters[panel['name']] = panel defaultlist = PglTurtle().getColorList() materials = [] for i, cmat in self.color_list.items(): if not isSimilarToDefaultTurtleMat(cmat, i): jmat = jrep.to_json_rep(cmat) jmat['index'] = i materials.append(jmat) if self.animation_timestep is None: options = self.execOptions else: options = self.execOptions.copy() options['animation_timestep'] = self.animation_timestep options['default_category'] = self.default_category_name result = dict( schema = 'lpy', version = str(default_lpyjson_version), options = options, materials = materials, parameters = list(parameters.values()), credits = dict([(key,value) for key,value in self.credits.items() if value != '']) ) assert LsystemParameters.is_valid_schema(result) return result
def initialisationFunction(self,withall=True): header = "def "+LsysContext.InitialisationFunctionName+"(context):\n" init_txt = '' if withall: defaultlist = PglTurtle().getColorList() currentlist = self.lsystem.context().turtle.getColorList() nbdefault = len(defaultlist) nbcurrent = len(currentlist) firstcol = True defaultmat = Material('default') printer = PyStrPrinter() printer.pglnamespace = 'pgl' printer.indentation = '\t' printer.indentation_increment = '\t' printer.line_between_object = 0 if self.fname and len(self.fname) > 0: printer.reference_dir = os.path.abspath(os.path.dirname(self.getStrFname())) #print printer.reference_dir for i in xrange(nbcurrent): cmat = currentlist[i] if ( (i >= nbdefault) or (cmat.isTexture()) or (not cmat.isSimilar(defaultlist[i])) or (cmat.name != defaultlist[i].name)): if cmat.isTexture() or not cmat.isSimilar(defaultmat): if firstcol : init_txt += "\timport openalea.plantgl.all as pgl\n" firstcol = False cmat.name = 'Color_'+str(i) cmat.apply(printer) init_txt += printer.str() printer.clear() init_txt += '\tcontext.turtle.setMaterial('+repr(i)+','+str(cmat.name)+')\n' if not self.lsystem.context().is_animation_timestep_to_default(): init_txt += '\tcontext.animation_timestep = '+str(self.getTimeStep())+'\n' options = self.lsystem.context().options for i in xrange(len(options)): if not options[i].isToDefault(): init_txt += '\tcontext.options.setSelection('+repr(options[i].name)+','+str(options[i].selection)+')\n' if len(self.scalars): init_txt += '\tscalars = '+str([i.tostr() for i in self.scalars])+'\n' init_txt += '\tcontext["__scalars__"] = scalars\n' init_txt += '\tfor s in scalars:\n\t\tif not s[1] == "Category" : context[s[0]] = s[2]\n' def emptyparameterset(params): for panel,data in params: if len(data) > 0: return False return True if not emptyparameterset(self.visualparameters) : intialized_managers = {} panelid = 0 for panelinfo,objects in self.visualparameters: if panelinfo.get('active',True) or withall: for manager,obj in objects: if not intialized_managers.has_key(manager): intialized_managers[manager] = True init_txt += manager.initWriting('\t') init_txt += manager.writeObject(obj,'\t') init_txt += '\tpanel_'+str(panelid)+' = ('+repr(panelinfo)+',['+','.join(['('+repr(manager.typename)+','+manager.getName(obj)+')' for manager,obj in objects])+'])\n' panelid += 1 init_txt += '\tparameterset = [' panelid = 0 for panelinfo,objects in self.visualparameters: if panelinfo.get('active',True) or withall: init_txt += 'panel_'+str(panelid)+',' panelid += 1 init_txt += ']\n' if withall and self.keepCode_1_0_Compatibility: init_txt += '\tcontext["__functions__"] = [' for panelinfo,objects in self.visualparameters: if panelinfo.get('active',True): for manager,obj in objects: if manager.typename == 'Function': init_txt += '('+repr(manager.getName(obj))+','+manager.getName(obj)+'),' init_txt += ']\n' init_txt += '\tcontext["__curves__"] = [' for panelinfo,objects in self.visualparameters: if panelinfo.get('active',True): for manager,obj in objects: if manager.typename == 'Curve2D': init_txt += '('+repr(manager.getName(obj))+','+manager.getName(obj)+'),' init_txt += ']\n' init_txt += '\tcontext["__parameterset__"] = parameterset\n' for panelinfo,objects in self.visualparameters: if panelinfo.get('active',True): for manager,obj in objects: init_txt += '\tcontext["'+manager.getName(obj)+'"] = '+manager.writeObjectToLsysContext(obj) + '\n' if len(init_txt) > 0: return header+init_txt else: return ''
from .__lpy_kernel__ import LpyParsing, LsysContext, Lsystem from collections import OrderedDict default_credits = {'__authors__' : '' , '__institutes__' : '' , '__copyright__' : '' , '__description__' : '' , '__references__' : '' } default_lpycode_version = 1.2 default_lpyjson_version = 1.0 scalartypemap = { int : 'Integer', float :'Float', bool : 'Bool'} graphictypemap = { NurbsCurve2D : 'Curve2D', BezierCurve2D : 'Curve2D', Polyline2D : 'Curve2D', NurbsPatch : 'NurbsPatch'} defaultturtlecolorlist = PglTurtle().getColorList() def isSimilarToDefaultTurtleMat(cmat, i): if cmat.isTexture() : return False nbdefault = len(defaultturtlecolorlist) if i >= nbdefault: defaultmat = Material('Color_'+str(i)) else: defaultmat = Material(defaultturtlecolorlist[i]) if cmat.isSimilar(defaultmat) and cmat.name == defaultmat.name: return True else: return False
def interpret(self, instanciation=True, colorproperty=None, bounds=None, colormap='jet'): import openalea.lpy as lpy from openalea.plantgl.all import PglTurtle, Scene, Group, Shape print 'Instanciation :', instanciation def smb_interpret(cmd, turtle): ls = lpy.Lstring(cmd) lpy.turtle_interpretation(ls, turtle) def transf_interpret(cmd, smb, turtle): ls = lpy.Lstring(cmd) #print 'interpret', ls, cmd turtle.push() sc = lpy.turtle_partial_interpretation(ls, turtle) turtle.customGeometry(smb) turtle.pop() def sc2group(sc): if len(sc) == 1: return sc[0].geometry return Group([sh.geometry for sh in sc]) def color_scene(sc, color): from openalea.plantgl.all import Discretizer, Scene d = Discretizer() for sh in sc: sh.geometry.apply(d) tr = d.result tr.colorList = [color for i in xrange(len(tr.indexList))] tr.colorPerVertex = False sh.geometry = tr geommap = dict() invorderednodes = [] if colorproperty: from openalea.plantgl.scenegraph.colormap import PglColorMap if type(colorproperty) == str: cproperty = self.node_property(colorproperty) else: cproperty = colorproperty if bounds: cmap = PglColorMap(bounds[0], bounds[1], colormap) else: cmap = PglColorMap(min(cproperty.values()), max(cproperty.values()), colormap) for node in self.postorder_node_traversal(): t = PglTurtle() #print node smb_interpret(self.geom(node), t) if colorproperty: color_scene(t.getScene(), cmap(cproperty[node])) if self.nb_children(node) > 0: for cchild, edgecmds in self.geomcommands[node][1].items(): for edgecmd in edgecmds: transf_interpret( edgecmd, geommap[cchild] if instanciation else geommap[cchild].deepcopy(), t) smb = sc2group(t.getScene()) smb.name = 'Shape_' + str(node) geommap[node] = smb t = PglTurtle() if colorproperty: return Scene([Shape(geommap[self.root], t.getColorList()[1])]) + cmap.pglrepr() else: return Scene([Shape(geommap[self.root], t.getColorList()[1])])
def sweepSymbol(self, path): from openalea.plantgl.all import PglTurtle t = PglTurtle() t.start() return t.startGC().sweep(path, self.section, self.length, self.dlength, self.radius, self.radiusvariation).stopGC().getScene()[0].geometry