Skip to content

Commit

Permalink
Merge pull request #23 from jvail/master
Browse files Browse the repository at this point in the history
Fix schema error and return params after creation
  • Loading branch information
fredboudon authored Dec 16, 2020
2 parents a05cb7c + 10b96a2 commit 07ca778
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ doc/_build/doctrees
cmake-build-debug/

# PyBuilder
target/
target/

.vscode
build-cmake
56 changes: 28 additions & 28 deletions src/openalea/lpy/lsysparameters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from openalea.plantgl.all import PglTurtle, PyStrPrinter, Material, NurbsCurve2D, BezierCurve2D, Polyline2D, NurbsPatch
import openalea.plantgl.all as pgl
from .__lpy_kernel__ import LpyParsing, LsysContext, Lsystem
from collections import OrderedDict
from collections import OrderedDict

default_credits = {'__authors__' : '' ,
'__institutes__' : '' ,
Expand All @@ -19,10 +19,10 @@

def isSimilarToDefaultTurtleMat(cmat, i):
if cmat.isTexture() : return False

nbdefault = len(defaultturtlecolorlist)

if i >= nbdefault:
if i >= nbdefault:
defaultmat = Material('Color_'+str(i))
else:
defaultmat = Material(defaultturtlecolorlist[i])
Expand Down Expand Up @@ -116,13 +116,13 @@ def check_validity(self):

def check_similarity(self, other):
from itertools import zip_longest
if self.execOptions != other.execOptions:
if self.execOptions != other.execOptions:
raise ValueError('execOptions',self.execOptions,other.execOptions)
if self.credits != other.credits:
if self.credits != other.credits:
raise ValueError('credits',self.credits,other.credits)
if self.animation_timestep != other.animation_timestep:
if self.animation_timestep != other.animation_timestep:
raise ValueError('animation_timestep',self.animation_timestep,other.animation_timestep)
if self.default_category_name != other.default_category_name:
if self.default_category_name != other.default_category_name:
raise ValueError('default_category',self.default_category_name,other.default_category_name)

self.check_similar_colors(other)
Expand All @@ -131,9 +131,9 @@ def check_similarity(self, other):

def check_similar_parameters(self, other):
from itertools import zip_longest
import openalea.plantgl.scenegraph.pglinspect as inspect
import openalea.plantgl.scenegraph.pglinspect as inspect
def similar_pgl_object(v1,v2):
attributes = inspect.get_pgl_attributes(v1)
attributes = inspect.get_pgl_attributes(v1)
for att in attributes:
if getattr(v1,att) != getattr(v2,att):
if not isinstance(getattr(v1,att),pgl.PglObject): # cannot compare easily
Expand Down Expand Up @@ -183,11 +183,11 @@ def check_similar_colors(self, other):
if type(v1) != type(v2):
raise ValueError('color',v1,v2)
if isinstance(v1, pgl.Material) :
if not v1.isSimilar(v2):
if not v1.isSimilar(v2):
raise ValueError('material',v1,v2)
else:
if v1.image.filename != v1.image.filename:
raise ValueError('texture',v1.image.filename,v2.image.filename)
raise ValueError('texture',v1.image.filename,v2.image.filename)


def get_available_parameter_types(self):
Expand Down Expand Up @@ -215,8 +215,7 @@ def add(self, name, value, ptype = None, category = None, **params):
raise TypeError(ptype)

def add_category(self, name, **params):
category = Category({ 'name' : category, 'enabled' : True })
category.update(params)
category = Category({ 'name' : name, 'enabled' : True }, params=params)
self.categories[name] = category

def set_defaut_category(self, name):
Expand All @@ -234,22 +233,23 @@ def add_scalar(self, name, value, ptype = None, category = None, **params):
scalar.category = category

self._add_scalar(category, scalar)
return scalar

def _add_scalar(self, category, scalar):
categoryobj = self.get_category(category)
categoryobj.add_scalar(scalar)

def add_function(self, name, value = None, category = None):
""" if value is None a default function value is created """
self.add_graphicalparameter(name, value, 'Function', category)
return self.add_graphicalparameter(name, value, 'Function', category)

def add_curve(self, name, value = None, category = None):
""" if value is None a default function value is created """
self.add_graphicalparameter(name, value, 'Curve2D', category)
return self.add_graphicalparameter(name, value, 'Curve2D', category)

def add_patch(self, name, value = None, category = None):
""" if value is None a default function value is created """
self.add_graphicalparameter(name, value, 'NurbsPatch', category)
return self.add_graphicalparameter(name, value, 'NurbsPatch', category)

def add_graphicalparameter(self, name, value, ptype = None, category = None):
assert ptype in self.get_available_graphical_types()
Expand All @@ -263,6 +263,7 @@ def add_graphicalparameter(self, name, value, ptype = None, category = None):
manager.setName(value, name)

self._add_graphicalparameter(category, manager, value)
return (manager, value)

def _add_graphicalparameter(self, category, manager, value):
categoryobj = self.get_category(category)
Expand Down Expand Up @@ -365,7 +366,7 @@ def _retrieve_scalars_from_env(self, context, code_version):
scalars = context.get('__scalars__', [])
currentcategory = self.default_category_name
for sc in scalars:
if sc[1] == 'Category':
if sc[1] == 'Category':
currentcategory = sc[0]
else:
csc = ProduceScalar(sc)
Expand All @@ -377,9 +378,9 @@ def _retrieve_graphical_parameters_from_env(self, context, code_version):
from openalea.lpy.parameters.scalar import scalar_from_json_rep
managers = self.get_graphicalparameter_managers()

def checkinfo(info):
def checkinfo(info):
if type(info) == str:
return {'name':info , 'enabled':True}
return {'name':info , 'enabled':True}
else :
if code_version == 1.1:
if 'active' in info:
Expand Down Expand Up @@ -438,8 +439,8 @@ def _apply_colors_to_env(self, context):
context.turtle.setMaterial(i, cmat)

def _apply_parameters_to_env(self, context):
context["__parameterset__"] = [(category.info,
[(manager.typename, obj) for manager, obj in category.items.values()],
context["__parameterset__"] = [(category.info,
[(manager.typename, obj) for manager, obj in category.items.values()],
[scalar.todict() for scalar in category.scalars.values()]) for category in self.categories.values()]
for category in self.categories.values():
if category.info.get('enabled',True):
Expand Down Expand Up @@ -494,14 +495,14 @@ def _generate_colors_py_code(self, indentation = '\t', reference_dir = None, ver
init_txt += printer.str()
printer.clear()
init_txt += indentation+'context.turtle.setMaterial('+repr(i)+','+str(cmat.name)+')\n'
return init_txt
return init_txt

def _generate_exec_parameters_py_code(self, indentation = '\t', version = default_lpycode_version):
init_txt = ''
if not self.animation_timestep is None:
init_txt += indentation+'context.animation_timestep = '+str(self.animation_timestep)+'\n'
init_txt += indentation+'context.animation_timestep = '+str(self.animation_timestep)+'\n'
if self.default_category_name != 'default':
init_txt += indentation+'context.default_category = '+str(self.default_category)+'\n'
init_txt += indentation+'context.default_category = '+str(self.default_category)+'\n'
for optname, optvalue in self.execOptions.items():
init_txt += indentation+'context.options.setSelection('+repr(optname)+','+str(optvalue)+')\n'
return init_txt
Expand All @@ -520,7 +521,7 @@ def emptyparameterset(params):
for manager,obj in category.items.values():
if manager not in intialized_managers:
intialized_managers[manager] = True
init_txt += manager.initWriting('\t')
init_txt += manager.initWriting('\t')
init_txt += manager.writeObject(obj,'\t')
init_txt += indentation+'category_'+str(panelid)+' = ('+repr(category.info)
init_txt += ',['+','.join(['('+repr(manager.typename)+','+manager.getName(obj)+')' for manager,obj in category.items.values()])+']\n'
Expand Down Expand Up @@ -549,7 +550,7 @@ def _generate_scalars_py_code(self, indentation = '\t', version = default_lpycod

def _generate_credits_py_code(self, indentation = '\t'):
txt = ''
for key,value in self.credits.items():
for key,value in self.credits.items():
if len(value) > 0:
txt += indentation+key+' = '+repr(str(value))+'\n'
return txt
Expand Down Expand Up @@ -593,7 +594,7 @@ def generate_json_parameter_dict(self):
credits = dict([(key,value) for key,value in self.credits.items() if value != ''])
)
assert LsystemParameters.is_valid_schema(result)
return result
return result

@staticmethod
def is_valid_schema(obj):
Expand Down Expand Up @@ -686,4 +687,3 @@ def dump(self, file):
import json
obj = self.generate_json_parameter_dict()
json.dump(obj, file)

2 changes: 1 addition & 1 deletion src/openalea/lpy/parameters/schema/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "object",
"properties": {
"animation_timestep": {
"type": "integer",
"type": "number",
"minimum": 0
}
},
Expand Down

0 comments on commit 07ca778

Please sign in to comment.