forked from helluvamesh/GYAZ-Export-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh_tools.py
765 lines (571 loc) · 33.3 KB
/
mesh_tools.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# ##### 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 laTter 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 #####
##########################################################################################################
##########################################################################################################
import bpy, bmesh
from bpy.props import *
from bpy.types import Operator, Panel, PropertyGroup, UIList, Menu, Mesh, Scene
from mathutils import Vector
# report
def report (self, item, error_or_info):
self.report({error_or_info}, item)
# merge materials ui
def ui_merge_materials (self, context):
lay = self.layout
obj = context.active_object
if obj.type == 'MESH':
owner = obj.data.gyaz_export
lay.prop (owner, 'merge_materials', toggle=True)
lay.prop (owner, 'atlas_name')
# uv utils
class Op_GYAZ_MoveUVMap (bpy.types.Operator):
bl_idname = "object.gyaz_move_uv_map"
bl_label = "Move UV Map"
bl_description = "Move active uv map"
up: BoolProperty (default=False)
#operator function
def execute(self, context):
mesh = bpy.context.object.data
uvmaps = mesh.uv_layers
map_count = len (uvmaps)
if map_count >= 8:
report (self, 'Cannot reorder 8 uv maps.', 'WARNING')
else:
if map_count > 0:
def move_down ():
save__export = list(mesh.gyaz_export.uv_export[:])
ori_active_index = uvmaps.active_index
ori_active_name = uvmaps[ori_active_index].name
for uvm in uvmaps:
if uvm.active_render:
ori_active_render = uvm.name
if ori_active_index != map_count-1:
def move_to_bottom (skip):
active_index = ori_active_index + skip
uvmaps.active_index = active_index
active_name = uvmaps[uvmaps.active_index].name
new = uvmaps.new ()
uvmaps.remove (uvmaps[active_index])
uvmaps.active_index = 0
uvmaps[-1].name = active_name
move_to_bottom (skip=0)
for n in range (0, map_count-ori_active_index-2):
move_to_bottom (skip=1)
uvmaps.get(ori_active_name).active = True
uvmaps.get(ori_active_render).active_render = True
# update export bools
new_export = save__export.copy ()
new_export[ori_active_index] = save__export[uvmaps.active_index]
new_export[uvmaps.active_index] = save__export[ori_active_index]
mesh.gyaz_export.uv_export = new_export
def move_up ():
ori_active_index = uvmaps.active_index
ori_active_name = uvmaps[ori_active_index].name
for uvm in uvmaps:
if uvm.active_render:
ori_active_render = uvm.name
if ori_active_index != 0:
uvmaps.active_index -= 1
move_down ()
uvmaps.get(ori_active_name).active = True
uvmaps.get(ori_active_render).active_render = True
if self.up:
move_up ()
else:
move_down ()
return {'FINISHED'}
class Op_GYAZ_MoveVertColor (bpy.types.Operator):
bl_idname = "object.gyaz_move_vert_color"
bl_label = "Move Vertex Color"
bl_description = "Move active vertex color"
up: BoolProperty (default=False)
#operator function
def execute(self, context):
mesh = bpy.context.object.data
vert_colors = mesh.vertex_colors
map_count = len (vert_colors)
if map_count >= 8:
report (self, 'Cannot reorder 8 vertex colors.', 'WARNING')
else:
if map_count > 0:
def move_down ():
save__export = list(mesh.gyaz_export.vert_color_export[:])
ori_active_index = vert_colors.active_index
ori_active_name = vert_colors[ori_active_index].name
for vcol in vert_colors:
if vcol.active_render:
ori_active_render = vcol.name
if ori_active_index != map_count-1:
def move_to_bottom (skip):
active_index = ori_active_index + skip
vert_colors.active_index = active_index
active_name = vert_colors[vert_colors.active_index].name
new = vert_colors.new ()
vert_colors.remove (vert_colors[active_index])
vert_colors.active_index = 0
vert_colors[-1].name = active_name
move_to_bottom (skip=0)
for n in range (0, map_count-ori_active_index-2):
move_to_bottom (skip=1)
vert_colors.get(ori_active_name).active = True
vert_colors.get(ori_active_render).active_render = True
# update export bools
new_export = save__export.copy ()
new_export[ori_active_index] = save__export[vert_colors.active_index]
new_export[vert_colors.active_index] = save__export[ori_active_index]
mesh.gyaz_export.vert_color_export = new_export
def move_up ():
ori_active_index = vert_colors.active_index
ori_active_name = vert_colors[ori_active_index].name
for vcol in vert_colors:
if vcol.active_render:
ori_active_render = vcol.name
if ori_active_index != 0:
vert_colors.active_index -= 1
move_down ()
vert_colors.get(ori_active_name).active = True
vert_colors.get(ori_active_render).active_render = True
if self.up:
move_up ()
else:
move_down ()
return {'FINISHED'}
class Op_GYAZ_BatchSetActiveUVMapByIndex (bpy.types.Operator):
bl_idname = "object.gyaz_batch_set_active_uv_map_by_index"
bl_label = "Batch Set Active UV Map by Index"
bl_description = "Set uv map active on all selected objects by the index of the active object's active uv map"
for_render: BoolProperty (default=False)
#operator function
def execute(self, context):
scene = bpy.context.scene
object = bpy.context.active_object
selected_objects = bpy.context.selected_objects
uvmaps = object.data.uv_layers
if len (uvmaps) > 0:
active_uv_map_index = uvmaps.active_index
meshes = {obj.data for obj in selected_objects if obj.type == 'MESH'}
for mesh in meshes:
uv_maps = mesh.uv_layers
if len (uv_maps) > 0:
if active_uv_map_index < len(uv_maps):
if not self.for_render:
uv_maps.active_index = active_uv_map_index
else:
uv_maps[active_uv_map_index].active_render = True
return {'FINISHED'}
class Op_GYAZ_BatchSetActiveUVMapByName (bpy.types.Operator):
bl_idname = "object.gyaz_batch_set_active_uv_map_by_name"
bl_label = "Batch Set Active UV Map by Name"
bl_description = "Set uv map active on all selected objects by the name of active object's active uv map"
for_render: BoolProperty (default=False)
#operator function
def execute(self, context):
scene = bpy.context.scene
object = bpy.context.active_object
selected_objects = bpy.context.selected_objects
uvmaps = object.data.uv_layers
if len (uvmaps) > 0:
active_uv_map_index = uvmaps.active_index
active_uv_map_name = uvmaps[active_uv_map_index].name
meshes = {obj.data for obj in selected_objects if obj.type == 'MESH'}
for mesh in meshes:
uv_maps = mesh.uv_layers
if len (uv_maps) > 0:
for index, uv_map in enumerate(uv_maps):
if uv_map.name == active_uv_map_name:
if not self.for_render:
uv_maps.active_index = index
else:
uv_maps[active_uv_map_name].active_render = True
return {'FINISHED'}
class Op_GYAZ_BatchAddUVMap (bpy.types.Operator):
bl_idname = "object.gyaz_batch_add_uv_map"
bl_label = "Batch Add UV Map"
bl_description = "Add new uv map to all selected objects"
name: StringProperty (name='Name')
# confirm popup
def invoke (self, context, event):
wm = bpy.context.window_manager
return wm.invoke_props_dialog (self)
#operator function
def execute(self, context):
name = self.name if self.name != '' else 'UVMap'
meshes = {obj.data for obj in bpy.context.selected_objects if obj.type == 'MESH'}
for mesh in meshes:
uv_map = mesh.uv_layers.new (name=name)
if uv_map is not None:
uv_map.active = True
return {'FINISHED'}
# Panel Overrides:
# uvmaps
class UI_UL_GYAZ_UVMaps (UIList):
def draw_item (self, context, layout, data, set, icon, active_data, active_propname, index):
row = layout.row (align=True)
owner = context.mesh.gyaz_export
row.prop (owner, "uv_export", text='', emboss=False, index=index, icon='EXPORT' if owner.uv_export[index] else 'BLANK1')
row.prop (set, "name", text='', emboss=False)
row.prop (set, "active_render", text='', emboss=False, icon='RESTRICT_RENDER_OFF' if set.active_render else 'RESTRICT_RENDER_ON')
class DATA_MT_GYAZ_UVUtils (Menu):
bl_label = 'UV Utils'
def draw (self, context):
lay = self.layout
lay.operator_context = 'INVOKE_REGION_WIN'
lay.operator (Op_GYAZ_BatchSetActiveUVMapByIndex.bl_idname).for_render=False
lay.operator (Op_GYAZ_BatchSetActiveUVMapByIndex.bl_idname, text='For Render', icon='RESTRICT_RENDER_OFF').for_render=True
lay.operator (Op_GYAZ_BatchSetActiveUVMapByName.bl_idname).for_render=False
lay.operator (Op_GYAZ_BatchSetActiveUVMapByName.bl_idname, text='For Render', icon='RESTRICT_RENDER_OFF').for_render=True
lay.operator (Op_GYAZ_BatchAddUVMap.bl_idname)
class DATA_PT_uv_texture (Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = 'data'
bl_label = 'UV Maps'
# add ui elements here
def draw (self, context):
lay = self.layout
mesh = bpy.context.mesh
uv_count = len (mesh.uv_layers)
row_count = 4 if uv_count > 1 else 2
row = lay.row ()
row.template_list ("UI_UL_GYAZ_UVMaps", "", # type and unique id
mesh, "uv_layers", # pointer to the CollectionProperty
mesh.uv_layers, "active_index", # pointer to the active identifier
rows = row_count, maxrows = row_count)
col = row.column (align=True)
col.enabled = True if not context.space_data.use_pin_id else False
col.operator ('mesh.uv_texture_add', icon='ADD', text='')
col.operator ('mesh.uv_texture_remove', icon='REMOVE', text='')
col.separator ()
col.menu ('DATA_MT_GYAZ_UVUtils', text='', icon='DOWNARROW_HLT')
if uv_count > 1:
col.separator ()
col.operator (Op_GYAZ_MoveUVMap.bl_idname, text='', icon='TRIA_UP').up = True
col.operator (Op_GYAZ_MoveUVMap.bl_idname, text='', icon='TRIA_DOWN').up = False
#when the buttons should show up
@classmethod
def poll(cls, context):
ob = bpy.context.active_object
return ob.type == 'MESH'
# vertex colors
class UI_UL_GYAZ_VertexColorList (UIList):
def draw_item (self, context, layout, data, set, icon, active_data, active_propname, index):
row = layout.row (align=True)
owner = context.mesh.gyaz_export
row.prop (owner, "vert_color_export", text='', emboss=False, index=index, icon='EXPORT' if owner.vert_color_export[index] else 'BLANK1')
row.prop (set, "name", text='', emboss=False)
row.prop (set, "active_render", text='', emboss=False, icon='RESTRICT_RENDER_OFF' if set.active_render else 'RESTRICT_RENDER_ON')
class DATA_PT_vertex_colors (Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = 'data'
bl_label = 'Vertex Colors'
# add ui elements here
def draw (self, context):
lay = self.layout
mesh = bpy.context.mesh
row = lay.row ()
row.template_list ("UI_UL_GYAZ_VertexColorList", "", # type and unique id
mesh, "vertex_colors", # pointer to the CollectionProperty
mesh.vertex_colors, "active_index", # pointer to the active identifier
rows = 1, maxrows = 1)
col = row.column (align=True)
col.enabled = True if not context.space_data.use_pin_id else False
col.operator ('mesh.vertex_color_add', icon='ADD', text='')
col.operator ('mesh.vertex_color_remove', icon='REMOVE', text='')
if len (mesh.vertex_colors) > 1:
col.separator ()
col.operator (Op_GYAZ_MoveVertColor.bl_idname, text='', icon='TRIA_UP').up = True
col.operator (Op_GYAZ_MoveVertColor.bl_idname, text='', icon='TRIA_DOWN').up = False
#when the buttons should show up
@classmethod
def poll(cls, context):
ob = bpy.context.active_object
return ob.type == 'MESH'
class PG_GYAZ_Export_EncodeShapeKeysInUVChannel (PropertyGroup):
def update_base_mesh (self, context):
prop = bpy.context.scene.gyaz_export_shapes.base_mesh
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.base_mesh = None
def update_shape_mesh_1 (self, context):
prop = bpy.context.scene.gyaz_export_shapes.shape_key_mesh_1
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.shape_key_mesh_1 = None
def update_shape_mesh_2 (self, context):
prop = bpy.context.scene.gyaz_export_shapes.shape_key_mesh_2
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.shape_key_mesh_2 = None
def update_shape_mesh_3 (self, context):
prop = bpy.context.scene.gyaz_export_shapes.shape_key_mesh_3
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.shape_key_mesh_3 = None
def update_shape_mesh_4 (self, context):
prop = bpy.context.scene.gyaz_export_shapes.shape_key_mesh_4
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.shape_key_mesh_4 = None
def update_shape_mesh_5 (self, context):
prop = bpy.context.scene.gyaz_export_shapes.shape_key_mesh_5
if prop is not None:
if prop.type != 'MESH':
bpy.context.scene.gyaz_export_shapes.shape_key_mesh_5 = None
show_props: BoolProperty (name='Shape Keys In UVs')
base_mesh: PointerProperty (type = bpy.types.Object, name='Base Mesh', update=update_base_mesh)
shape_key_mesh_1: PointerProperty (type = bpy.types.Object, name='Shape Mesh 1', update=update_shape_mesh_1)
shape_key_mesh_2: PointerProperty (type = bpy.types.Object, name='Shape Mesh 2', update=update_shape_mesh_2)
shape_key_mesh_3: PointerProperty (type = bpy.types.Object, name='Shape Mesh 3', update=update_shape_mesh_3)
shape_key_mesh_4: PointerProperty (type = bpy.types.Object, name='Shape Mesh 4', update=update_shape_mesh_4)
shape_key_mesh_5: PointerProperty (type = bpy.types.Object, name='Shape Mesh 5', update=update_shape_mesh_5)
shape_nor_to_vert_col: EnumProperty (name='Shape Normal To Vert Color', items=(('None', 'None', ''), ('0', 'Shape 1', ''), ('1', 'Shape 2', ''), ('2', 'Shape 3', ''), ('3', 'Shape 4', ''), ('4', 'Shape 5', '')), default='None')
encode_normals: BoolProperty (name='Normals', description='Whether to encode normals, too for higher quality')
class Op_GYAZ_Export_EncodeShapeKeysInUVChannels (Operator):
bl_idname = "object.gyaz_export_encode_shape_keys_in_uv_channels"
bl_label = "Encode Shape Keys In UV Channels"
bl_description = "UV1-UV3: World Position Offset, Normal"
bl_options = {'REGISTER', 'UNDO'}
# operator function
def execute(self, context):
owner = bpy.context.scene.gyaz_export_shapes
base_mesh = owner.base_mesh
shape_1 = owner.shape_key_mesh_1
shape_2 = owner.shape_key_mesh_2
shape_3 = owner.shape_key_mesh_3
shape_4 = owner.shape_key_mesh_4
shape_5 = owner.shape_key_mesh_5
if base_mesh is not None:
base_mesh = base_mesh.data
if shape_1 is not None:
shape_1 = shape_1.data
if shape_2 is not None:
shape_2 = shape_2.data
if shape_3 is not None:
shape_3 = shape_3.data
if shape_4 is not None:
shape_4 = shape_4.data
if shape_5 is not None:
shape_5 = shape_5.data
def main (uv_maps):
def set_uvs_by_vert (vert, co, uv_layer):
for loop in vert.link_loops:
loop[uv_layer].uv = co
if owner.encode_normals and shape_3 == None or not owner.encode_normals and shape_5 == None:
if len (uv_maps) == 0:
uv_maps.new (name='UVMap')
if owner.encode_normals:
uv_map_1 = uv_maps.new (name='Shape1_WPOxy')
uv_map_2 = uv_maps.new (name='Shape1_WPOz_NORx')
uv_map_3 = uv_maps.new (name='Shape1_NORyz')
if shape_2 is not None:
uv_map_4 = uv_maps.new (name='Shape2_WPOxy')
uv_map_5 = uv_maps.new (name='Shape2_WPOz_NORx')
uv_map_6 = uv_maps.new (name='Shape2_NORyz')
if shape_3 is not None:
uv_map_7 = uv_maps.new (name='Shape3_WPOxy')
uv_map_8 = uv_maps.new (name='Shape3_WPOz')
bm = bmesh.new ()
bm.from_mesh (base_mesh)
verts = bm.verts
uv_layer_1 = bm.loops.layers.uv[uv_map_1.name]
uv_layer_2 = bm.loops.layers.uv[uv_map_2.name]
uv_layer_3 = bm.loops.layers.uv[uv_map_3.name]
if shape_2 is not None:
uv_layer_4 = bm.loops.layers.uv[uv_map_4.name]
uv_layer_5 = bm.loops.layers.uv[uv_map_5.name]
uv_layer_6 = bm.loops.layers.uv[uv_map_6.name]
if shape_3 is not None:
uv_layer_7 = bm.loops.layers.uv[uv_map_7.name]
uv_layer_8 = bm.loops.layers.uv[uv_map_8.name]
def pack_data (shape_key_mesh, vert_index, vert, uv_layer_1, uv_layer_2, uv_layer_3):
vec = (shape_key_mesh.vertices[vert_index].co - vert.co) * 100
nor = ( shape_key_mesh.vertices[vert_index].normal + Vector ((1, 1, 1)) ) * 0.5
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_1)
set_uvs_by_vert (vert, (vec[2], nor[1]), uv_layer_2)
set_uvs_by_vert (vert, (1-nor[0], 1-nor[2]), uv_layer_3)
pack_shape_2 = True if shape_2 is not None else False
pack_shape_3 = True if shape_3 is not None else False
for vert_index, vert in enumerate (verts):
pack_data (shape_1, vert_index, vert, uv_layer_1, uv_layer_2, uv_layer_3)
if pack_shape_2:
pack_data (shape_2, vert_index, vert, uv_layer_4, uv_layer_5, uv_layer_6)
if pack_shape_3:
vec = (shape_3.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_7)
set_uvs_by_vert (vert, (vec[2], 0), uv_layer_8)
else:
uv_map_1 = uv_maps.new (name='Shape1_WPOxy')
uv_map_2 = uv_maps.new (name='Shape1_WPOz')
if shape_2 is not None:
uv_map_2.name = 'Shape1_WPOz_Shape2_WPOx'
uv_map_3 = uv_maps.new (name='Shape2_WPOyz')
if shape_3 is not None:
uv_map_4 = uv_maps.new (name='Shape3_WPOxy')
uv_map_5 = uv_maps.new (name='Shape3_WPOz')
if shape_4 is not None:
uv_map_5.name = 'Shape3_WPOz_Shape4_WPOx'
uv_map_6 = uv_maps.new (name='Shape4_WPOyz')
if shape_5 is not None:
uv_map_7 = uv_maps.new (name='Shape5_WPOxy')
uv_map_8 = uv_maps.new (name='Shape5_WPOz')
bm = bmesh.new ()
bm.from_mesh (base_mesh)
verts = bm.verts
uv_layer_1 = bm.loops.layers.uv[uv_map_1.name]
uv_layer_2 = bm.loops.layers.uv[uv_map_2.name]
if shape_2 is not None:
uv_layer_3 = bm.loops.layers.uv[uv_map_3.name]
if shape_3 is not None:
uv_layer_4 = bm.loops.layers.uv[uv_map_4.name]
uv_layer_5 = bm.loops.layers.uv[uv_map_5.name]
if shape_4 is not None:
uv_layer_6 = bm.loops.layers.uv[uv_map_6.name]
if shape_5 is not None:
uv_layer_7 = bm.loops.layers.uv[uv_map_7.name]
uv_layer_8 = bm.loops.layers.uv[uv_map_8.name]
if shape_2 == None:
for vert_index, vert in enumerate (verts):
vec = (shape_1.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_1)
set_uvs_by_vert (vert, (vec[2], 0), uv_layer_2)
else:
for vert_index, vert in enumerate (verts):
vec = (shape_1.vertices[vert_index].co - vert.co) * 100
vec2 = (shape_2.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_1)
set_uvs_by_vert (vert, (vec[2], vec2[1]), uv_layer_2)
set_uvs_by_vert (vert, (-vec2[0], -vec2[2]), uv_layer_3)
if shape_3 and not shape_4:
for vert_index, vert in enumerate (verts):
vec = (shape_3.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_4)
set_uvs_by_vert (vert, (vec[2], 0), uv_layer_5)
if shape_3 and shape_4:
for vert_index, vert in enumerate (verts):
vec = (shape_3.vertices[vert_index].co - vert.co) * 100
vec2 = (shape_4.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_4)
set_uvs_by_vert (vert, (vec[2], vec2[1]), uv_layer_5)
set_uvs_by_vert (vert, (-vec2[0], -vec2[2]), uv_layer_6)
if shape_5:
for vert_index, vert in enumerate (verts):
vec = (shape_5.vertices[vert_index].co - vert.co) * 100
set_uvs_by_vert (vert, (-vec[1], vec[0]), uv_layer_7)
set_uvs_by_vert (vert, (vec[2], 0), uv_layer_8)
nor_to_vc = owner.shape_nor_to_vert_col
if nor_to_vc != 'None':
shape_mesh = None
shapes = [shape_1, shape_2, shape_3, shape_4, shape_5]
shape_mesh = shapes[int(nor_to_vc)]
if shape_mesh is not None:
color_layer = bm.loops.layers.color.new ('Shape'+str(int(nor_to_vc)+1)+'_NOR')
for vert_index, vert in enumerate (verts):
nor = ( shape_mesh.vertices[vert_index].normal + Vector ((1, 1, 1)) ) * 0.5
for loop in vert.link_loops:
loop[color_layer] = 1-nor[1], 1-nor[0], nor[2]
bm.to_mesh (base_mesh)
bm.free ()
def call (limit):
if len (uv_maps) > limit:
report (self, 'Base Mesh has more than ' +str(limit)+ ' uv maps.', 'WARNING')
else:
nor_to_vc = owner.shape_nor_to_vert_col
if nor_to_vc != 'None':
if len (base_mesh.vertex_colors) >= 8:
report (self, 'No empty vertex color slot.', 'WARNING')
else:
main (uv_maps)
else:
main (uv_maps)
if base_mesh is not None and shape_1 is not None:
uv_maps = base_mesh.uv_layers
if owner.encode_normals:
if shape_2 is not None:
limit = 2
if shape_3 is not None:
limit = 0
else:
limit = 5
else:
limit = 6
if shape_2 is not None:
limit = 5
if shape_3 is not None:
limit = 3
if shape_4 is not None:
limit = 2
if shape_5 is not None:
limit = 0
call (limit)
else:
report (self, 'Base Mesh and Shape Mesh 1 always have to be set.', 'WARNING')
return {'FINISHED'}
#when the buttons should show up
@classmethod
def poll(cls, context):
return context.active_object is not None and context.mode == 'OBJECT'
# mesh props
class PG_GYAZ_Export_MeshProps (PropertyGroup):
uv_export: BoolVectorProperty (size=8, default=[True]*8, description='Whether the GYAZ Exporter keeps this uv map')
vert_color_export: BoolVectorProperty (size=8, default=(True, False, False, False, False, False, False, False), description='Whether the GYAZ Exporter keeps this vertex color layer')
merge_materials: BoolProperty (name='Merge Materials On Export', default=False, description='Whether the GYAZ Exporter merges materials on export or keeps them as they are')
atlas_name: StringProperty (name='Atlas', default='Atlas', description='Name of the merged material')
def register():
bpy.utils.register_class (PG_GYAZ_Export_MeshProps)
Mesh.gyaz_export = PointerProperty (type=PG_GYAZ_Export_MeshProps)
bpy.utils.register_class (PG_GYAZ_Export_EncodeShapeKeysInUVChannel)
Scene.gyaz_export_shapes = PointerProperty (type=PG_GYAZ_Export_EncodeShapeKeysInUVChannel)
bpy.utils.register_class (Op_GYAZ_Export_EncodeShapeKeysInUVChannels)
# merge materials ui
bpy.types.CYCLES_PT_context_material.append (ui_merge_materials)
bpy.types.EEVEE_MATERIAL_PT_context_material.append (ui_merge_materials)
# uv utils
bpy.utils.register_class (Op_GYAZ_MoveUVMap)
bpy.utils.register_class (Op_GYAZ_MoveVertColor)
bpy.utils.register_class (Op_GYAZ_BatchSetActiveUVMapByIndex)
bpy.utils.register_class (Op_GYAZ_BatchSetActiveUVMapByName)
bpy.utils.register_class (Op_GYAZ_BatchAddUVMap)
bpy.utils.register_class (DATA_MT_GYAZ_UVUtils)
# panel overrides
bpy.utils.register_class (UI_UL_GYAZ_UVMaps)
bpy.utils.register_class (UI_UL_GYAZ_VertexColorList)
bpy.utils.register_class (DATA_PT_uv_texture)
bpy.utils.register_class (DATA_PT_vertex_colors)
def unregister():
bpy.utils.unregister_class (PG_GYAZ_Export_MeshProps)
del Mesh.gyaz_export
bpy.utils.unregister_class (PG_GYAZ_Export_EncodeShapeKeysInUVChannel)
del Scene.gyaz_export_shapes
bpy.utils.unregister_class (Op_GYAZ_Export_EncodeShapeKeysInUVChannels)
# merge materials ui
bpy.types.CYCLES_PT_context_material.remove (ui_merge_materials)
bpy.types.EEVEE_MATERIAL_PT_context_material.remove (ui_merge_materials)
# uv utils
bpy.utils.unregister_class (Op_GYAZ_MoveUVMap)
bpy.utils.unregister_class (Op_GYAZ_MoveVertColor)
bpy.utils.unregister_class (Op_GYAZ_BatchSetActiveUVMapByIndex)
bpy.utils.unregister_class (Op_GYAZ_BatchSetActiveUVMapByName)
bpy.utils.unregister_class (Op_GYAZ_BatchAddUVMap)
bpy.utils.unregister_class (DATA_MT_GYAZ_UVUtils)
# panel overrides (the actual overrides can't be unregistered)
# Blender needs to be restarted for the old panels to exist again
bpy.utils.unregister_class (UI_UL_GYAZ_UVMaps)
bpy.utils.unregister_class (UI_UL_GYAZ_VertexColorList)
bpy.utils.unregister_class (DATA_PT_uv_texture)
bpy.utils.unregister_class (DATA_PT_vertex_colors)