/
FbxRigImporter.script.lua
1262 lines (1091 loc) · 46.4 KB
/
FbxRigImporter.script.lua
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
local rigBuilderRojoVersionFlagExists, rigBuilderRojoVersionFlagValue = pcall(function() return settings():GetFFlag("StudioRigBuilderRojoVersion") end)
if not rigBuilderRojoVersionFlagExists or not rigBuilderRojoVersionFlagValue then
return
end
local RigBuilderFolder = script.Parent.Parent
local GUIsFolder = RigBuilderFolder:WaitForChild("GUIs")
local rigCreator = require(script.Parent.RigCreator)
local toolbar = plugin:CreateToolbar("Animations")
local fbxFlagExists, fbxFlagValue = pcall(function() return settings():GetFFlag("DebugStudioRigImporter") end)
local newR15IntermediateStateExists, newR15IntermediateStateValue = pcall(function() return settings():GetFFlag("AnthroArtistIntentFBXImporterIntermediateState") end)
local anthroProportionsFlagExists, anthroProportionsFlagValue = pcall(function() return settings():GetFFlag("ImporterUseAnthroProportions") end)
local ANTHRO_PROPORTIONS_CLASSIC = "Classic"
local ANTHRO_PROPORTIONS_SLENDER = "ProportionsSlender"
local ANTHRO_PROPORTIONS_NORMAL = "ProportionsNormal"
local R15PartMapping = {
["Head"] = {"Head"},
["Left Leg"] = {"LeftFoot", "LeftLowerLeg", "LeftUpperLeg"},
["Right Leg"] = {"RightFoot", "RightLowerLeg", "RightUpperLeg"},
["Torso"] = {"UpperTorso", "LowerTorso"},
["Right Arm"] = {"RightUpperArm", "RightLowerArm", "RightHand"},
["Left Arm"] = {"LeftUpperArm", "LeftLowerArm", "LeftHand"}
}
-- If the old CharacterMeshs are imported by ID then this will contain a map in the format:
-- BodyPart.Name => AssetId
local assetIdMap = {}
local function setupTextClearOnFocus(textbox)
-- We do want to clear the text on focus if it is the template text
textbox.Focused:connect(function()
if string.find(textbox.Text, "<") ~= nil then
textbox.Text = ""
end
end)
end
local function textboxListSetup(frame, template, nameList)
table.sort(nameList)
for i = 1, #nameList do
local newTextbox = template:Clone()
newTextbox.Name = nameList[i]
newTextbox.Parent = frame
newTextbox.Position = template.Position + UDim2.new(0, 0, 0, (i-1)*newTextbox.Size.Y.Offset)
newTextbox.Description.Text = nameList[i]
for _, obj in pairs(newTextbox:GetChildren()) do
if obj:IsA("TextBox") then
setupTextClearOnFocus(obj)
end
end
end
template:Destroy()
end
local function createConfigureGui(R15Folder, R6Folder, R15FixedFolder, R15LegacyFolder)
local screenGui = GUIsFolder.FBXimport:Clone()
screenGui.Parent = game:GetService("CoreGui")
local rigConfigurationFrame = screenGui.RigConfiguration
rigConfigurationFrame.Cancel.MouseButton1Down:connect(function()
screenGui:Destroy()
end)
-- Do initial setup of TextBox fields
local R15TextureIds = rigConfigurationFrame.R15.TextureIDs
local textboxTemplate = R15TextureIds.TextboxTemplate
local nameList = {}
for key, _ in pairs(R15PartMapping) do
table.insert(nameList, key)
end
textboxListSetup(R15TextureIds, textboxTemplate, nameList)
nameList = {}
for _, enum in pairs(Enum.BodyPart:GetEnumItems()) do
table.insert(nameList, enum.Name)
end
local R6CharaterMeshes = rigConfigurationFrame.R6.CharacterMeshIDs
textboxTemplate = R6CharaterMeshes.TextboxTemplate
textboxListSetup(R6CharaterMeshes, textboxTemplate, nameList)
local R6TextureTextboxes = rigConfigurationFrame.R6.TextureIDs.Textboxes
setupTextClearOnFocus(R6TextureTextboxes.BaseTextureId)
setupTextClearOnFocus(R6TextureTextboxes.OverlayTextureId)
local assetUrls = {
"http://roblox.com/asset/?id=",
"rbxassetid://"
}
local function getTextureIdFromContent(contentId)
for _, assetUrl in pairs(assetUrls) do
if string.find(contentId, assetUrl, 1, true) ~= nil then
return string.sub(contentId, assetUrl:len() + 1)
end
end
return contentId
end
-- Show the current values from the rig
for _, obj in pairs(R15TextureIds:GetChildren()) do
for _, name in pairs(R15PartMapping[obj.Name]) do
local part = R15Folder:FindFirstChild(name)
if part and part:IsA("MeshPart") and part.TextureID ~= "" then
local textureId = getTextureIdFromContent(part.TextureID)
obj.TextureID.Text = textureId
if (not R15FixedFolder and not R15LegacyFolder) then
break
end
end
if (R15FixedFolder) then
local part = R15FixedFolder:FindFirstChild(name)
if part and part:IsA("MeshPart") and part.TextureID ~= "" then
local textureId = getTextureIdFromContent(part.TextureID)
obj.TextureID.Text = textureId
if (not R15LegacyFolder) then
break
end
end
end
if (R15LegacyFolder) then
local part = R15LegacyFolder:FindFirstChild(name)
if part and part:IsA("MeshPart") and part.TextureID ~= "" then
local textureId = getTextureIdFromContent(part.TextureID)
obj.TextureID.Text = textureId
break
end
end
end
end
local R6AssetIdFrame = rigConfigurationFrame.R6.AssetId
R6AssetIdFrame.Visible = true
setupTextClearOnFocus(R6AssetIdFrame.OldAssetId)
for _, obj in pairs(R6Folder:GetChildren()) do
if obj:IsA("CharacterMesh") then
if tostring(obj.BaseTextureId) ~= "" and obj.BaseTextureId ~= 0 then
R6TextureTextboxes.BaseTextureId.Text = tostring(obj.BaseTextureId)
end
if tostring(obj.OverlayTextureId) ~= "" and obj.OverlayTextureId ~= 0 then
R6TextureTextboxes.OverlayTextureId.Text = tostring(obj.OverlayTextureId)
end
if tostring(obj.MeshId) ~= "" and obj.MeshId ~= 0 then
R6CharaterMeshes[obj.BodyPart.Name].MeshId.Text = tostring(obj.MeshId)
end
end
end
rigConfigurationFrame.Done.MouseButton1Down:connect(function()
if R15Folder and R6Folder then
-- Fill the values from the TextBox fields into the rig
for _, obj in pairs(R15TextureIds:GetChildren()) do
local id = obj.TextureID.Text
if id ~= "" and string.find(id, "<") == nil then
local assetString = "http://roblox.com/asset/?id="
if string.find(id, assetString, 1, true) == nil then
id = assetString.. "" ..id
end
for _, name in pairs(R15PartMapping[obj.Name]) do
local part = R15Folder:FindFirstChild(name)
if part and part:IsA("MeshPart") then
part.TextureID = id
end
if (R15FixedFolder) then
local part = R15FixedFolder:FindFirstChild(name)
if part and part:IsA("MeshPart") then
part.TextureID = id
end
end
if (R15LegacyFolder) then
local part = R15LegacyFolder:FindFirstChild(name)
if part and part:IsA("MeshPart") then
part.TextureID = id
end
end
end
end
end
local R6TextureTextboxes = rigConfigurationFrame.R6.TextureIDs.Textboxes
if tonumber(R6AssetIdFrame.OldAssetId.Text) == nil then
for _, enum in pairs(Enum.BodyPart:GetEnumItems()) do
local meshId = tonumber(R6CharaterMeshes[enum.Name].MeshId.Text)
local baseTextureId = tonumber(R6TextureTextboxes.BaseTextureId.Text)
local overlayTextureId = tonumber(R6TextureTextboxes.OverlayTextureId.Text)
if meshId or baseTextureId or overlayTextureId then
local characterMesh = nil
for _, characterObject in pairs(R6Folder:GetChildren()) do
if characterObject:IsA("CharacterMesh") and characterObject.BodyPart == enum then
characterMesh = characterObject
end
end
if not characterMesh then
characterMesh = Instance.new("CharacterMesh")
characterMesh.BodyPart = enum
characterMesh.Parent = R6Folder
end
if meshId then
characterMesh.MeshId = meshId
end
if baseTextureId then
characterMesh.BaseTextureId = baseTextureId
end
if overlayTextureId then
characterMesh.OverlayTextureId = overlayTextureId
end
end
end
else
-- We can get the R6 package information from the asset of the old packages
local R6PackageAssetId = tonumber(R6AssetIdFrame.OldAssetId.Text)
local bodyPartAssetIds = game:GetService("AssetService"):GetAssetIdsForPackage(R6PackageAssetId)
for _, assetId in pairs(bodyPartAssetIds) do
local model = game:GetService("InsertService"):LoadAsset(assetId)
local children = model:GetChildren()
if model:FindFirstChild("R6") then
-- This is the new dual format for packages
children = model.R6:GetChildren()
end
for _, child in pairs(children) do
if child:IsA("CharacterMesh") then
assetIdMap[child.BodyPart.Name] = assetId
-- Destroy duplicates of this Character mesh if there are any
for _, existingPart in pairs(R6Folder:GetChildren()) do
if existingPart:IsA("CharacterMesh") and existingPart.BodyPart == child.BodyPart then
existingPart:Destroy()
end
end
child.Parent = R6Folder
end
end
model:Destroy()
end
end
end
screenGui:Destroy()
end)
end
-----------------------------------------------------
--- --- --- TEMP ASSET MIGRATION CODE --- --- --- ---
-----------------------------------------------------
--- DELETE THIS SECTION WHEN REMOVING R15ToArtistIntentAssetMigratorEnabled
local migratorFlagExists, migratorFlagTrue = pcall(function () return settings():GetFFlag("R15ToArtistIntentAssetMigratorEnabled") end)
-- -- We need to shift the top RightUpperArm down by 0.047.
local shoulderShiftY = -0.047 -- Defined by Art Spec (New R15 compared to old R15)
local originalR15UpperArmSize = Vector3.new(1, 1.4, 1); -- See Client\content\avatar\characterR15.rbxm
local newR15UpperArmSize = originalR15UpperArmSize + Vector3.new(0, shoulderShiftY, 0)
local R15MigrationScales =
{
["RightUpperArm"] = newR15UpperArmSize / originalR15UpperArmSize,
["LeftUpperArm"] = newR15UpperArmSize / originalR15UpperArmSize
}
-- -- When we shrink and object is actually shrinks around its 0,0,0. This means we have to shift the part down by
-- -- half the shrink margin to make sure that the part only shrinks from the top.
local R15MigrationPartOffsets = {
["RightUpperArm"] = Vector3.new( 0, (shoulderShiftY / 2), 0),
["LeftUpperArm"] = Vector3.new( 0, (shoulderShiftY / 2), 0),
}
-- --- Resizing using Part.Size does not automatically drag the attachment relatively with the part
local R15AttachmentPositionOffsets = {
["RightShoulderAttachment"] = Vector3.new( 0, (shoulderShiftY / 2), 0),
["LeftShoulderAttachment"] = Vector3.new( 0, (shoulderShiftY / 2), 0),
}
-- -- Old R15 Spec to new R15 Spec moves the RootRigJoint down by 0.350 (to the hip rig joints)
local r15AnthroMigrationAdjust = Vector3.new(0, -0.350, 0)
-- -- JOINT POSITION VERIFICATION MAP -- --
-- -- This is technically Duplicate date (some of it anyway) to a map used in the
-- -- FBX importer above.
local r15FixedAttachmentVerificationMap = {
["RootRigAttachment"] = Vector3.new(0, 0, 0),
["WaistRigAttachment"] = Vector3.new(0, 0.4, 0),
["NeckRigAttachment"] = Vector3.new(0, 2, 0),
["LeftShoulderRigAttachment"] = Vector3.new(-1, 1.763, 0),
["RightShoulderRigAttachment"] = Vector3.new(1, 1.763, 0),
["LeftElbowRigAttachment"] = Vector3.new(-1.5, 1.035, 0),
["RightElbowRigAttachment"] = Vector3.new(1.5, 1.035, 0),
["LeftWristRigAttachment"] = Vector3.new(-1.5, 0.275, 0),
["RightWristRigAttachment"] = Vector3.new(1.5, 0.275, 0),
["LeftHipRigAttachment"] = Vector3.new(-0.5, 0, 0),
["RightHipRigAttachment"] = Vector3.new(0.5, 0, 0),
["LeftKneeRigAttachment"] = Vector3.new(-0.5, -0.822, 0),
["RightKneeRigAttachment"] = Vector3.new(0.5, -0.822, 0),
["LeftAnkleRigAttachment"] = Vector3.new(-0.5, -1.748, 0),
["RightAnkleRigAttachment"] = Vector3.new(0.5, -1.748, 0),
["RightShoulderAttachment"] = Vector3.new(1.5, 1.953, 0),
["LeftShoulderAttachment"] = Vector3.new(-1.5, 1.953, 0),
["FaceCenterAttachment"] = Vector3.new(0, 2.5, 0),
["FaceFrontAttachment"] = Vector3.new(0, 2.5, -0.6),
["HairAttachment"] = Vector3.new(0, 3.1, 0),
["HatAttachment"] = Vector3.new(0, 3.1, 0),
["NeckAttachment"] = Vector3.new(0, 2, 0),
["BodyFrontAttachment"] = Vector3.new(0, 1, -0.5),
["BodyBackAttachment"] = Vector3.new(0, 1, 0.5),
["LeftCollarAttachment"] = Vector3.new(-1, 2, 0),
["RightCollarAttachment"] = Vector3.new(1, 2, 0),
["WaistBackAttachment"] = Vector3.new(0, 0, 0.5),
["WaistCenterAttachment"] = Vector3.new(0, 0, 0),
["WaistFrontAttachment"] = Vector3.new(0, 0, -0.5)
}
-- -- Pre R15Fixed Process Steps
-- -- When migrating packages from R15 to new R15 we must adjust Arms and Hands by 0.047 studs down, before reprojecting attachments.
local function findAdjustArmParts( inst )
local scaleValue = R15MigrationScales[inst.Name]
if inst:IsA("BasePart") and scaleValue then
inst.Size = inst.Size * scaleValue
end
local offsetValue = R15MigrationPartOffsets[inst.Name]
if inst:IsA("BasePart") and offsetValue then
inst.CFrame = inst.CFrame + offsetValue
end
local attachmentOffsetValue = R15AttachmentPositionOffsets[inst.Name]
if inst:IsA("Attachment") and attachmentOffsetValue then
inst.Position = inst.Position + attachmentOffsetValue
end
for i,v in pairs(inst:GetChildren()) do
findAdjustArmParts( v )
end
end
local epsilon = 0.0001
local function verifyAllAttachmentPositionsRecursively( inst )
local attachmentWorldSpace = r15FixedAttachmentVerificationMap[inst.Name]
if attachmentWorldSpace then
local delta = (attachmentWorldSpace - inst.WorldPosition)
if (delta.Magnitude > epsilon) then
spawn(
function() error("Attachment: " .. inst.Name .. " - Parent: " .. tostring(inst.Parent.Name) .. " Position Expected: " .. tostring(attachmentWorldSpace) .. " result " .. tostring(inst.WorldPosition))
end)
wait(1)
end
end
for i,v in pairs(inst:GetChildren()) do
verifyAllAttachmentPositionsRecursively( v )
end
end
local function breakRigJointsRecursive( inst )
if inst:IsA("Motor6D") then
inst:Destroy()
end
for i,v in pairs(inst:GetChildren()) do
breakRigJointsRecursive( v )
end
end
-- -- Main Functions for Shoulder Adjust
local function adjustShouldersForNewR15( model )
breakRigJointsRecursive( model )
findAdjustArmParts( model )
end
local exportModels = nil
local dummyRootPart = nil
-- -- Folder Names -- --
-- -- As Expected in C++ -- --
local r15AIFolderName = "R15ArtistIntent"
local r15FixedFolderName = "R15Fixed"
-- -- Load Package from a Package ID
local function loadPackage(id, processForAnthro)
local partIDs = game:GetService("AssetService"):GetAssetIdsForPackage(id)
local avatarModel = Instance.new("Model", exportModels)
avatarModel.Name = tostring(id)
local R15AIRig
local R15FixedRig
if (processForAnthro == true) then
R15AIRig = Instance.new("Model", avatarModel)
R15AIRig.Name = "R15AIRig"
local AIRoot = dummyRootPart:Clone()
AIRoot.Parent = R15AIRig
R15FixedRig = Instance.new("Model", avatarModel)
R15FixedRig.Name = "R15FixedRig"
local FixedRoot = dummyRootPart:Clone()
FixedRoot.Parent = R15FixedRig
end
for i,partID in pairs(partIDs) do
local model
local success = pcall(function()
model = game:GetService("InsertService"):LoadAsset(partID)
end)
if (success) then
model.Name = tostring(partID)
model.Parent = avatarModel
if (processForAnthro == true) then
local R15 = model:FindFirstChild("R15")
if (R15) then
local R15AI = model:FindFirstChild(r15AIFolderName)
local R15Fixed = model:FindFirstChild(r15FixedFolderName)
if (R15AI) then
R15AI:Destroy()
end
if (R15Fixed) then
R15Fixed:Destroy()
end
R15AI = Instance.new("Folder", model)
R15AI.Name = r15AIFolderName
R15Fixed = Instance.new("Folder", model)
R15Fixed.Name = r15FixedFolderName
for j, inst in pairs(R15:GetChildren()) do
local AIClone = inst:Clone()
AIClone.Parent = R15AIRig
local aiPtr = Instance.new("ObjectValue", AIClone)
aiPtr.Value = R15AI
aiPtr.Name = "AssetPtr"
local FixedClone = inst:Clone()
FixedClone.Parent = R15FixedRig
local fixedPtr = Instance.new("ObjectValue", FixedClone)
fixedPtr.Value = R15Fixed
fixedPtr.Name = "AssetPtr"
end
end
end
end
end
return avatarModel
end
-- -- Hack. Current system expects there to be a String Value to parse with PackageIDs
local function getPackageListFromStringValue()
local screenGui = Instance.new("ScreenGui", game:GetService("CoreGui"))
local textBox = Instance.new("TextBox", screenGui)
textBox.Text = "Paste PackageID CSV here"
textBox.Size = UDim2.new(0, 400, 0, 50)
textBox.Position = UDim2.new(0.5, -200, 0.5, 0)
textBox.BackgroundColor3 = Color3.new(255, 255, 255)
local confirmButton = Instance.new("TextButton", textBox)
confirmButton.Position = UDim2.new(0.25, 0, 1.2, 0)
confirmButton.Size = UDim2.new(0.5, 0, 0.5, 0)
confirmButton.BackgroundColor3 = Color3.new( 50, 50, 50)
confirmButton.Text = "Confirm"
local clicked = false
local stringOut = ""
confirmButton.MouseButton1Click:connect( function()
stringOut = textBox.Text
clicked = true
print(stringOut)
screenGui:Destroy()
end)
while( not clicked ) do
wait()
end
local packageIds = {}
for word in string.gmatch(stringOut, '([^,]+)') do
table.insert(packageIds, tonumber(word))
end
return packageIds
end
-- -- Finds Humanoid Root Parts
local function recursiveFindRootPart( model)
for i,v in pairs(model:GetChildren()) do
local root = recursiveFindRootPart(v)
if (root) then
return root
end
end
if ( model:IsA("BasePart") and model.Name == "HumanoidRootPart" ) then
return model
else
return nil
end
end
-- -- Goes through newly allocated R15 Packages and puts them back into
-- -- Export format.
local function recursivelyTryToRepackRigParts( inst )
if inst:IsA("BasePart") then
for i,v in pairs (inst:GetChildren()) do
if (v:IsA("Motor6D")) then
v:Destroy()
end
end
local assetPtr = inst:FindFirstChild("AssetPtr")
if (assetPtr) then
inst.Parent = assetPtr.Value
assetPtr:Destroy()
end
end
for i,v in pairs(inst:GetChildren()) do
recursivelyTryToRepackRigParts( v )
end
end
local function buildJoint(parentAttachment, partForJointAttachment)
local newMotor = Instance.new("Motor6D")
newMotor.Name = parentAttachment.Name:gsub("RigAttachment", "")
newMotor.Part0 = parentAttachment.Parent
newMotor.Part1 = partForJointAttachment.Parent
newMotor.C0 = parentAttachment.CFrame
newMotor.C1 = partForJointAttachment.CFrame
local oldMotor = partForJointAttachment.Parent:FindFirstChild(newMotor.Name)
while oldMotor do
oldMotor:Destroy()
oldMotor = partForJointAttachment.Parent:FindFirstChild(newMotor.Name)
end
newMotor.Parent = partForJointAttachment.Parent
return newMotor
end
-- Removes old Motor6Ds and builds the rig from the attachments in the parts
-- Call this with nil, HumanoidRootPart
local function buildRigFromAttachments(last, part)
for _, attachment in pairs(part:GetChildren()) do
if attachment:IsA("Attachment") and string.find(attachment.Name, "RigAttachment") then
for _, sibling in pairs(part.Parent:GetChildren()) do
if sibling ~= part and sibling ~= last then
local matchingAttachment = sibling:FindFirstChild(attachment.Name)
if matchingAttachment then
buildJoint(attachment, matchingAttachment)
-- Continue the recursive tree traversal building joints
buildRigFromAttachments(part, matchingAttachment.Parent)
end
end
end
end
end
end
-- -- Moves the entire model by the deltaCFrame by shifting all the part
-- -- CFrames
local function displaceModelRecursively( inst , deltaCFrame, rigged)
if ( inst:IsA("BasePart") ) then
if (not rigged or inst.Name == "HumanoidRootPart") then
inst.CFrame = deltaCFrame * inst.CFrame
end
end
for i,v in pairs(inst:GetChildren()) do
displaceModelRecursively(v, deltaCFrame, rigged)
end
end
-- Old R15 to new R15 Anthro Spec Adjustment Values/Map
-- -- R15 Fixed Rig Attachment Pos Map
local R15FixedRigAttachmentMap = {
["RootRigAttachment"] = Vector3.new( 0, 0, 0), --HumanoidRootPart to LowerTorso
["WaistRigAttachment"] = Vector3.new( 0, 0.4, 0), --UpperTorso to LowerTorso
["NeckRigAttachment"] = Vector3.new( 0, 2.0, 0), --UpperTorso to Head
["LeftShoulderRigAttachment"] = Vector3.new( -1, 1.763, 0), --UpperTorso to LeftShoulder
["RightShoulderRigAttachment"] = Vector3.new( 1, 1.763, 0), --UpperTorso to RightUpperArm
["RightElbowRigAttachment"] = Vector3.new( 1.5, 1.035, 0), --RightUpperArm to RightLowerArm
["RightWristRigAttachment"] = Vector3.new( 1.5, 0.275, 0), --RightLowerArm to RightHand
["LeftElbowRigAttachment"] = Vector3.new( -1.5, 1.035, 0), --LeftUpperArm to LeftLowerArm
["LeftWristRigAttachment"] = Vector3.new( -1.5, 0.275, 0), --LeftLowerArm to LeftHand
["RightHipRigAttachment"] = Vector3.new( 0.5, 0, 0), --LowerTorso to RightUpperLeg
["RightKneeRigAttachment"] = Vector3.new( 0.5, -0.822, 0), --RightUpperLeg to RightLowerLeg
["RightAnkleRigAttachment"] = Vector3.new( 0.5, -1.748, 0), --RightLowerLeg to RightFoot
["LeftHipRigAttachment"] = Vector3.new( -0.5, 0, 0), --LowerTorso to LeftUpperLeg
["LeftKneeRigAttachment"] = Vector3.new( -0.5, -0.822, 0), --LeftUpperLeg to LeftLowerLeg
["LeftAnkleRigAttachment"] = Vector3.new( -0.5, -1.748, 0), --LeftLowerLeg to LeftFoot
["LeftGripAttachment"] = Vector3.new(-1.5, 0, 0),
["LeftShoulderAttachment"] = Vector3.new(-1.5, 1.953, 0),
["RightGripAttachment"] = Vector3.new(1.5, 0, 0),
["RightShoulderAttachment"] = Vector3.new(1.5, 1.953, 0),
["BodyFrontAttachment"] = Vector3.new(0, 1, -0.5),
["BodyBackAttachment"] = Vector3.new(0, 1, 0.5),
["LeftCollarAttachment"] = Vector3.new(-1, 2, 0),
["RightCollarAttachment"] = Vector3.new(1, 2, 0),
["NeckAttachment"] = Vector3.new(0, 2, 0),
["LeftFootAttachment"] = Vector3.new(-0.5, -2, 0),
["RightFootAttachment"] = Vector3.new(0.5, -2, 0),
["WaistCenterAttachment"] = Vector3.new(0, 0, 0),
["WaistFrontAttachment"] = Vector3.new(0, 0, -0.5),
["WaistBackAttachment"] = Vector3.new(0, 0, 0.5),
["FaceCenterAttachment"] = Vector3.new(0, 2.5, 0),
["FaceFrontAttachment"] = Vector3.new(0, 2.5, -0.6),
["HairAttachment"] = Vector3.new(0, 3.1, 0),
["HatAttachment"] = Vector3.new(0, 3.1, 0),
}
local AnthroNormalFixedRigAttachmentMap = {
["RootRigAttachment"] = Vector3.new(0, 0, 0), --HumanoidRootPart to LowerTorso
["WaistRigAttachment"] = Vector3.new(0, 0.488, 0), --UpperTorso to LowerTorso
["NeckRigAttachment"] = Vector3.new(0, 1.807, 0), --UpperTorso to Head
["LeftShoulderRigAttachment"] = Vector3.new(-0.67, 1.612, 0), --UpperTorso to LeftShoulder
["RightShoulderRigAttachment"] = Vector3.new(0.67, 1.612, 0), --UpperTorso to RightUpperArm
["RightElbowRigAttachment"] = Vector3.new(1, 0.592, 0), --RightUpperArm to RightLowerArm
["RightWristRigAttachment"] = Vector3.new(1, -0.331, 0), --RightLowerArm to RightHand
["LeftElbowRigAttachment"] = Vector3.new(-1, 0.592, 0), --LeftUpperArm to LeftLowerArm
["LeftWristRigAttachment"] = Vector3.new(-1, -0.331, 0), --LeftLowerArm to LeftHand
["RightHipRigAttachment"] = Vector3.new(0.325, 0, 0), --LowerTorso to RightUpperLeg
["RightKneeRigAttachment"] = Vector3.new(0.325, -1.431, 0), --RightUpperLeg to RightLowerLeg
["RightAnkleRigAttachment"] = Vector3.new(0.325, -3.031, 0), --RightLowerLeg to RightFoot
["LeftHipRigAttachment"] = Vector3.new(-0.325, 0, 0), --LowerTorso to LeftUpperLeg
["LeftKneeRigAttachment"] = Vector3.new(-0.325, -1.431, 0), --LeftUpperLeg to LeftLowerLeg
["LeftAnkleRigAttachment"] = Vector3.new(-0.325, -3.031, 0), --LeftLowerLeg to LeftFoot
["LeftGripAttachment"] = Vector3.new(-1, -0.606, 0),
["LeftShoulderAttachment"] = Vector3.new(-1, 1.877, 0),
["RightGripAttachment"] = Vector3.new(1, -0.606, 0),
["RightShoulderAttachment"] = Vector3.new(1, 1.877, 0),
["BodyFrontAttachment"] = Vector3.new(0, 0.983, -0.335),
["BodyBackAttachment"] = Vector3.new(0, 0.983, 0.335),
["LeftCollarAttachment"] = Vector3.new(-0.67, 1.807, 0),
["RightCollarAttachment"] = Vector3.new(0.67, 1.807, 0),
["NeckAttachment"] = Vector3.new(0, 1.807, 0),
["LeftFootAttachment"] = Vector3.new(-0.325, -3.602, 0),
["RightFootAttachment"] = Vector3.new(0.325, -3.602, 0),
["WaistCenterAttachment"] = Vector3.new(0, 0, 0),
["WaistFrontAttachment"] = Vector3.new(0, 0, -0.335),
["WaistBackAttachment"] = Vector3.new(0, 0, 0.335),
["FaceCenterAttachment"] = Vector3.new(0, 2.261, 0),
["FaceFrontAttachment"] = Vector3.new(0, 2.261, -0.45),
["HairAttachment"] = Vector3.new(0, 2.807, 0),
["HatAttachment"] = Vector3.new(0, 2.807, 0),
}
local AnthroSlenderFixedRigAttachmentMap = {
["RootRigAttachment"] = Vector3.new(0, 0, 0), --HumanoidRootPart to LowerTorso
["WaistRigAttachment"] = Vector3.new(0, 0.375, 0), --UpperTorso to LowerTorso
["NeckRigAttachment"] = Vector3.new(0, 1.587, 0), --UpperTorso to Head
["LeftShoulderRigAttachment"] = Vector3.new(-0.587, 1.408, 0), --UpperTorso to LeftShoulder
["RightShoulderRigAttachment"] = Vector3.new(0.587, 1.408, 0), --UpperTorso to RightUpperArm
["RightElbowRigAttachment"] = Vector3.new(0.881, 0.49, 0), --RightUpperArm to RightLowerArm
["RightWristRigAttachment"] = Vector3.new(0.881, -0.341, 0), --RightLowerArm to RightHand
["LeftElbowRigAttachment"] = Vector3.new(-0.881, 0.49, 0), --LeftUpperArm to LeftLowerArm
["LeftWristRigAttachment"] = Vector3.new(-0.881, -0.341, 0), --LeftLowerArm to LeftHand
["RightHipRigAttachment"] = Vector3.new(0.31, 0, 0), --LowerTorso to RightUpperLeg
["RightKneeRigAttachment"] = Vector3.new(0.31, -1.332, 0), --RightUpperLeg to RightLowerLeg
["RightAnkleRigAttachment"] = Vector3.new(0.31, -2.715, 0), --RightLowerLeg to RightFoot
["LeftHipRigAttachment"] = Vector3.new(-0.31, 0, 0), --LowerTorso to LeftUpperLeg
["LeftKneeRigAttachment"] = Vector3.new(-0.31, -1.332, 0), --LeftUpperLeg to LeftLowerLeg
["LeftAnkleRigAttachment"] = Vector3.new(-0.31, -2.715, 0), --LeftLowerLeg to LeftFoot
["LeftGripAttachment"] = Vector3.new(-0.881, -0.616, 0),
["LeftShoulderAttachment"] = Vector3.new(-0.881, 1.647, 0),
["RightGripAttachment"] = Vector3.new(0.881, -0.616, 0),
["RightShoulderAttachment"] = Vector3.new(0.881, 1.647, 0),
["BodyFrontAttachment"] = Vector3.new(0, 0.83, -0.294),
["BodyBackAttachment"] = Vector3.new(0, 0.83, 0.293),
["LeftCollarAttachment"] = Vector3.new(-0.587, 1.587, 0),
["RightCollarAttachment"] = Vector3.new(0.587, 1.587, 0),
["NeckAttachment"] = Vector3.new(0, 1.587, 0),
["LeftFootAttachment"] = Vector3.new(-0.31, -3.084, 0),
["RightFootAttachment"] = Vector3.new(0.31, -3.084, 0),
["WaistCenterAttachment"] = Vector3.new(0, 0, 0),
["WaistFrontAttachment"] = Vector3.new(0, 0, -0.294),
["WaistBackAttachment"] = Vector3.new(0, 0, 0.294),
["FaceCenterAttachment"] = Vector3.new(0, 2.042, 0),
["FaceFrontAttachment"] = Vector3.new(0, 2.042, -0.428),
["HairAttachment"] = Vector3.new(0, 2.587, 0),
["HatAttachment"] = Vector3.new(0, 2.587, 0),
}
-- New ANTHRO-BASED R15 UTILS
-- -- Recursively searches for attachment in a model
local function findRigAttachment(model, name)
for i,v in pairs(model:GetChildren()) do
local rigAttachment = findRigAttachment(v, name)
if (rigAttachment ~= nil) then
return rigAttachment
end
end
if (model:IsA("Attachment") and model.Name == name) then
return model
else
return nil
end
end
-- -- We want ALL FBX imports to have the RootRigAttachment be at 0,0,0
local function adjustPackageToMoveRootToOrigin(model, rigged, posOffset)
local rootAttachment = findRigAttachment(model, "RootRigAttachment")
if (rootAttachment) then
local waistJointParent = rootAttachment.Parent
local CFramePos = CFrame.new(rootAttachment.Position) * waistJointParent.CFrame
if (posOffset) then
CFramePos = CFramePos + posOffset
end
displaceModelRecursively( model, CFramePos:inverse(), rigged)
else
spawn( function()
error(model.Name .. " Could Not Find RootRigAttachment ")
end)
end
end
-- -- Shift All Attachments to match the WorldSpace positions of the above map.
-- -- We assume that RootRigAttachment is 0,0,0
local function adjustModelAttachmentsToMatchR15Fixed( inst )
if inst:IsA("Motor6D") then
inst:Destroy() -- Remove Current Character Rigging
elseif inst:IsA("Attachment") then
if (inst.Parent and inst.Parent.Name == "HumanoidRootPart") then
-- Humanoid Root Part should already be properly positioned from Prod specific dummy R15s
-- Skip
print("Don't displace HumanoidRootPart attachment")
else
local attachmentDesiredPos
if anthroProportionsFlagExists and anthroProportionsFlagValue == true then
local rigAttachmentMap
local avatarPartScaleTypeInst = inst.Parent:FindFirstChild("AvatarPartScaleType")
local avatarPartScaleType = avatarPartScaleTypeInst and avatarPartScaleTypeInst.Value or ANTHRO_PROPORTIONS_CLASSIC
if avatarPartScaleType == ANTHRO_PROPORTIONS_NORMAL then
rigAttachmentMap = AnthroNormalFixedRigAttachmentMap
elseif avatarPartScaleType == ANTHRO_PROPORTIONS_SLENDER then
rigAttachmentMap = AnthroSlenderFixedRigAttachmentMap
else
rigAttachmentMap = R15FixedRigAttachmentMap
end
attachmentDesiredPos = rigAttachmentMap[inst.Name]
else
attachmentDesiredPos = R15FixedRigAttachmentMap[inst.Name]
end
if (attachmentDesiredPos) then
-- Found Valid Attachment
local parentPart = inst.Parent
local localSpaceDesiredPos = parentPart.CFrame:inverse() * CFrame.new( attachmentDesiredPos )
inst.Position = localSpaceDesiredPos.p
end
end
end
for i,v in pairs(inst:GetChildren()) do
adjustModelAttachmentsToMatchR15Fixed( v )
end
end
-- Mr Grey R15
local r15DummyId = 516159357
local r15NewSpecDummyId = 1664543044
-- Reprocess Packages Main Functions
local function loadAssetsForAnthroDateUpdate()
-- Cleanup
if (exportModels) then
exportModels:Destroy()
end
local packageIds = getPackageListFromStringValue()
exportModels = Instance.new("Folder", workspace)
exportModels.Name = "ExportModels"
local R15Dummy = game:GetService("InsertService"):LoadAsset(r15NewSpecDummyId):GetChildren()[1]
dummyRootPart = recursiveFindRootPart(R15Dummy)
for i,v in pairs(packageIds) do
print("Processing Package: "..tostring(v))
local package = loadPackage(v, true)
package.Parent = exportModels
local R15AIRig = package:FindFirstChild("R15AIRig")
local R15FixedRig = package:FindFirstChild("R15FixedRig")
if (R15AIRig and R15FixedRig) then
local AIRoot = recursiveFindRootPart(R15AIRig)
buildRigFromAttachments(nil, AIRoot)
AIRoot.Anchored = true
adjustPackageToMoveRootToOrigin( R15AIRig, true, r15AnthroMigrationAdjust)
adjustShouldersForNewR15( R15AIRig )
adjustModelAttachmentsToMatchR15Fixed( R15AIRig )
buildRigFromAttachments(nil, AIRoot)
verifyAllAttachmentPositionsRecursively( R15AIRig )
recursivelyTryToRepackRigParts( R15AIRig )
local FixedRoot = recursiveFindRootPart(R15FixedRig)
buildRigFromAttachments(nil, FixedRoot)
FixedRoot.Anchored = true
adjustPackageToMoveRootToOrigin( R15FixedRig, true, r15AnthroMigrationAdjust)
adjustShouldersForNewR15( R15FixedRig )
adjustModelAttachmentsToMatchR15Fixed( R15FixedRig )
buildRigFromAttachments(nil, FixedRoot)
verifyAllAttachmentPositionsRecursively( R15FixedRig )
recursivelyTryToRepackRigParts( R15FixedRig )
R15AIRig:Destroy()
R15FixedRig:Destroy()
-- Save changes to File
for j, b in pairs (package:GetChildren()) do
local name = b.Name
game.Selection:Set(b:GetChildren())
plugin:PromptSaveSelection(name)
end
end
package:Destroy()
end
end
-- -- We need to shift the top RightUpperArm up by 0.047.
local R15ReverseMigrationScales =
{
["RightUpperArm"] = originalR15UpperArmSize / newR15UpperArmSize,
["LeftUpperArm"] = originalR15UpperArmSize / newR15UpperArmSize
}
-- -- When we shrink and object is actually shrinks around its 0,0,0. This means we have to shift the part down by
-- -- half the shrink margin to make sure that the part only shrinks from the top.
local R15ReverseMigrationPartOffsets = {
["RightUpperArm"] = Vector3.new( 0, (-shoulderShiftY / 2), 0),
["LeftUpperArm"] = Vector3.new( 0, (-shoulderShiftY / 2), 0),
}
-- --- Resizing using Part.Size does not automatically drag the attachment relatively with the part
local R15ReverseAttachmentPositionOffsets = {
["RightShoulderAttachment"] = Vector3.new( 0, (-shoulderShiftY / 2), 0),
["LeftShoulderAttachment"] = Vector3.new( 0, (-shoulderShiftY / 2), 0),
}
-- -- Pre R15Fixed Process Steps
-- -- When migrating packages from R15 to new R15 we must adjust Arms and Hands by 0.047 studs down, before reprojecting attachments.
local function findReverseAdjustArmParts( inst )
local scaleValue = R15ReverseMigrationScales[inst.Name]
if inst:IsA("BasePart") and scaleValue then
inst.Size = inst.Size * scaleValue
end
local offsetValue = R15ReverseMigrationPartOffsets[inst.Name]
if inst:IsA("BasePart") and offsetValue then
inst.CFrame = inst.CFrame + offsetValue
end
local attachmentOffsetValue = R15ReverseAttachmentPositionOffsets[inst.Name]
if inst:IsA("Attachment") and attachmentOffsetValue then
inst.Position = inst.Position + attachmentOffsetValue
end
for i,v in pairs(inst:GetChildren()) do
findReverseAdjustArmParts( v )
end
end
-- R15 New to Old Pos Conversion
-- -- R15 Fixed Rig Attachment Pos Map
local R15OldRigAttachmentMap = {
["RootRigAttachment"] = Vector3.new( 0.000, 0.350, 0.000), --HumanoidRootPart to LowerTorso
["WaistRigAttachment"] = Vector3.new( 0.000, 0.750, 0.000), --UpperTorso to LowerTorso
["NeckRigAttachment"] = Vector3.new( 0.000, 2.000, 0.000), --UpperTorso to Head
["LeftShoulderRigAttachment"] = Vector3.new(-1.250, 1.750, 0.000), --UpperTorso to LeftShoulder
["RightShoulderRigAttachment"] = Vector3.new( 1.250, 1.750, 0.000), --UpperTorso to RightUpperArm
["RightElbowRigAttachment"] = Vector3.new( 1.500, 1.100, 0.000), --RightUpperArm to RightLowerArm
["RightWristRigAttachment"] = Vector3.new( 1.500, 0.300, 0.000), --RightLowerArm to RightHand
["LeftElbowRigAttachment"] = Vector3.new(-1.500, 1.100, 0.000), --LeftUpperArm to LeftLowerArm
["LeftWristRigAttachment"] = Vector3.new(-1.500, 0.300, 0.000), --LeftLowerArm to LeftHand
["RightHipRigAttachment"] = Vector3.new( 0.500, -0.000, 0.000), --LowerTorso to RightUpperLeg
["RightKneeRigAttachment"] = Vector3.new( 0.500, -0.800, 0.000), --RightUpperLeg to RightLowerLeg
["RightAnkleRigAttachment"] = Vector3.new( 0.500, -1.800, 0.000), --RightLowerLeg to RightFoot
["LeftHipRigAttachment"] = Vector3.new(-0.500, -0.000, 0.000), --LowerTorso to LeftUpperLeg
["LeftKneeRigAttachment"] = Vector3.new(-0.500, -0.800, 0.000), --LeftUpperLeg to LeftLowerLeg
["LeftAnkleRigAttachment"] = Vector3.new(-0.500, -1.800, 0.000), --LeftLowerLeg to LeftFoot
}
-- -- Shift All Attachments to match the WorldSpace positions of the above map.
-- -- We assume that RootRigAttachment is 0,0,0
local function adjustModelAttachmentsToMatchR15Legacy( inst )
if inst:IsA("Motor6D") then
inst:Destroy() -- Remove Current Character Rigging
elseif inst:IsA("Attachment") then
local attachmentDesiredPos = R15OldRigAttachmentMap[inst.Name]
if (attachmentDesiredPos) then
-- Found Valid Attachment
local parentPart = inst.Parent
local localSpaceDesiredPos = parentPart.CFrame:inverse() * CFrame.new( attachmentDesiredPos )
inst.Position = localSpaceDesiredPos.p
end
end
for i,v in pairs(inst:GetChildren()) do
adjustModelAttachmentsToMatchR15Legacy( v )
end
end
-----------------------------------------------------
--- --- --- END TEMP ASSET MIGRATION CODE --- --- ---
-----------------------------------------------------
-- Makes a copy of the rig in the export format
local function makeExportVersion(R15Model, R6Model, R15FixedModel, R15LegacyModel)
-- Remove existing export models
for _, model in pairs(game.ServerStorage:GetChildren()) do
if model.Name == "PackageExport" then
model:Destroy()
end
end
local exportModel = Instance.new("Model")
exportModel.Name = "PackageExport"
exportModel.Parent = game.ServerStorage
local function populateExportModel()
-- The model is repopulated every time a property of any descendant changes
exportModel:ClearAllChildren()
for r6Name, r15Names in pairs(R15PartMapping) do
local partFolder = Instance.new("Folder", exportModel)
partFolder.Name = r6Name
if (R15FixedModel) then
local R15subfolder = Instance.new("Folder", partFolder)
R15subfolder.Name = "R15ArtistIntent"
local R15FixedSubfolder = Instance.new("Folder", partFolder)
R15FixedSubfolder.Name = "R15Fixed"
local R15LegacyFolder
if (R15LegacyModel) then
R15LegacyFolder = Instance.new("Folder", partFolder)
R15LegacyFolder.Name = "R15"
end
for _, r15Name in pairs(r15Names) do
local R15part = R15Model:FindFirstChild(r15Name)
if R15part then
R15part:Clone().Parent = R15subfolder
end
local R15FixedPart = R15FixedModel:FindFirstChild(r15Name)
if R15FixedPart then
R15FixedPart:Clone().Parent = R15FixedSubfolder
end
if (R15LegacyModel) then
local R15LegacyPart = R15LegacyModel:FindFirstChild(r15Name)
if (R15LegacyPart) then
R15LegacyPart:Clone().Parent = R15LegacyFolder
end
end
end
else
local R15subfolder = Instance.new("Folder", partFolder)
R15subfolder.Name = "R15"
for _, r15Name in pairs(r15Names) do
local R15part = R15Model:FindFirstChild(r15Name)
if R15part then
R15part:Clone().Parent = R15subfolder
end
end
end
local R6subfolder = Instance.new("Folder", partFolder)
R6subfolder.Name = "R6"
for _, child in pairs(R6Model:GetChildren()) do
if child:IsA("CharacterMesh") then
local R6EnumName = r6Name:gsub("%s+", "")
if child.BodyPart.Name == R6EnumName then
child:Clone().Parent = R6subfolder
end
end