This repository was archived by the owner on Jan 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path__init__.py
More file actions
4771 lines (4036 loc) · 230 KB
/
__init__.py
File metadata and controls
4771 lines (4036 loc) · 230 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ##### 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 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; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Importer and exporter for Blizzard's Starcraft 2 model files (*.m3)",
'author': 'Florian Köberle, netherh, chaos2night',
"location": "Properties Editor -> Scene -> M3 Panels",
"description": "Allows to export (and import) simple Starcraft 2 models (.m3) with particle systems. Use on own risk!",
"category": "Import-Export",
"wiki_url": "http://wiki.blender.org/index.php/Dev:2.5/Py/Scripts/Import-Export/m3addon",
"tracker_url": "https://github.com/flo/m3addon/issues?state=open"
}
if "bpy" in locals():
import imp
if "m3import" in locals():
imp.reload(m3import)
if "m3export" in locals():
imp.reload(m3export)
if "shared" in locals():
imp.reload(shared)
from . import shared
import bpy
from bpy.props import StringProperty
from bpy_extras.io_utils import ExportHelper, ImportHelper
import mathutils
import math
def boneNameSet():
boneNames = set()
for armature in bpy.data.armatures:
for bone in armature.bones:
boneNames.add(bone.name)
for bone in armature.edit_bones:
boneNames.add(bone.name)
return boneNames
def availableBones(self, context):
sortedBoneNames = []
sortedBoneNames.extend(boneNameSet())
sortedBoneNames.sort()
list = [("", "None", "Not assigned to a bone")]
for boneName in sortedBoneNames:
list.append((boneName,boneName,boneName))
return list
def availableMaterials(self, context):
list = [("", "None", "No Material")]
for material in context.scene.m3_material_references:
list.append((material.name, material.name, material.name))
return list
def updateBoenShapesOfParticleSystemCopies(scene, particleSystem):
for copy in particleSystem.copies:
boneName = copy.boneName
bone, armatureObject = shared.findBoneWithArmatureObject(scene, boneName)
if bone != None:
poseBone = armatureObject.pose.bones[boneName]
shared.updateBoneShapeOfParticleSystem(particleSystem, bone, poseBone)
def handleAttachmentPointTypeOrBoneSuffixChange(self, context):
attachmentPoint = self
scene = context.scene
typeName = "Unknown"
if attachmentPoint.volumeType == "-1":
typeName = "Point"
else:
typeName = "Volume"
boneSuffix = attachmentPoint.boneSuffix
attachmentPoint.name = "%s (%s)" % (boneSuffix, typeName)
currentBoneName = attachmentPoint.boneName
calculatedBoneName = shared.boneNameForAttachmentPoint(attachmentPoint)
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
attachmentPoint.boneName = bone.name
else:
attachmentPoint.boneName = calculatedBoneName
if attachmentPoint.updateBlenderBone:
selectOrCreateBoneForAttachmentPoint(scene, attachmentPoint)
def handleGeometicShapeTypeOrBoneNameUpdate(self, context):
shapeObject = self
scene = context.scene
typeName = "Unknown"
for typeId, name, description in geometricShapeTypeList:
if typeId == shapeObject.shape:
typeName = name
shapeObject.name = "%s (%s)" % (shapeObject.boneName, typeName)
if shapeObject.updateBlenderBone:
selectOrCreateBoneForShapeObject(scene, shapeObject)
def handleParticleSystemTypeOrNameChange(self, context):
particleSystem = self
scene = context.scene
if particleSystem.updateBlenderBoneShapes:
currentBoneName = particleSystem.boneName
calculatedBoneName = shared.boneNameForPartileSystem(particleSystem)
if currentBoneName!= calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
particleSystem.boneName = bone.name
else:
particleSystem.boneName = calculatedBoneName
selectOrCreateBoneForPartileSystem(scene, particleSystem)
updateBoenShapesOfParticleSystemCopies(scene, particleSystem)
def handleParticleSystemCopyRename(self, context):
scene = context.scene
particleSystemCopy = self
currentBoneName = particleSystemCopy.boneName
calculatedBoneName = shared.boneNameForPartileSystemCopy(particleSystemCopy)
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
particleSystemCopy.boneName = bone.name
else:
particleSystemCopy.boneName = calculatedBoneName
def handleRibbonBoneSuffixChange(self, context):
ribbon = self
scene = context.scene
# no type yet to combine in the name
ribbon.name = ribbon.boneSuffix
if ribbon.updateBlenderBoneShapes:
currentBoneName = ribbon.boneName
calculatedBoneName = shared.boneNameForRibbon(ribbon)
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
ribbon.boneName = bone.name
else:
ribbon.boneName = calculatedBoneName
selectOrCreateBoneForRibbon(scene, ribbon)
#TODO for sub ribbons:
# updateBoenShapesOfSubRibbons(scene, ribbon)
def handleParticleSystemAreaSizeChange(self, context):
particleSystem = self
scene = context.scene
if particleSystem.updateBlenderBoneShapes:
selectOrCreateBoneForPartileSystem(scene, particleSystem)
updateBoenShapesOfParticleSystemCopies(scene, particleSystem)
def handleForceTypeOrBoneSuffixChange(self, context):
scene = context.scene
force = self
typeName = "Unknown"
for typeId, name, description in forceTypeList:
if typeId == force.type:
typeName = name
boneSuffix = force.boneSuffix
self.name = "%s (%s)" % (boneSuffix, typeName)
if force.updateBlenderBoneShape:
currentBoneName = force.boneName
calculatedBoneName = shared.boneNameForForce(force)
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
force.boneName = bone.name
else:
force.boneName = calculatedBoneName
selectOrCreateBoneForForce(scene, force)
def handleForceRangeUpdate(self, context):
scene = context.scene
force = self
if force.updateBlenderBoneShape:
selectOrCreateBoneForForce(scene, force)
def handleLightTypeOrBoneSuffixChange(self, context):
scene = context.scene
light = self
typeName = "Unknown"
for typeId, name, description in lightTypeList:
if typeId == light.lightType:
typeName = name
light.name = "%s (%s)" % (light.boneSuffix, typeName)
currentBoneName = light.boneName
calculatedBoneName = shared.boneNameForLight(light)
if light.updateBlenderBone:
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
light.boneName = bone.name
else:
light.boneName = calculatedBoneName
selectOrCreateBoneForLight(scene, light)
def handleProjectionSizeChange(self, context):
projection = self
scene = context.scene
if projection.updateBlenderBone:
selectOrCreateBoneForProjection(scene, projection)
def handleLightSizeChange(self, context):
scene = context.scene
light = self
if light.updateBlenderBone:
selectOrCreateBoneForLight(scene, light)
def handleWarpRadiusChange(self, context):
scene = context.scene
warp = self
if warp.updateBlenderBone:
selectOrCreateBoneForWarp(scene, warp)
def handleProjectionTypeOrBoneSuffixChange(self, context):
scene = context.scene
projection = self
projection.name = projection.boneSuffix
currentBoneName = projection.boneName
calculatedBoneName = shared.boneNameForProjection(projection)
if projection.updateBlenderBone:
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
projection.boneName = bone.name
else:
projection.boneName = calculatedBoneName
selectOrCreateBoneForProjection(scene, projection)
def handleWarpBoneSuffixChange(self, context):
scene = context.scene
warp = self
warp.name = warp.boneSuffix
currentBoneName = warp.boneName
calculatedBoneName = shared.boneNameForWarp(warp)
if warp.updateBlenderBone:
if currentBoneName != calculatedBoneName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, currentBoneName)
if bone != None:
bone.name = calculatedBoneName
warp.boneName = bone.name
else:
warp.boneName = calculatedBoneName
selectOrCreateBoneForWarp(scene, warp)
def handleCameraNameChange(self, context):
scene = context.scene
if self.name != self.oldName:
bone, armatureObject = shared.findBoneWithArmatureObject(scene, self.oldName)
if bone != None:
bone.name = self.name
self.oldName = self.name
def handleDepthBlendFalloffChanged(self, context):
material = self
if material.depthBlendFalloff <= 0.0:
if material.useDepthBlendFalloff:
material.useDepthBlendFalloff = False
else:
if not material.useDepthBlendFalloff:
material.useDepthBlendFalloff = True
def handleUseDepthBlendFalloffChanged(self, context):
material = self
if material.useDepthBlendFalloff:
if material.depthBlendFalloff <= 0.0:
material.depthBlendFalloff = shared.defaultDepthBlendFalloff
else:
if material.depthBlendFalloff != 0.0:
material.depthBlendFalloff = 0.0
def handleMaterialNameChange(self, context):
scene = context.scene
materialName = self.name
materialReferenceIndex = self.materialReferenceIndex
if materialReferenceIndex != -1:
materialReference = scene.m3_material_references[self.materialReferenceIndex]
materialIndex = materialReference.materialIndex
materialType = materialReference.materialType
oldMaterialName = materialReference.name
materialReference.name = materialName
for particle_system in scene.m3_particle_systems:
if particle_system.materialName == oldMaterialName:
particle_system.materialName = materialName
for projection in scene.m3_projections:
if projection.materialName == oldMaterialName:
projection.materialName = materialName
for meshObject in shared.findMeshObjects(scene):
mesh = meshObject.data
if mesh.m3_material_name == oldMaterialName:
mesh.m3_material_name = materialName
def handleMaterialLayerFieldNameChange(self, context):
self.name = layerFieldNameToNameMap.get(fieldName, fieldName)
def handleAttachmentVolumeTypeChange(self, context):
handleAttachmentPointTypeOrBoneSuffixChange(self, context)
if self.volumeType in ["0", "1", "2"]:
if self.volumeSize0 == 0.0:
self.volumeSize0 = 1.0
else:
self.volumeSize0 = 0.0
if self.volumeType in ["0", "2"]:
if self.volumeSize1 == 0.0:
self.volumeSize1 = 1.0
else:
self.volumeSize1 = 0.0
if self.volumeType in ["0"]:
if self.volumeSize2 == 0.0:
self.volumeSize2 = 1.0
else:
self.volumeSize2 = 0.0
def handleAttachmentVolumeSizeChange(self, context):
scene = context.scene
attachmentPoint = self
if attachmentPoint.updateBlenderBone:
selectOrCreateBoneForAttachmentPoint(scene, attachmentPoint)
def handleGeometicShapeUpdate(self, context):
shapeObject = self
if shapeObject.updateBlenderBone:
selectOrCreateBoneForShapeObject(context.scene, shapeObject)
def handleParticleSystemsVisiblityUpdate(self, context):
scene = context.scene
for particleSystem in scene.m3_particle_systems:
boneName = particleSystem.boneName
shared.setBoneVisibility(scene, boneName, self.showParticleSystems)
for copy in particleSystem.copies:
boneName = copy.boneName
shared.setBoneVisibility(scene, boneName, self.showParticleSystems)
def handleRibbonsVisiblityUpdate(self, context):
scene = context.scene
for ribbon in scene.m3_ribbons:
boneName = ribbon.boneName
shared.setBoneVisibility(scene, boneName, self.showRibbons)
# TODO for sub ribbons:
#for subRibbon in ribbon.subRibbons:
# boneName = subRibbon.boneName
# shared.setBoneVisibility(scene, boneName, self.showRibbons)
def handleFuzzyHitTestVisiblityUpdate(self, context):
scene = context.scene
for fuzzyHitTest in scene.m3_fuzzy_hit_tests:
boneName = fuzzyHitTest.boneName
shared.setBoneVisibility(scene, boneName, self.showFuzzyHitTests)
def handleTightHitTestVisiblityUpdate(self, context):
scene = context.scene
tightHitTest = scene.m3_tight_hit_test
boneName = tightHitTest.boneName
shared.setBoneVisibility(scene, boneName, self.showTightHitTest)
def handleAttachmentPointVisibilityUpdate(self, context):
scene = context.scene
for attachmentPoint in scene.m3_attachment_points:
boneName = attachmentPoint.boneName
shared.setBoneVisibility(scene, boneName, self.showAttachmentPoints)
def handleLightsVisiblityUpdate(self, context):
scene = context.scene
for light in scene.m3_lights:
boneName = light.boneName
shared.setBoneVisibility(scene, boneName, self.showLights)
def handleForcesVisiblityUpdate(self, context):
scene = context.scene
for force in scene.m3_forces:
boneName = force.boneName
shared.setBoneVisibility(scene, boneName, self.showForces)
def handleCamerasVisiblityUpdate(self, context):
scene = context.scene
for camera in scene.m3_cameras:
boneName = camera.name
shared.setBoneVisibility(scene, boneName, self.showCameras)
def handlePhysicsShapeVisibilityUpdate(self, context):
scene = context.scene
for rigidBody in scene.m3_rigid_bodies:
boneName = rigidBody.boneName
shared.setBoneVisibility(scene, boneName, self.showPhysicsShapes)
def handleProjectionVisibilityUpdate(self, context):
scene = context.scene
for projection in scene.m3_projections:
boneName = projection.boneName
shared.setBoneVisibility(scene, boneName, self.showProjections)
def handleWarpVisibilityUpdate(self, context):
scene = context.scene
for warp in scene.m3_warps:
boneName = warp.boneName
shared.setBoneVisibility(scene, boneName, self.showWarps)
def getTrackName(m3Animation):
return m3Animation.name + "_full"
def getOrCreateStrip(m3Animation, animationData):
trackName = getTrackName(m3Animation)
track = shared.getOrCreateTrack(animationData, trackName)
if len(track.strips) > 0:
strip = track.strips[0]
else:
stripName = trackName
strip = track.strips.new(name=stripName, start=0,action=animationData.action)
return strip
def handleAnimationChange(targetObject, oldAnimation, newAnimation):
animationData = targetObject.animation_data
oldAction = animationData.action
if oldAction != None and oldAnimation != None:
oldStrip = getOrCreateStrip(oldAnimation, animationData)
oldStrip.action = animationData.action
if newAnimation:
newTrackName = newAnimation.name + "_full"
newTrack = animationData.nla_tracks.get(newTrackName)
if newTrack != None and len(newTrack.strips) > 0:
newStrip = newTrack.strips[0]
newAction = newStrip.action
else:
newAction = None
else:
newAction = None
prepareDefaultValuesForNewAction(targetObject, newAction)
animationData.action = newAction
def handleAnimationSequenceIndexChange(self, context):
scene = self
newIndex = scene.m3_animation_index
oldIndex = scene.m3_animation_old_index
shared.setAnimationWithIndexToCurrentData(scene, oldIndex)
if (newIndex >= 0) and (newIndex < len(scene.m3_animations)):
newAnimation = scene.m3_animations[newIndex]
else:
newAnimation = None
if oldIndex >= 0 and (oldIndex < len(scene.m3_animations)):
oldAnimation = scene.m3_animations[oldIndex]
else:
oldAnimation = None
if newAnimation != None:
scene.frame_start = newAnimation.startFrame
scene.frame_end = newAnimation.exlusiveEndFrame - 1
for targetObject in scene.objects:
animationData = targetObject.animation_data
if animationData != None:
handleAnimationChange(targetObject, oldAnimation, newAnimation)
if scene.animation_data != None:
handleAnimationChange(scene, oldAnimation, newAnimation)
scene.m3_animation_old_index = newIndex
def prepareDefaultValuesForNewAction(objectWithAnimationData, newAction):
oldAnimatedProperties = set()
animationData = objectWithAnimationData.animation_data
if animationData == None:
raise Exception("Must have animation data")
oldAction = animationData.action
if oldAction != None:
for curve in oldAction.fcurves:
oldAnimatedProperties.add((curve.data_path, curve.array_index))
newAnimatedProperties = set()
if newAction != None:
for curve in newAction.fcurves:
newAnimatedProperties.add((curve.data_path, curve.array_index))
defaultAction = shared.getOrCreateDefaultActionFor(objectWithAnimationData)
removedProperties = set()
propertiesBecomingAnimated = newAnimatedProperties.difference(oldAnimatedProperties)
for prop in propertiesBecomingAnimated:
try:
value = getAttribute(objectWithAnimationData, prop[0],prop[1])
propertyExists = True
except:
propertyExists = False
if propertyExists:
shared.setDefaultValue(defaultAction,prop[0],prop[1], value)
else:
print ("Can't find prop %s" % prop[0], prop[1])
removedProperties.add(prop)
propertiesBecomingUnanimated = oldAnimatedProperties.difference(newAnimatedProperties)
if len(removedProperties) > 0:
print("Removing animations for %s since those properties do no longer exist" % removedProperties)
removedCurves = list()
if newAction != None:
for curve in newAction.fcurves:
if (curve.data_path, curve.array_index) in removedProperties:
removedCurves.append(curve)
for removedCurve in removedCurves:
newAction.fcurves.remove(removedCurve)
defaultsToRemove = set()
for curve in defaultAction.fcurves:
prop = (curve.data_path, curve.array_index)
if prop in propertiesBecomingUnanimated:
defaultValue = curve.evaluate(0)
curvePath = curve.data_path
curveIndex = curve.array_index
try:
resolvedObject = objectWithAnimationData.path_resolve(curvePath)
propertyExists = True
except:
propertyExists = False
if propertyExists:
if type(resolvedObject) in [float, int, bool]:
dotIndex = curvePath.rfind(".")
attributeName = curvePath[dotIndex+1:]
resolvedObject = objectWithAnimationData.path_resolve(curvePath[:dotIndex])
setattr(resolvedObject, attributeName, defaultValue)
else:
resolvedObject[curveIndex] = defaultValue
else:
defaultsToRemove.add(prop)
removedDefaultCurves = list()
for curve in defaultAction.fcurves:
if (curve.data_path, curve.array_index) in removedProperties:
removedDefaultCurves.append(curve)
for removedDefaultCurve in removedDefaultCurves:
defaultAction.fcurves.remove(removedDefaultCurve)
def getAttribute(obj, curvePath, curveIndex):
"""Gets the value of an attribute via animation path and index"""
obj = obj.path_resolve(curvePath)
if type(obj) in [float, int, bool]:
return obj
else:
return obj[curveIndex]
def findUnusedParticleSystemName(scene):
usedNames = set()
for particle_system in scene.m3_particle_systems:
usedNames.add(particle_system.name)
for copy in particle_system.copies:
usedNames.add(copy.name)
unusedName = None
counter = 1
while unusedName == None:
suggestedName = "%02d" % counter
if not suggestedName in usedNames:
unusedName = suggestedName
counter += 1
return unusedName
def handlePartileSystemIndexChanged(self, context):
scene = context.scene
if scene.m3_particle_system_index == -1:
return
particleSystem = scene.m3_particle_systems[scene.m3_particle_system_index]
particleSystem.copyIndex = -1
selectOrCreateBoneForPartileSystem(scene, particleSystem)
def handleRibbonIndexChanged(self, context):
scene = context.scene
if scene.m3_ribbon_index == -1:
return
ribbon = scene.m3_ribbons[scene.m3_ribbon_index]
ribbon.endPointIndex = -1
selectOrCreateBoneForRibbon(scene, ribbon)
def handleRibbonEndPointIndexChanged(self, context):
scene = context.scene
ribbon = self
if ribbon.endPointIndex >= 0 and ribbon.endPointIndex < len(ribbon.endPoints):
endPoint = ribbon.endPoints[ribbon.endPointIndex]
selectBoneIfItExists(scene,endPoint.name)
def handleForceIndexChanged(self, context):
scene = context.scene
if scene.m3_force_index == -1:
return
force = scene.m3_forces[scene.m3_force_index]
selectOrCreateBoneForForce(scene, force)
def handlePhysicsShapeUpdate(self, context):
scene = context.scene
if self.updateBlenderBoneShapes:
if scene.m3_rigid_body_index != -1:
rigidBody = scene.m3_rigid_bodies[scene.m3_rigid_body_index]
shared.updateBoneShapeOfRigidBody(scene, rigidBody)
selectCurrentRigidBodyBone(scene)
scene.m3_bone_visiblity_options.showPhysicsShapes = True
def handleRigidBodyIndexChange(self, context):
scene = context.scene
scene.m3_bone_visiblity_options.showPhysicsShapes = True
selectCurrentRigidBodyBone(scene)
def handleRigidBodyBoneChange(self, context):
# TODO: remove custom bone shape for old bone, create custom bone shape for new bone.
# need to save old bone name somehow?
scene = context.scene
selectCurrentRigidBodyBone(scene)
def selectCurrentRigidBodyBone(scene):
if scene.m3_rigid_body_index != -1:
rigidBody = scene.m3_rigid_bodies[scene.m3_rigid_body_index]
selectBone(scene, rigidBody.boneName)
def handleLightIndexChanged(self, context):
scene = context.scene
if scene.m3_light_index == -1:
return
light = scene.m3_lights[scene.m3_light_index]
selectOrCreateBoneForLight(scene, light)
def handleBillboardBehaviorIndexChanged(self, context):
scene = context.scene
if scene.m3_billboard_behavior_index == -1:
return
billboardBehavior = scene.m3_billboard_behaviors[scene.m3_billboard_behavior_index]
selectBoneIfItExists(scene, billboardBehavior.name)
def handleProjectionIndexChanged(self, context):
scene = context.scene
if scene.m3_projection_index == -1:
return
projection = scene.m3_projections[scene.m3_projection_index]
selectOrCreateBoneForProjection(scene, projection)
def handleWarpIndexChanged(self, context):
scene = context.scene
if scene.m3_warp_index == -1:
return
warp = scene.m3_warps[scene.m3_warp_index]
selectOrCreateBoneForWarp(scene, warp)
def handleAttachmentPointIndexChanged(self, context):
scene = context.scene
if scene.m3_attachment_point_index == -1:
return
attachmentPoint = scene.m3_attachment_points[scene.m3_attachment_point_index]
selectOrCreateBoneForAttachmentPoint(scene, attachmentPoint)
def handlePartileSystemCopyIndexChanged(self, context):
scene = context.scene
particleSystem = self
if particleSystem.copyIndex >= 0 and particleSystem.copyIndex < len(particleSystem.copies):
copy = particleSystem.copies[particleSystem.copyIndex]
selectOrCreateBoneForPartileSystemCopy(scene, particleSystem, copy)
def handleCameraIndexChanged(self, context):
scene = context.scene
if scene.m3_camera_index == -1:
return
camera = scene.m3_cameras[scene.m3_camera_index]
selectOrCreateBoneForCamera(scene, camera)
def handleFuzzyHitTestIndexChanged(self, context):
scene = context.scene
if scene.m3_fuzzy_hit_test_index == -1:
return
fuzzyHitTest = scene.m3_fuzzy_hit_tests[scene.m3_fuzzy_hit_test_index]
selectOrCreateBoneForShapeObject(scene, fuzzyHitTest)
def selectOrCreateBoneForAttachmentPoint(scene, attachmentPoint):
scene.m3_bone_visiblity_options.showAttachmentPoints = True
boneName = attachmentPoint.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfAttachmentPoint(attachmentPoint, bone, poseBone)
def selectOrCreateBoneForPartileSystemCopy(scene, particleSystem, copy):
scene.m3_bone_visiblity_options.showParticleSystems = True
boneName = copy.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfParticleSystem(particleSystem, bone, poseBone)
def selectOrCreateBoneForForce(scene, force):
scene.m3_bone_visiblity_options.showForces = True
boneName = force.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfForce(force, bone, poseBone)
return (bone, poseBone)
def selectOrCreateBoneForLight(scene, light):
scene.m3_bone_visiblity_options.showLights = True
boneName = light.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfLight(light, bone, poseBone)
def selectOrCreateBoneForProjection(scene, projection):
scene.m3_bone_visiblity_options.showProjections = True
boneName = projection.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfProjection(projection, bone, poseBone)
def selectOrCreateBoneForWarp(scene, projection):
scene.m3_bone_visiblity_options.showWarps = True
boneName = projection.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfWarp(projection, bone, poseBone)
def selectOrCreateBoneForCamera(scene, camera):
scene.m3_bone_visiblity_options.showCameras = True
selectOrCreateBone(scene, camera.name)
def selectOrCreateBoneForPartileSystem(scene, particle_system):
scene.m3_bone_visiblity_options.showParticleSystems = True
boneName = particle_system.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfParticleSystem(particle_system, bone, poseBone)
def selectOrCreateBoneForRibbon(scene, ribbon):
scene.m3_bone_visiblity_options.showRibbons = True
boneName = ribbon.boneName
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfRibbon(ribbon, bone, poseBone)
def selectOrCreateBoneForShapeObject(scene, shapeObject):
boneName = shapeObject.boneName
if boneName == shared.tightHitTestBoneName:
scene.m3_bone_visiblity_options.showTightHitTest = True
else:
scene.m3_bone_visiblity_options.showFuzzyHitTests = True
bone, poseBone = selectOrCreateBone(scene, boneName)
shared.updateBoneShapeOfShapeObject(shapeObject, bone, poseBone)
def selectBone(scene, boneName):
bone, armature = shared.findBoneWithArmatureObject(scene, boneName)
if bone == None or armature == None:
return
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
armature.select = True
scene.objects.active = armature
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='POSE')
for b in armature.data.bones:
b.select = False
bone.select = True
def removeBone(scene, boneName):
"removes the given bone if it exists"
bone, armatureObject = shared.findBoneWithArmatureObject(scene, boneName)
if bone == None or armatureObject == None:
return
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
armatureObject.select = True
scene.objects.active = armatureObject
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='EDIT')
armature = armatureObject.data
edit_bone = armature.edit_bones[boneName]
armature.edit_bones.remove(edit_bone)
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='POSE')
def selectOrCreateBone(scene, boneName):
"Returns the bone and it's pose variant"
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
bone, armatureObject = shared.findBoneWithArmatureObject(scene, boneName)
boneExists = bone != None
if boneExists:
armature = armatureObject.data
armatureObject.select = True
scene.objects.active = armatureObject
else:
armatureObject = shared.findArmatureObjectForNewBone(scene)
if armatureObject == None:
armature = bpy.data.armatures.new(name="Armature")
armatureObject = bpy.data.objects.new("Armature", armature)
scene.objects.link(armatureObject)
else:
armature = armatureObject.data
armatureObject.select = True
scene.objects.active = armatureObject
bpy.ops.object.mode_set(mode='EDIT')
editBone = armature.edit_bones.new(boneName)
editBone.head = (0, 0, 0)
editBone.tail = (1, 0, 0)
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='POSE')
for boneOfArmature in armature.bones:
isBoneToSelect = boneOfArmature.name == boneName
boneOfArmature.select_head = isBoneToSelect
boneOfArmature.select_tail = isBoneToSelect
boneOfArmature.select = isBoneToSelect
armature.bones.active = bone
scene.objects.active = armatureObject
armatureObject.select = True
for currentBone in armature.bones:
currentBone.select = currentBone.name == boneName
poseBone = armatureObject.pose.bones[boneName]
bone = armatureObject.data.bones[boneName]
return (bone, poseBone)
def selectBoneIfItExists(scene, boneName):
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='OBJECT')
if bpy.ops.object.select_all.poll():
bpy.ops.object.select_all(action='DESELECT')
bone, armatureObject = shared.findBoneWithArmatureObject(scene, boneName)
armature = armatureObject.data
armatureObject.select = True
scene.objects.active = armatureObject
if bpy.ops.object.mode_set.poll():
bpy.ops.object.mode_set(mode='POSE')
scene.objects.active = armatureObject
armatureObject.select = True
for currentBone in armature.bones:
currentBone.select = currentBone.name == boneName
def determineLayerNames(defaultSetting):
from . import m3
settingToStructureNameMap = {
defaultSettingMesh: "MAT_",
defaultSettingParticle: "MAT_",
defaultSettingCreep: "CREP",
defaultSettingDisplacement: "DIS_",
defaultSettingComposite: "CMP_",
defaultSettingVolume: "VOL_",
defaultSettingVolumeNoise: "VON_",
defaultSettingTerrain: "TER_",
defaultSettingSplatTerrainBake: "STBM",
defaultSettingLensFlare: "LFLR"
}
structureName = settingToStructureNameMap[defaultSetting]
structureDescription = m3.structures[structureName].getNewestVersion()
for field in structureDescription.fields:
if hasattr(field, "referenceStructureDescription"):
if field.historyOfReferencedStructures.name == "LAYR":
yield shared.getLayerNameFromFieldName(field.name)
def finUnusedMaterialName(scene):
usedNames = set()
for materialReferenceIndex in range(0, len(scene.m3_material_references)):
materialReference = scene.m3_material_references[materialReferenceIndex]
material = shared.getMaterial(scene, materialReference.materialType, materialReference.materialIndex)
if material != None:
usedNames.add(material.name)
unusedName = None
counter = 1
while unusedName == None:
suggestedName = "%02d" % counter
if not suggestedName in usedNames:
unusedName = suggestedName
counter += 1
return unusedName
def createMaterial(scene, materialName, defaultSetting):
layerNames = determineLayerNames(defaultSetting)
if defaultSetting in [defaultSettingMesh, defaultSettingParticle]:
materialType = shared.standardMaterialTypeIndex
materialIndex = len(scene.m3_standard_materials)
material = scene.m3_standard_materials.add()
for layerName in layerNames:
layer = material.layers.add()
layer.name = layerName
if layerName == "Diffuse":
if defaultSetting != defaultSettingParticle:
layer.colorChannelSetting = shared.colorChannelSettingRGBA
if defaultSetting == defaultSettingParticle:
material.unfogged = True
material.blendMode = "2"
material.layerBlendType = "2"
material.emisBlendType = "2"
material.noShadowsCast = True
material.noHitTest = True
material.noShadowsReceived = True
material.forParticles = True
material.useDepthBlendFalloff = True
material.depthBlendFalloff = shared.defaultDepthBlendFalloff
material.unknownFlag0x2 = True
material.unknownFlag0x8 = True
elif defaultSetting == defaultSettingDisplacement:
materialType = shared.displacementMaterialTypeIndex
materialIndex = len(scene.m3_displacement_materials)
material = scene.m3_displacement_materials.add()
for layerName in layerNames:
layer = material.layers.add()
layer.name = layerName
elif defaultSetting == defaultSettingComposite:
materialType = shared.compositeMaterialTypeIndex
materialIndex = len(scene.m3_composite_materials)
material = scene.m3_composite_materials.add()
# has no layers
elif defaultSetting == defaultSettingTerrain:
materialType = shared.terrainMaterialTypeIndex
materialIndex = len(scene.m3_terrain_materials)
material = scene.m3_terrain_materials.add()
for layerName in layerNames:
layer = material.layers.add()
layer.name = layerName
elif defaultSetting == defaultSettingVolume:
materialType = shared.volumeMaterialTypeIndex
materialIndex = len(scene.m3_volume_materials)
material = scene.m3_volume_materials.add()
for layerName in layerNames:
layer = material.layers.add()
layer.name = layerName
elif defaultSetting == defaultSettingVolumeNoise:
materialType = shared.volumeNoiseMaterialTypeIndex