-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
1541 lines (1352 loc) · 47.2 KB
/
__init__.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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ========================================
# SURF/DynaModel Plugin for Blender
#
# Copyright (c) 2016 Mr Mofumofu
# ========================================
import os
import bpy
import bmesh
import mathutils
from bpy.props import (BoolProperty, FloatProperty, StringProperty, EnumProperty)
from bpy_extras.io_utils import (ImportHelper, ExportHelper)
# Infomation
bl_info = {
'name' : 'SURF/DynaModel Format',
'description': 'Import/Export SURF/DynaModel Format.',
'author' : 'Mr Mofumofu',
'version' : (3, 6),
'blender' : (2, 75, 0),
'location' : 'File > Import-Export',
'warning' : '',
'wiki_url' : '',
'category' : 'Import-Export'
}
# Surface Class
class Surface:
# Getting Data
def __init__(self, obj, scene, scale=1.0, parts=False):
self.obj = obj
# Apply Modifier
bpy.context.scene.objects.active = obj
bpy.ops.object.modifier_apply(modifier='EdgeSplit')
# Set Location and Scale
self.location = obj.location
self.scale = scale
# File name
self.name = '{}.srf'.format(self.obj.name)
if parts:
self.name = 'parts/{}.srf'.format(self.obj.name)
# ID
self.uid = SurfMan().getUID()
SurfMan().addUID()
self.children = []
for objs in (ob for ob in obj.children if ob.is_visible(scene) and ob.type == 'MESH'):
self.children.append(SurfMan().getUID())
SurfMan().addList(Surface(objs, scene, scale, parts))
# PCK Node
def pck(self, parts=False, ground=False):
# ==============================
# Getting Data
# ==============================
# Convert to BMesh(For N-Sided Polygon)
bm = bmesh.new()
bm.from_mesh(self.obj.data)
# Transform
ys_matrix = mathutils.Matrix((
(-1.0 * self.scale, 0.0, 0.0, 0.0),
( 0.0, 0.0, 1.0 * self.scale, 0.0),
( 0.0, -1.0 * self.scale, 0.0, 0.0),
( 0.0, 0.0, 0.0, 1.0),
))
bm.transform(ys_matrix * self.obj.matrix_world)
bm.normal_update()
# Set Axis
local_axis = ys_matrix.to_3x3() * self.obj.location
# Vertexs and Faces
verts = bm.verts
faces = bm.faces
# ==============================
# Output
# ==============================
output = ''
za = ''
zacount = 0
# Header
output += 'SURF\n'
# Vertexs
for vert in verts:
vertex = vert.co - local_axis
output += 'V {:.5f} {:.5f} {:.5f} '.format(*vertex)
# Smoothing
for face in vert.link_faces:
if face.smooth:
output += 'R'
break
output += '\n'
# Faces
for face in faces:
output += 'F\n'
# Has Material?
if len(self.obj.material_slots):
# Getting Material
material = self.obj.material_slots[face.material_index].material
# Color
color = material.diffuse_color * 255.0
output += 'C {:.0f} {:.0f} {:.0f}\n'.format(*color)
# Lighting
if material.emit > 0.0:
output += 'B\n'
# Transparent
if material.alpha < 1.0:
if zacount == 0:
za += 'ZA {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
elif zacount % 8 == 0:
za += '\nZA {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
else:
za += ' {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
zacount = zacount + 1
# Median and Normal
median = face.calc_center_median_weighted() - local_axis
normal = -face.normal
if SurfMan().flip:
normal = face.normal
output += 'N {:.5f} {:.5f} {:.5f} '.format(*median)
output += '{:.5f} {:.5f} {:.5f}\n'.format(*normal)
# Vertexs consist Face
output += 'V'
face_verts = ''
for vid in face.verts:
face_verts = ' {:d}{}'.format(vid.index, face_verts)
output += face_verts
output += '\n'
output += 'E\n'
# Footer
output += 'E\n'
# For Transparent
if za != '':
output += za + '\n'
# Finalize
length = len(output.split('\n')) - 1
result = 'PCK {} {:d}\n{}\n'.format(self.name, length, output)
if parts:
result = output
if ground:
result = 'PCK {}.srf {:d}\n{}\n'.format(self.name.split('.')[0], length, output)
# ==============================
# Close
# ==============================
bm.free()
return result
# SRF Node
def srf(self):
# ==============================
# Getting Data
# ==============================
# Transform
ys_matrix = mathutils.Matrix((
(-1.0 * self.scale, 0.0, 0.0, 0.0),
( 0.0, 0.0, 1.0 * self.scale, 0.0),
( 0.0, -1.0 * self.scale, 0.0, 0.0),
( 0.0, 0.0, 0.0, 1.0),
))
# Set Axis
local_axis = ys_matrix.to_3x3() * self.obj.location
local_rotate = [
-self.obj.rotation_euler.z * 10430.37835,
self.obj.rotation_euler.x * 10430.37835,
-self.obj.rotation_euler.y * 10430.37835,
]
# ==============================
# Output
# ==============================
output = ''
# Status
output += 'SRF "{:04d}"\n'.format(self.uid)
output += 'FIL {}\n'.format(self.name)
output += 'CLA 0\n'
output += 'NST 0\n'
# Support Axis Export
if self.obj.parent is not None:
local_axis_parent = ys_matrix.to_3x3() * self.obj.parent.location
local_axis_pos = local_axis - local_axis_parent
if local_axis_parent == (0, 0, 0):
output += 'POS 0.0000 0.0000 0.0000 {:.0f} {:.0f} {:.0f} 1\n'.format(*local_rotate)
output += 'CNT {:.5f} {:.5f} {:.5f}\n'.format(*local_axis)
else:
output += 'POS {:.5f} {:.5f} {:.5f} '.format(*local_axis_pos)
output += '{:.0f} {:.0f} {:.0f} 1\n'.format(*local_rotate)
output += 'CNT 0.0000 0.0000 0.0000\n'
else:
output += 'POS 0.0000 0.0000 0.0000 {:.0f} {:.0f} {:.0f} 1\n'.format(*local_rotate)
output += 'CNT {:.5f} {:.5f} {:.5f}\n'.format(*local_axis)
# Support Parent-Children Relation Export
output += 'REL DEP\n'
output += 'NCH {:d}\n'.format(len(self.children))
for uid in self.children:
output += 'CLD "{:04d}"\n'.format(uid)
output += 'END\n'
return output
# Surface Manager
class SurfMan(object):
_instance = None
_list = []
_saved = []
_uid = 0
flip = False
# Singleton
def __new__(this, *argarray, **argdict):
if this._instance is None:
this._instance = object.__new__(this, *argarray, **argdict)
return this._instance
# Add List
def addList(self, obj):
if not obj.name in self._saved:
self._list.append(obj)
self._saved.append(obj.name)
# Get List
def getList(self):
return self._list
# Add UID
def addUID(self):
self._uid = self._uid + 1
# Get UID
def getUID(self):
return self._uid
# Finalize
def free(self):
self._list = []
self._saved = []
self._uid = 0
# Import SURF
class ImportSRF(bpy.types.Operator, ImportHelper):
# Settings
bl_idname = 'import_model.srf'
bl_label = 'Import SURF'
filter_glob = StringProperty(
default = '*.srf',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.srf'
# On Click Save Button
def execute(self, context):
# Generate
mesh = self.load(context, self.filepath)
obj = bpy.data.objects.new(mesh.name, mesh)
# Currently Scene
scene = bpy.context.scene
scene.objects.link(obj)
scene.update()
return {'FINISHED'}
def load(self, context, filename):
file_path = os.fsencode(filename)
with open(file_path, 'r') as file_stream:
# Stacks
verts = []
faces = []
materials = {}
# Flags
vert_flag = True
face_flag = False
# Temps
mat_tmp = {}
face_tmp = {}
# Reader
for line_raw in file_stream:
# Split with space
line_split = line_raw.rstrip().split(' ')
# Line idents like 'V' and 'F'...
line_ident = line_split[0]
# Vertex
if line_ident == 'V':
if vert_flag:
smoothing = False
if len(line_split) == 5:
smoothing = True
verts.append({
'vert' : [
float(line_split[1]),
float(line_split[2]),
float(line_split[3]),
],
'round' : smoothing,
})
else:
for vert_no in line_split[1:]:
face_tmp['vert'].append(int(vert_no))
# Face
elif line_ident == 'F':
vert_flag = False
face_flag = True
face_tmp['vert'] = []
# Color
elif line_ident == 'C':
if len(line_split) > 2:
mat_tmp = {
'color' : [
int(line_split[1])/255,
int(line_split[2])/255,
int(line_split[3])/255,
],
'bright' : 0.0,
}
else:
c=int(line_split[1]) & 32767
g=((c>>10)&31)/31
r=((c>> 5)&31)/31
b=((c )&31)/31
mat_tmp = {
'color' : [
r,
g,
b,
],
'bright' : 0.0,
}
# Self Brighting
elif line_ident == 'B':
mat_tmp['bright'] = 2.0
# End of Statement
elif line_ident == 'E':
# End of Face
if face_flag:
# Material Matching
if mat_tmp not in materials.values():
# Create Material
materials[len(materials.values()) + 1] = mat_tmp
face_tmp['mats'] = len(materials.values())
# Material Key Finding
else:
for key, var in materials.items():
if var == mat_tmp:
face_tmp['mats'] = key
faces.append(face_tmp)
# Temp Cleaning
face_tmp = {}
mat_tmp = {}
face_flag = False
# End of SURF
else:
break
# Generate Mesh
file_name = bpy.path.display_name_from_filepath(file_path)
mesh = bpy.data.meshes.new(
name = file_name,
)
# Convert Mesh
mesh.from_pydata(
[vert['vert'] for vert in verts],
[],
[vert['vert'] for vert in faces],
)
# Convert Material
for key, var in materials.items():
material = bpy.data.materials.new('{}{}'.format(file_name, key))
material.diffuse_color = [
var['color'][0],
var['color'][1],
var['color'][2],
]
material.emit = var['bright']
mesh.materials.append(material)
# Set Material
for orig, blender in zip(faces, mesh.polygons):
blender.material_index = orig['mats'] - 1
for face_count in orig['vert']:
if verts[face_count - 1]['round']:
blender.use_smooth = True
# Fix Mesh
bm = bmesh.new()
bm.from_mesh(mesh)
# Transform
ys_matrix = mathutils.Matrix((
(-1.0, 0.0, 0.0, 0.0),
( 0.0, 0.0, -1.0, 0.0),
( 0.0, 1.0, 0.0, 0.0),
( 0.0, 0.0, 0.0, -1.0),
))
bm.transform(ys_matrix)
# Calc Normal
bm.normal_update()
# Calc Normal
for face in bm.faces:
face.normal_flip()
bm.to_mesh(mesh)
return mesh
# Import DNM
class ImportDNM(bpy.types.Operator, ImportHelper):
# Settings
bl_idname = 'import_model.dnm'
bl_label = 'Import DNM'
filter_glob = StringProperty(
default = '*.dnm',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.dnm'
# On Click Save Button
def execute(self, context):
# Generate
self.load(context, self.filepath)
# Scene Update
context.scene.update()
return {'FINISHED'}
def load(self, context, filename):
file_path = os.fsencode(filename)
with open(file_path, 'r') as file_stream:
# Stacks
verts = []
faces = []
materials = {}
material_blender = []
# Flags
vert_flag = True
face_flag = False
# Temps
surf_name = ""
mat_tmp = {}
face_tmp = {}
# Reader
for line_raw in file_stream:
# Split with space
line_split = line_raw.rstrip().split(' ')
# Line idents like 'V' and 'F'...
line_ident = line_split[0]
# PCK Node
if line_ident == 'PCK':
surf_name = line_split[1]
# Vertex
elif line_ident == 'V':
if vert_flag:
smoothing = False
if len(line_split) == 5:
smoothing = True
verts.append({
'vert' : [
float(line_split[1]),
float(line_split[2]),
float(line_split[3]),
],
'round' : smoothing,
})
else:
face_tmp['vert'] = [int(vert) for vert in line_split[1:]]
# Face
elif line_ident == 'F':
vert_flag = False
face_flag = True
# Color
elif line_ident == 'C':
if len(line_split) > 2:
mat_tmp = {
'color' : [
int(line_split[1])/255,
int(line_split[2])/255,
int(line_split[3])/255,
],
'bright' : 0.0,
}
else:
c=int(line_split[1]) & 32767
g=((c>>10)&31)/31
r=((c>> 5)&31)/31
b=((c )&31)/31
mat_tmp = {
'color' : [
r,
g,
b,
],
'bright' : 0.0,
}
# Self Brighting
elif line_ident == 'B':
mat_tmp['bright'] = 2.0
# End of Statement
elif line_ident == 'E':
# End of Face
if face_flag:
# Material Matching
if mat_tmp not in materials.values():
# Create Material
materials[len(materials.values()) + 1] = mat_tmp
face_tmp['mats'] = len(materials.values())
# Convert Material
material = bpy.data.materials.new('Material{}'.format(len(materials.values())))
material.diffuse_color = [
mat_tmp['color'][0],
mat_tmp['color'][1],
mat_tmp['color'][2],
]
material.emit = mat_tmp['bright']
material_blender.append(material)
# Material Key Finding
else:
for key, var in materials.items():
if var == mat_tmp:
face_tmp['mats'] = key
faces.append(face_tmp)
# Temp Cleaning
face_tmp = {}
mat_tmp = {}
face_flag = False
# End of SURF
else:
# Generate Mesh
mesh = bpy.data.meshes.new(
name = surf_name.split('.')[0],
)
# Convert Mesh
mesh.from_pydata(
[vert['vert'] for vert in verts],
[],
[vert['vert'] for vert in faces],
)
# Set Material
for orig, blender in zip(faces, mesh.polygons):
blender.material_index = orig['mats'] - 1
for face_count in orig['vert']:
if verts[face_count - 1]['round']:
blender.use_smooth = True
for var in material_blender:
mesh.materials.append(var)
# Fix Mesh
bm = bmesh.new()
bm.from_mesh(mesh)
# Transform
ys_matrix = mathutils.Matrix((
(-1.0, 0.0, 0.0, 0.0),
( 0.0, 0.0, -1.0, 0.0),
( 0.0, 1.0, 0.0, 0.0),
( 0.0, 0.0, 0.0, -1.0),
))
bm.transform(ys_matrix)
# Calc Normal
bm.normal_update()
bm.to_mesh(mesh)
# Create Object
obj = bpy.data.objects.new(mesh.name, mesh)
scene = bpy.context.scene
scene.objects.link(obj)
#Cleaning
verts = []
faces = []
vert_flag = True
face_flag = False
surf_name = ""
return True
# Export SURF
class ExportSRF(bpy.types.Operator, ExportHelper):
# Settings
bl_idname = 'export_model.srf'
bl_label = 'Export SURF'
filter_glob = StringProperty(
default = '*.srf',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.srf'
transform = EnumProperty(
name='Apply Transform(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
flip_normal = EnumProperty(
name='Flip Normal(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
twoside_normal = EnumProperty(
name='Two Side Normal',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
# On Click Save Button
def execute(self, context):
# Apply Transform
if self.transform == 'On':
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
bpy.ops.ed.undo()
# Currently Scene
scene = context.scene
filepath = os.fsencode(self.filepath)
fp = open(filepath, 'w')
# Selected Object
fp.write(self.export(scene.objects.active))
return {'FINISHED'}
def export(self, obj):
# ==============================
# Getting Data
# ==============================
# Convert to BMesh(For N-Sided Polygon)
bm = bmesh.new()
bm.from_mesh(obj.data)
# Transform
ys_matrix = mathutils.Matrix((
(-1.0, 0.0, 0.0, 0.0),
( 0.0, 0.0, 1.0, 0.0),
( 0.0, -1.0, 0.0, 0.0),
( 0.0, 0.0, 0.0, 1.0),
))
bm.transform(ys_matrix * obj.matrix_world)
bm.normal_update()
# Set Axis
local_axis = ys_matrix.to_3x3() * obj.location
# Vertexs and Faces
verts = bm.verts
faces = bm.faces
# ==============================
# Output
# ==============================
output = ''
za = ''
zacount = 0
# Header
output += 'SURF\n'
# Vertexs
for vert in verts:
vertex = vert.co - local_axis
output += 'V {:.5f} {:.5f} {:.5f} '.format(*vertex)
# Smoothing
smooth = True
for edge in vert.link_edges:
if edge.smooth == False:
smooth = False
break
if smooth:
for face in vert.link_faces:
if face.smooth:
output += 'R'
break
output += '\n'
# Faces
for face in faces:
output += 'F\n'
# Has Material?
if len(obj.material_slots):
# Getting Material
material = obj.material_slots[face.material_index].material
# Color
color = material.diffuse_color * 255.0
output += 'C {:.0f} {:.0f} {:.0f}\n'.format(*color)
# Lighting
if material.emit > 0.0:
output += 'B\n'
# Transparent
if material.alpha < 1.0:
if zacount == 0:
za += 'ZA {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
elif zacount % 8 == 0:
za += '\nZA {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
else:
za += ' {:d} {:.0f}'.format(face.index, (1.0 - material.alpha) * 228.0)
zacount = zacount + 1
# Median and Normal
median = face.calc_center_median_weighted() - local_axis
# Flip Normal
if self.flip_normal == 'On':
normal = face.normal
else:
normal = -face.normal
if self.twoside_normal == 'On':
output += 'N {:.5f} {:.5f} {:.5f} '.format(*median)
output += '0.000 0.000 0.000\n'
else:
output += 'N {:.5f} {:.5f} {:.5f} '.format(*median)
output += '{:.5f} {:.5f} {:.5f}\n'.format(*normal)
# Vertexs consist Face
output += 'V'
face_verts = ''
for vid in face.verts:
face_verts = ' {:d}{}'.format(vid.index, face_verts)
output += face_verts
output += '\n'
output += 'E\n'
# Footer
output += 'E\n'
# For Transparent
if za != '':
output += za + '\n'
# ==============================
# Close
# ==============================
bm.free()
return output
# Export DNM
class ExportDNM(bpy.types.Operator, ExportHelper):
# Settings
bl_idname = 'export_model.dnm'
bl_label = 'Export DNM Model'
filter_glob = StringProperty(
default = '*.dnm',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.dnm'
transform = EnumProperty(
name='Apply Transform(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
flip_normal = EnumProperty(
name='Flip Normal(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
scale = FloatProperty(
name='Scale',
subtype='UNSIGNED',
unit='LENGTH',
default=1.0,
)
# On Click Save Button
def execute(self, context):
# ==============================
# Getting Data
# ==============================
# Apply Transform
if self.transform == 'On':
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
bpy.ops.ed.undo()
# Flip Normal
if self.flip_normal == 'On':
SurfMan().flip = True
# Currently Scene
scene = context.scene
# Selected Object
for obj in (ob for ob in scene.objects if ob.is_visible(scene) and ob.type == 'MESH'):
if obj.type == 'MESH':
SurfMan().addList(Surface(obj, scene, self.scale))
# ==============================
# Output
# ==============================
# Save File
filepath = os.fsencode(self.filepath)
fp = open(filepath, 'w')
# Header
fp.write('DYNAMODEL\n')
fp.write('DNMVER 1\n')
# PCK Node
for surf in SurfMan().getList():
fp.write(surf.pck())
# SRF Node
for surf in SurfMan().getList():
fp.write(surf.srf())
# Footer
fp.write('END\n')
# ==============================
# Close
# ==============================
SurfMan().free()
fp.close()
return {'FINISHED'}
# Export PCK
class ExportPCK(bpy.types.Operator, ExportHelper):
# Settings
bl_idname = 'export_model.pck'
bl_label = 'Export PCK Node'
filter_glob = StringProperty(
default = '*.dnm',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.dnm'
transform = EnumProperty(
name='Apply Transform(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
flip_normal = EnumProperty(
name='Flip Normal(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
scale = FloatProperty(
name='Scale',
subtype='UNSIGNED',
unit='LENGTH',
default=1.0,
)
# On Click Save Button
def execute(self, context):
# ==============================
# Getting Data
# ==============================
# Apply Transform
if self.transform == 'On':
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
bpy.ops.ed.undo()
# Flip Normal
if self.flip_normal == 'On':
SurfMan().flip = True
# Currently Scene
scene = context.scene
# Selected Object
for obj in (ob for ob in scene.objects if ob.is_visible(scene) and ob.type == 'MESH'):
if obj.type == 'MESH':
SurfMan().addList(Surface(obj, scene, self.scale))
# ==============================
# Output
# ==============================
# Save File
filepath = os.fsencode(self.filepath)
fp = open(filepath, 'w')
# Header
fp.write('DYNAMODEL\n')
fp.write('DNMVER 1\n')
# PCK Node
for surf in SurfMan().getList():
fp.write(surf.pck())
# ==============================
# Close
# ==============================
SurfMan().free()
fp.close()
return {'FINISHED'}
# Explode DNM
class ExplodeDNM(bpy.types.Operator, ExportHelper):
# Settings
bl_idname = 'explode_model.dnm'
bl_label = 'Explode DNM Model'
filter_glob = StringProperty(
default = '*.dnm',
options = {'HIDDEN'},
)
check_extension = True
filename_ext = '.dnm'
transform = EnumProperty(
name='Apply Transform(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
flip_normal = EnumProperty(
name='Flip Normal(Fix)',
items=(
('On', 'On', ''),
('Off', 'Off', ''),
),
default='Off',
)
scale = FloatProperty(
name='Scale',
subtype='UNSIGNED',
unit='LENGTH',
default=1.0,
)
# On Click Save Button
def execute(self, context):
# ==============================
# Getting Data
# ==============================
# Apply Transform
if self.transform == 'On':
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.transform_apply(location=False, rotation=True, scale=True)
bpy.ops.ed.undo()
# Flip Normal
if self.flip_normal == 'On':
SurfMan().flip = True
# Currently Scene
scene = context.scene
# Selected Object
for obj in (ob for ob in scene.objects if ob.is_visible(scene) and ob.type == 'MESH'):
if obj.type == 'MESH':
SurfMan().addList(Surface(obj, scene, self.scale, True))
# ==============================
# Output
# ==============================
# Save File
filepath = os.fsencode(self.filepath)
fp = open(filepath, 'w')
# Header
fp.write('DYNAMODEL\n')
fp.write('DNMVER 1\n')
# PCK Node
for surf in SurfMan().getList():
filepath = os.fsencode('{}/{}'.format(os.path.dirname(self.filepath), surf.name))
os.makedirs(os.path.dirname(filepath), exist_ok=True)
pck = open(filepath, 'w')