-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.py
336 lines (241 loc) · 11.3 KB
/
operators.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import os
import bpy
from bpy.types import Operator
from bpy.props import EnumProperty
from bpy.ops import view3d
from bpy_extras.io_utils import ExportHelper, ImportHelper
from .constants import MEMBERS_KEY, COLORS
from . import backend
from . import utils
# -------------------------------------------------------------------
class BaseOperator(Operator):
@classmethod
def poll(cls, context):
# Ensure that the operator will not be called when the context is
# not compatible with it. Here for instance our operator only applies
# to a mesh.
# It is better to do these context checks here than directly in
# execute() because it will also for instance deactivate buttons
# pointing to this operator in the UI.
return context.scene.active_mesh in bpy.data.objects and bpy.data.objects[context.scene.active_mesh].type == 'MESH'
# -------------------------------------------------------------------
class ResetSettings(BaseOperator):
bl_idname = "bone_generator.reset_settings"
bl_label = "Reset Settings"
def execute(self, context):
# Reset all colors
utils.mode_set(mode='OBJECT')
points_cloud = utils.get_vertices(context, 'ALL')
utils.color_to_vertices(context, points_cloud, (1, 1, 1, 1))
context.scene.active_mesh = ""
context.scene.selected_members.clear()
context.scene.selection_state.reset()
return {'FINISHED'}
# -------------------------------------------------------------------
class SaveSettings(BaseOperator, ExportHelper):
bl_idname = "bone_generator.save_settings"
bl_label = "Save Settings"
filename_ext = ".json"
def execute(self, context):
userpath = self.properties.filepath
if not userpath.lower().endswith('.json'):
self.report({'WARNING'}, "You need to export a valid .json file.")
json_loader = utils.JSONLoader(userpath)
json_loader.save(context.scene)
return {'FINISHED'}
# -------------------------------------------------------------------
class LoadSettings(BaseOperator, ImportHelper):
bl_idname = "bone_generator.load_settings"
bl_label = "Load Settings"
def execute(self, context):
userpath = self.properties.filepath
if not userpath.lower().endswith('.json'):
self.report({'WARNING'}, "You need to import a valid .json file.")
# Reset all colors
utils.mode_set(mode='OBJECT')
points_cloud = utils.get_vertices(context, 'ALL')
utils.color_to_vertices(context, points_cloud, (1, 1, 1, 1))
# Load
json_loader = utils.JSONLoader(userpath)
json_loader.load(context.scene)
# Colorize
for member in context.scene.selected_members:
points_cloud = utils.get_vertices(context, 'MEMBER', member=member)
utils.color_to_vertices(context, points_cloud, COLORS[member.name])
return {'FINISHED'}
# -------------------------------------------------------------------
class AddMember(BaseOperator):
bl_idname = "bone_generator.add_member"
bl_label = "Add member"
@classmethod
def poll(cls, context):
found = False
for member in context.scene.selected_members:
if context.scene.active_member_type == member.name:
found = True
return super().poll(context) and not found and not context.scene.selection_state.is_active
def execute(self, context):
# Panel
context.scene.selection_state.is_active = True
context.scene.selection_state.selection_member_type = context.scene.active_member_type
# 3D View (go to selection state)
utils.setup_selection_state(context)
return {'FINISHED'}
# -------------------------------------------------------------------
def reset_color_member(context, member_type) -> int:
# Find the member in the collection `selected_members`
for idx, member in enumerate(context.scene.selected_members):
if member_type == member.name:
# Get vertices in the active object mesh for the points cloud in the member
points_cloud = utils.get_vertices(context, 'MEMBER', member=context.scene.selected_members[member_type])
# Reset this colors
utils.color_to_vertices(context, points_cloud, (1, 1, 1, 1))
return idx
# -------------------------------------------------------------------
class ModifyMember(BaseOperator):
bl_idname = "bone_generator.modify_member"
bl_label = "modify member"
member_type: EnumProperty(name="Member", items=MEMBERS_KEY)
def execute(self, context):
# Panel
context.scene.selection_state.is_active = True
context.scene.selection_state.selection_member_type = self.member_type
# 3D View (go to selection state)
utils.setup_selection_state(context)
return {'FINISHED'}
# -------------------------------------------------------------------
class DeleteMember(BaseOperator):
bl_idname = "bone_generator.delete_member"
bl_label = "Delete member"
member_type: EnumProperty(name="Member", items=MEMBERS_KEY)
def execute(self, context):
idx = reset_color_member(context, self.member_type)
# Delete the member
context.scene.selected_members.remove(idx)
return {'FINISHED'}
# -------------------------------------------------------------------
class ValidateSelection(BaseOperator):
bl_idname = "bone_generator.validate_selection"
bl_label = "Validate Selection"
@classmethod
def poll(cls, context):
return super().poll(context) and context.scene.selection_state.is_active
def execute(self, context):
reset_color_member(context, context.scene.selection_state.selection_member_type)
# Panel
context.scene.selection_state.is_active = False
points_cloud = utils.get_vertices(context, 'SELECTED')
utils.color_to_vertices(context, points_cloud, COLORS[context.scene.selection_state.selection_member_type])
matrix_world = bpy.data.objects[context.scene.active_mesh].matrix_world
found = False
for idx, member in enumerate(context.scene.selected_members):
if context.scene.selection_state.selection_member_type == member.name:
found = True
# Update
new_member = context.scene.selected_members[idx]
new_member.points_cloud.clear()
for point in points_cloud:
new_point = new_member.points_cloud.add()
new_point.set(matrix_world @ point.co)
# Add
if not found:
new_member = context.scene.selected_members.add()
new_member.name = context.scene.selection_state.selection_member_type
new_member.points_cloud.clear()
for point in points_cloud:
new_point = new_member.points_cloud.add()
new_point.set(matrix_world @ point.co)
# View 3D (back to normal state)
utils.back_to_normal_state(context)
return {'FINISHED'}
# -------------------------------------------------------------------
class CancelSelection(BaseOperator):
bl_idname = "bone_generator.cancel_selection"
bl_label = "Cancel selection"
def execute(self, context):
# Panel
context.scene.selection_state.reset()
# View 3D (back to normal state)
utils.back_to_normal_state(context)
return {'FINISHED'}
# -------------------------------------------------------------------
class ComputeBonesGeneration(BaseOperator):
"""Compute the centroid of the object (this message is used as description in the UI)"""
bl_idname = "bone_generator.compute_bones_generation"
bl_label = "Compute Bones Generation"
@classmethod
def poll(cls, context):
return super().poll(context) # and len(context.scene.selected_members) == 6
def execute(self, context):
# The operator class defines the front-end to a function. Its core
# logic will likely resides in a separate module (called 'backend' here)
# as a regular python function.
principal_components = backend.compute_bones_generation(context.active_object, utils.JSONLoader.to_serializable(context.scene))
# We can report messages to the user, doc at:
# https://docs.blender.org/api/current/bpy.types.Operator.html#bpy.types.Operator.Operator.report
self.report({'INFO'}, f"Principal components coordinate are {principal_components}")
if principal_components:
# création de l'armature
utils.mode_set(mode='OBJECT')
bpy.ops.object.armature_add(location=(0, 0, 0))
armature = bpy.context.active_object
utils.mode_set(mode='EDIT')
# On unpack les valeurs
head, tail = principal_components['BODY']
bone_body = bpy.context.active_bone
# On lui assigne ses postions correspondante
bone_body.head = head
bone_body.tail = tail
for key, points in principal_components.items():
if key == 'BODY': continue
# On unpack les valeurs
head, tail = points
# On crée l'armature
bone = armature.data.edit_bones.new(name=f'Bone{key}')
# On lui assigne ses postions correspondante
if 'LEG' in key:
bone.head = tail
bone.tail = head
bone_racc = armature.data.edit_bones.new(name=f'Bone{key}_Raccordement')
bone_racc.head = bone_body.head
bone_racc.tail = bone.head
if key == 'HEAD':
bone.head = tail
bone.tail = head
bone_racc = armature.data.edit_bones.new(name=f'Bone{key}_Raccordement')
bone_racc.head = bone_body.tail
bone_racc.tail = bone.head
if 'ARM' in key:
bone.head = head
bone.tail = tail
bone_racc = armature.data.edit_bones.new(name=f'Bone{key}_Raccordement')
bone_racc.head = bone_body.tail
bone_racc.tail = bone.head
bone.use_relative_parent = True
bone.parent = bone_racc
bone_racc.use_relative_parent = True
bone_racc.parent = bone_body
utils.mode_set(mode='OBJECT')
cube = bpy.data.objects[context.scene.active_mesh]
cube.select_set(True)
armature.select_set(True)
bpy.ops.object.parent_set(type='ARMATURE_AUTO')
# debug
# utils.mode_set('OBJECT')
# for key, points in principal_components.items():
# for point in points:
# bpy.ops.mesh.primitive_cube_add(size=0.25, location=point)
return {'FINISHED'}
# -------------------------------------------------------------------
classes = (
ResetSettings,
SaveSettings,
LoadSettings,
AddMember,
ModifyMember,
DeleteMember,
ValidateSelection,
CancelSelection,
ComputeBonesGeneration
)
register, unregister = bpy.utils.register_classes_factory(classes)