python declarative UI
- remove XML testing feature
- add 2 modules: bpyml - generic, bpyml_ui - blender spesific. nothing uses these now.
==bpyml_ui module==
defines BPyML_BaseUI and its draw() function which uses the bpyml member of the class instance self.draw_data & self.draw_header_data.
This way declarative ui is opt-in and easy to use by using BPyML_BaseUI as a mix-in class.
==bpyml module==
This module translates a python like XML representation into XML
or simple python blender/ui function calls.
sometag(arg=10) [
another(),
another(key="value")
]
# converts into ...
<sometag arg="10">
<another/>
<another key="value" />
</sometag>
This commit is contained in:
100
release/scripts/modules/bpyml_ui.py
Normal file
100
release/scripts/modules/bpyml_ui.py
Normal file
@@ -0,0 +1,100 @@
|
||||
# ##### BEGIN GPL LICENSE BLOCK #####
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#
|
||||
# ##### END GPL LICENSE BLOCK #####
|
||||
|
||||
# <pep8 compliant>
|
||||
|
||||
|
||||
import bpy as _bpy
|
||||
import bpyml
|
||||
from bpyml import TAG, ARGS, CHILDREN
|
||||
from types import ModuleType
|
||||
|
||||
_uilayout_rna = _bpy.types.UILayout.bl_rna
|
||||
|
||||
_uilayout_tags = ["ui"] + \
|
||||
_uilayout_rna.properties.keys() + \
|
||||
_uilayout_rna.functions.keys()
|
||||
|
||||
# these need to be imported directly
|
||||
# >>> from bpyml_ui.locals import *
|
||||
locals = bpyml.tag_module("%s.locals" % __name__ , _uilayout_tags)
|
||||
|
||||
|
||||
def _parse_rna(prop, value):
|
||||
if prop.type == 'FLOAT':
|
||||
value = float(value)
|
||||
elif prop.type == 'INT':
|
||||
value = int(value)
|
||||
elif prop.type == 'BOOLEAN':
|
||||
if value in (True, False):
|
||||
pass
|
||||
else:
|
||||
if value not in ("True", "False"):
|
||||
raise Exception("invalid bool value: %s" % value)
|
||||
value = bool(value == "True")
|
||||
elif prop.type in ('STRING', 'ENUM'):
|
||||
pass
|
||||
elif prop.type == 'POINTER':
|
||||
value = eval("_bpy." + value)
|
||||
else:
|
||||
raise Exception("type not supported %s.%s" % (prop.identifier, prop.type))
|
||||
return value
|
||||
|
||||
|
||||
def _parse_rna_args(base, py_node):
|
||||
rna_params = base.bl_rna.functions[py_node[TAG]].parameters
|
||||
args = {}
|
||||
for key, value in py_node[ARGS].items():
|
||||
args[key] = _parse_rna(rna_params[key], value)
|
||||
return args
|
||||
|
||||
|
||||
def _call_recursive(context, base, py_node):
|
||||
prop = base.bl_rna.properties.get(py_node[TAG])
|
||||
if py_node[TAG] in base.bl_rna.properties:
|
||||
value = py_node[ARGS].get("expr")
|
||||
if value:
|
||||
value = eval(value, {"context": _bpy.context})
|
||||
setattr(base, py_node[TAG], value)
|
||||
else:
|
||||
value = py_node[ARGS]['value'] # have to have this
|
||||
setattr(base, name, value)
|
||||
else:
|
||||
args = _parse_rna_args(base, py_node)
|
||||
func_new = getattr(base, py_node[TAG])
|
||||
base_new = func_new(**args) # call blender func
|
||||
if base_new is not None:
|
||||
for py_node_child in py_node[CHILDREN]:
|
||||
_call_recursive(context, base_new, py_node_child)
|
||||
|
||||
|
||||
class BPyML_BaseUI():
|
||||
'''
|
||||
This is a mix-in class that defines a draw function
|
||||
which checks for draw_data
|
||||
'''
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
for py_node in self.draw_data[CHILDREN]:
|
||||
_call_recursive(context, layout, py_node)
|
||||
|
||||
def draw_header(self, context):
|
||||
layout = self.layout
|
||||
for py_node in self.draw_header_data[CHILDREN]:
|
||||
_call_recursive(context, layout, py_node)
|
||||
Reference in New Issue
Block a user