Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bpyml_ui.py « modules « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b4ad4e0b54acd84599ee0fd8374479e2cde9d691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# ##### 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

_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, py_node[TAG], 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)