-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsurface.py
More file actions
188 lines (142 loc) · 7.77 KB
/
surface.py
File metadata and controls
188 lines (142 loc) · 7.77 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
## INFO ########################################################################
## ##
## plastey ##
## ======= ##
## ##
## Oculus Rift + Leap Motion + Python 3 + C + Blender + Arch Linux ##
## Version: 0.2.0.943 (20150510) ##
## File: surface.py ##
## ##
## For more information about the project, visit ##
## <http://plastey.kibu.hu>. ##
## Copyright (C) 2015 Peter Varo, Kitchen Budapest ##
## ##
## 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 3 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, most likely a file in the root directory, ##
## called 'LICENSE'. If not, see <http://www.gnu.org/licenses>. ##
## ##
######################################################################## INFO ##
# Import python modules
from random import choice
from itertools import count
from pickle import dump, load, HIGHEST_PROTOCOL
# Import user modules
from utils import name_of_vertex
#------------------------------------------------------------------------------#
class VertexLocked(Exception): pass
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
class VertexAlreadySelected(Exception): pass
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
class SerialisedDataUnmatched(Exception): pass
#------------------------------------------------------------------------------#
class Surface:
MESH_INDEX = 0
MATERIAL_INDEX = 0
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
@property
def position(self):
return self._object.worldPosition
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
@property
def orientation(self):
return self._object.worldOrientation
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __init__(self, surface_creator, vertex_creator, base_color):
self._locked = {}
self._selected = {}
self._surface = surface_creator#()
self._vertices = vertex_creator#()
for vertex in self._vertices.children:
vertex.color = base_color
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# TODO: Check if self._vertices.children is still a KX_VertexProxy. It is
# most probably not => update all the function definition's return signatures
def __iter__(self) -> 'KX_VertexProxy':
yield from self._vertices.children
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def __getitem__(self, identifier: 'index or name') -> 'KX_VertexProxy':
return self._vertices.children[identifier]
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def serialise(self) -> 'list':
coords = []
vertices_children = self._vertices.children
try:
for i in count():
coords.extend(vertices_children[name_of_vertex(i)].localPosition)
except KeyError:
return coords
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def deserialise(self, coords):
vertices_children = self._vertices.children
try:
for i, coord in enumerate(zip(*(iter(coords),)*3)):
vertices_children[name_of_vertex(i)].localPosition = coord
except KeyError:
raise SerialisedDataUnmatched
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def update(self):
self._surface.update()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def selected(self) -> 'tuples of index and KX_VertexProxy pair':
yield from self._selected.items()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def is_selected(self, identifier: 'index or name') -> 'boolean':
return identifier in self._selected
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def select(self, identifier: 'index or name') -> 'KX_VertexProxy':
if identifier in self._locked:
raise VertexLocked
if identifier in self._selected:
raise VertexAlreadySelected
vertex = self._vertices.children[identifier]
self._selected[identifier] = vertex
return vertex
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def deselect(self, identifier: 'index or name') -> 'KX_VertexProxy':
if identifier in self._locked:
raise VertexLocked
return self._selected.pop(identifier, None)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def deselect_all(self) -> 'tuple of KX_VertexProxies':
selected = tuple(self._selected.values())
self._selected = {}
return selected
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def is_locked(self, identifier: 'index or name') -> 'boolean':
return identifier in self._locked
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def locked(self) -> 'tuples of index and KX_VertexProxy pair':
yield from self._locked.items()
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def lock(self, identifier: 'index or name') -> 'KX_VertexProxy':
vertex = self._vertices.children[identifier]
self._locked[identifier] = vertex
return vertex
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def unlock(self, identifier: 'index or name') -> 'KX_VertexProxy':
return self._locked.pop(identifier, None)
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def unlock_all(self) -> 'tuple of KX_VertexProxies':
locked = tuple(self._locked.values())
self._locked = {}
return locked
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
def move_vertices(self, identifiers, move_vector):
error = None
for identifier in identifiers:
if identifier in self._locked:
error = VertexLocked('Some vertices are locked')
else:
vertex.applyMovement(vector)
if error:
raise error