1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
24 def console_namespace():
26 get_consoles
= console_python
.get_console
27 consoles
= getattr(get_consoles
, "consoles", None)
29 for console
, stdout
, stderr
in get_consoles
.consoles
.values():
34 def is_display_list(listvar
):
35 from mathutils
import Vector
38 if type(var
) is not Vector
:
47 # Store the display states, called upon unregister the Add-on
48 # This is useful when you press F8 to reload the Addons.
49 # Then this function preserves the display states of the
51 state_props
= bpy
.context
.window_manager
.MathVisStatePropList
52 variables
= get_math_data()
53 for key
, ktype
in variables
.items():
54 if key
and key
not in state_props
:
55 prop
= state_props
.add()
57 prop
.ktype
= ktype
.__name
__
58 prop
.state
= [True, False]
62 index
= bpy
.context
.window_manager
.MathVisStatePropList
.find(key
)
67 state_props
= bpy
.context
.window_manager
.MathVisStatePropList
68 index
= state_props
.find(key
)
70 state_props
.remove(index
)
73 def toggle_display_state(key
):
74 state_props
= bpy
.context
.window_manager
.MathVisStatePropList
75 if key
in state_props
:
76 state_props
[key
].state
[0] = not state_props
[key
].state
[0]
78 print("Odd: Can not find key %s in MathVisStateProps" % (key
))
81 def toggle_lock_state(key
):
82 state_props
= bpy
.context
.window_manager
.MathVisStatePropList
83 if key
in state_props
:
84 state_props
[key
].state
[1] = not state_props
[key
].state
[1]
86 print("Odd: Can not find key %s in MathVisStateProps" % (key
))
90 from mathutils
import Matrix
, Vector
, Quaternion
, Euler
92 locals = console_namespace()
97 for key
, var
in locals.items():
98 if len(key
) == 0 or key
[0] == "_":
103 # Rules out sets/dicts.
104 # It's also possible the length check below is slow
105 # for data with underlying linked-list structure.
106 if not hasattr(type_var
, "__getitem__"):
109 # Don't do a truth test on the data because this causes an error with some
110 # array types, see T66107.
111 len_fn
= getattr(type_var
, "__len__", None)
117 if type_var
in {Matrix
, Vector
, Quaternion
, Euler
} or \
118 type_var
in {tuple, list} and is_display_list(var
):
120 variables
[key
] = type_var
125 def cleanup_math_data():
127 locals = console_namespace()
131 variables
= get_math_data()
133 for key
in variables
.keys():
134 index
= VarStates
.get_index(key
)
138 state_prop
= bpy
.context
.window_manager
.MathVisStatePropList
.get(key
)
139 if state_prop
.state
[1]:
143 bpy
.context
.window_manager
.MathVisStatePropList
.remove(index
)
146 def console_math_data():
147 from mathutils
import Matrix
, Vector
, Quaternion
, Euler
153 data_vector_array
= {}
155 for key
, var
in console_namespace().items():
159 state_prop
= bpy
.context
.window_manager
.MathVisStatePropList
.get(key
)
161 disp
, lock
= state_prop
.state
167 if var_type
is Matrix
:
168 if len(var
.col
) != 4 or len(var
.row
) != 4:
169 if len(var
.col
) == len(var
.row
):
171 else: # todo, support 4x3 matrix
173 data_matrix
[key
] = var
174 elif var_type
is Vector
:
177 data_vector
[key
] = var
178 elif var_type
is Quaternion
:
180 elif var_type
is Euler
:
181 data_euler
[key
] = var
182 elif var_type
in {list, tuple} and is_display_list(var
):
183 data_vector_array
[key
] = var
185 return data_matrix
, data_quat
, data_euler
, data_vector
, data_vector_array