Advertisement
Guest User

seanim_import.ms

a guest
Nov 30th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.68 KB | None | 0 0
  1. if (heapSize < 200000) then
  2.         heapSize = 2000000
  3.  
  4. fn readFixedString bstream fixedLen =
  5. (
  6.     local str = ""
  7.     for i = 1 to fixedLen do (
  8.     str += bit.intAsChar (ReadByte bstream #unsigned))
  9.     str
  10. )
  11.  
  12. fn ReadSuperString f =
  13. (
  14.         local str = "" , ch, b
  15.         while true do
  16.         (
  17.             b = readbyte f
  18.             if (b == 0) then return str
  19.             append str (bit.intaschar b)
  20.         )
  21. )
  22.  
  23. fn SkipStream f d =
  24. (
  25.     for x = 1 to d do
  26.     (
  27.         ReadByte f
  28.     )
  29. )
  30.  
  31. fn ReadPoint f =
  32. (
  33.     local x = readfloat f
  34.     local z = readfloat f
  35.     local y = readfloat f
  36.     return [x, y, z]
  37. )
  38.  
  39. fn ReadPointD f =
  40. (
  41.     local x = ReadDouble f
  42.     local z = ReadDouble f
  43.     local y = ReadDouble f
  44.     return [x, y, z]
  45. )
  46.  
  47. fn ReadVertexColor f =
  48. (
  49.     local x = ReadByte f
  50.     local z = ReadByte f
  51.     local y = ReadByte f
  52.     local w = ReadByte f
  53.     return [x, y, z, w]
  54. )
  55.  
  56. fn ReadQuat f =
  57. (
  58.     local x = readfloat f
  59.     local z = readfloat f
  60.     local y = readfloat f
  61.     local w = readfloat f
  62.     return [x, y, z, w]
  63. )
  64.  
  65. fn GetNodeParents node = if isvalidnode node do  
  66. (
  67.     --Print ("GEG")
  68.     local parents = #()
  69.     while node.parent != undefined do
  70.     (
  71.         append parents node.parent
  72.         --Print("Append is " + node.parent as string)
  73.         node  = node.parent
  74.     )
  75.     return parents
  76. )
  77.  
  78. fn ReadQuatD f =
  79. (
  80.     local x = ReadDouble f
  81.     local z = ReadDouble f
  82.     local y = ReadDouble f
  83.     local w = ReadDouble f
  84.     return [x, y, z, w]
  85. )
  86.  
  87. fname = getOpenFileName \
  88. caption:"Open .seanim animation file" \
  89. types:"SEAnim|*.seanim" \
  90. historyCategory:"SEMODELObjectPresets"
  91.  
  92. f = fopen fname "rb"
  93.  
  94. struct frame_data
  95. (
  96.     translations,rotations,scales
  97. )
  98.  
  99. struct bone_data
  100. (
  101.     BoneName,KeyFrames,BoneModif
  102. )
  103.  
  104. clearlistener()
  105.  
  106. Magic = readFixedString f 6
  107.  
  108. if (Magic == "SEAnim") then
  109. (
  110.     local Version = ReadShort f
  111.     local HeaderSize = ReadShort f
  112.     local AnimType = ReadByte f #unsigned
  113.     local AnimFlags = ReadByte f #unsigned
  114.  
  115.     local Looping = (bit.and AnimFlags 1 )
  116.  
  117.     local DataPresentFlags = ReadByte f #unsigned
  118.     local DataPropertyFlags = ReadByte f #unsigned
  119.  
  120.     -- skip 2 bytes
  121.     SkipStream f 2
  122.  
  123.     local FrameRate = ReadFloat f
  124.     local NumFrames = ReadLong f
  125.     local NumBones = ReadLong f
  126.     local NumMods = ReadByte f
  127.  
  128.     local DeltaTagName = ""
  129.     local BoneArray = #()
  130.  
  131.     local AnimationBoneModifiers = #()
  132.  
  133.    
  134.  
  135.     fn GetBoneModif bone =
  136.     (
  137.         local parents = GetNodeParents bone
  138.  
  139.         if parents == undefined then return 0
  140.  
  141.         for bn in parents do
  142.             for br in BoneArray do
  143.                 if (stricmp br.BoneName bn.name) == 0 then return br.BoneModif
  144.        
  145.         return 0
  146.     )
  147.  
  148.     -- skip 3 bytes
  149.     SkipStream f 3
  150.  
  151.     local NumNotes = ReadLong f
  152.  
  153.     for i = 1 to NumBones do
  154.     (
  155.         str = ReadSuperString f
  156.         bone = (bone_data BoneName:str KeyFrames:#() BoneModif:0 )
  157.         append BoneArray bone
  158.     )
  159.         --If we're delta, set delta name
  160.    if AnimType == 3 then DeltaTagName = BoneNames[1]
  161.  
  162.    for i = 1 to NumMods do
  163.    (
  164.        local bone_idx = if NumBones <= 0xFF then ReadByte f else ReadShort f
  165.        BoneArray[bone_idx +1].BoneModif = ReadByte f
  166.    )
  167.  
  168.    for i = 1 to NumBones do
  169.    (
  170.        local BoneFlags = ReadByte f -- Read bone flags (unused)
  171.  
  172.        if(bit.and DataPresentFlags 1) then
  173.        (
  174.            local NumTranslations = 0
  175.  
  176.            if (NumFrames <= 0xFF) then
  177.            (
  178.                NumTranslations = ReadByte f #unsigned
  179.            )
  180.            else if (NumFrames <= 0xFFFF) then
  181.            (
  182.                NumTranslations = ReadShort f #unsigned
  183.            )
  184.            else
  185.            (
  186.                NumTranslations = ReadLong f #unsigned
  187.            )
  188.  
  189.            for ii = 1 to NumTranslations do
  190.            (
  191.                local KeyFrame = -1
  192.                if (NumFrames <= 0xFF) then
  193.                (
  194.                    KeyFrame = (ReadByte f #unsigned) + 1
  195.                )
  196.                else if (NumFrames <= 0xFFFF) then
  197.                (
  198.                    KeyFrame = (ReadShort f #unsigned) + 1
  199.                )
  200.                else
  201.                (
  202.                    KeyFrame = (ReadLong f #unsigned) + 1
  203.                )
  204.  
  205.                if BoneArray[i].KeyFrames[KeyFrame] == undefined then
  206.                (
  207.                    --frame = frame_data (translations:undefined rotations:undefined scales:undefined)
  208.                     frame = frame_data()
  209.                    BoneArray[i].KeyFrames[KeyFrame] = frame
  210.                )
  211.  
  212.                if(bit.and DataPropertyFlags 1) then
  213.                (
  214.                    BoneArray[i].KeyFrames[KeyFrame].translations = ReadPointD f
  215.                )
  216.                else
  217.                (
  218.                    BoneArray[i].KeyFrames[KeyFrame].translations = ReadPoint f
  219.                )
  220.            )
  221.        )
  222.  
  223.        if(bit.and DataPresentFlags 2) then
  224.        (
  225.            local NumRotations = 0
  226.  
  227.            if (NumFrames <= 0xFF) then
  228.            (
  229.                NumRotations = ReadByte f #unsigned
  230.            )
  231.            else if (NumFrames <= 0xFFFF) then
  232.            (
  233.                NumRotations = ReadShort f #unsigned
  234.            )
  235.            else
  236.            (
  237.                NumRotations = ReadLong f #unsigned
  238.            )
  239.  
  240.            for ii = 1 to NumRotations do
  241.            (
  242.                local KeyFrame = -1
  243.                if (NumFrames <= 0xFF) then
  244.                (
  245.                    KeyFrame = (ReadByte f #unsigned) + 1
  246.                )
  247.                else if (NumFrames <= 0xFFFF) then
  248.                (
  249.                    KeyFrame = (ReadShort f #unsigned) + 1
  250.                )
  251.                else
  252.                (
  253.                    KeyFrame = (ReadLong f #unsigned) + 1
  254.                )
  255.  
  256.                if BoneArray[i].KeyFrames[KeyFrame] == undefined then
  257.                (
  258.                    --frame = frame_data (translations:undefined rotations:undefined scales:undefined)
  259.                     frame = frame_data()
  260.                    BoneArray[i].KeyFrames[KeyFrame] = frame
  261.                )
  262.  
  263.                if(bit.and DataPropertyFlags 1) then
  264.                (
  265.                    BoneArray[i].KeyFrames[KeyFrame].rotations = ReadQuatD f
  266.                )
  267.                else
  268.                (
  269.                    BoneArray[i].KeyFrames[KeyFrame].rotations = ReadQuat f
  270.                )
  271.            )
  272.        )
  273.  
  274.        if(bit.and DataPresentFlags 4) then
  275.        (
  276.            local NumScales = 0
  277.  
  278.            if (NumFrames <= 0xFF) then
  279.            (
  280.                NumScales = ReadByte f #unsigned
  281.            )
  282.            else if (NumFrames <= 0xFFFF) then
  283.            (
  284.                NumScales = ReadShort f #unsigned
  285.            )
  286.            else
  287.            (
  288.                NumScales = ReadLong f #unsigned
  289.            )
  290.  
  291.            for ii = 1 to NumScales do
  292.            (
  293.                local KeyFrame = -1
  294.                if (NumFrames <= 0xFF) then
  295.                (
  296.                    KeyFrame = (ReadByte f #unsigned) + 1
  297.                )
  298.                else if (NumFrames <= 0xFFFF) then
  299.                (
  300.                    KeyFrame = (ReadShort f #unsigned) + 1
  301.                )
  302.                else
  303.                (
  304.                    KeyFrame = (ReadLong f #unsigned) + 1
  305.                )
  306.  
  307.                if BoneArray[i].KeyFrames[KeyFrame] == undefined then
  308.                (
  309.                    --frame = frame_data (translations:undefined rotations:undefined scales:undefined)
  310.                    frame = frame_data()
  311.                    BoneArray[i].KeyFrames[KeyFrame] = frame
  312.                )
  313.  
  314.                if(bit.and DataPropertyFlags 1) then
  315.                (
  316.                    BoneArray[i].KeyFrames[KeyFrame].scales = ReadPointD f
  317.                )
  318.                else
  319.                (
  320.                    BoneArray[i].KeyFrames[KeyFrame].scales = ReadPoint f
  321.                )
  322.            )
  323.        )
  324.  
  325.        --Print("Sucess!")
  326.  
  327.    )
  328.  
  329.    for i = 1 to NumNotes do
  330.    (
  331.        local KeyFrame = -1
  332.        if (NumFrames <= 0xFF) then
  333.        (
  334.            KeyFrame = (ReadByte f #unsigned) + 1
  335.        )
  336.        else if (NumFrames <= 0xFFFF) then
  337.        (
  338.            KeyFrame = (ReadShort f #unsigned) + 1
  339.        )
  340.        else
  341.        (
  342.            KeyFrame = (ReadLong f #unsigned) + 1
  343.        )
  344.  
  345.        ReadSuperString f
  346.    )
  347.  
  348.    --Print(BoneArray[1] as string)
  349.    --Print(BoneArray[1].KeyFrames.Count as string)
  350.    
  351.    -- for x = 1 to BoneArray[1].KeyFrames.Count do
  352.    -- (
  353.    --     Print(BoneArray[1].KeyFrames[x] as string)
  354.    -- )
  355.  
  356.    local ctime, _pos, _rot, _scale
  357.  
  358.    for i = 1 to BoneArray.Count do
  359.    (
  360.        current_bone = undefined
  361.  
  362.        for xo in selection do
  363.            if (stricmp BoneArray[i].BoneName xo.name) == 0 then (current_bone = xo ; exit)
  364.        
  365.        local ANM_TYPE = GetBoneModif current_bone
  366.        
  367.        print( ANM_TYPE as string)
  368.  
  369.        --ctr = for j = 1 to 3 collect linear_float ()
  370.        _pos = Position_XYZ ()
  371.        _rot = TCB_rotation ()
  372.        _scale = linear_scale ()
  373.        ctime = TCB_float ()
  374.  
  375.  
  376.        --Print("DEBUG")
  377.  
  378.  
  379.        
  380.        for x = 1 to BoneArray[i].KeyFrames.Count do
  381.        (
  382.            if BoneArray[i].KeyFrames[x] != undefined then
  383.            (
  384.  
  385.                local pos = if BoneArray[i].KeyFrames[x].translations != undefined then BoneArray[i].KeyFrames[x].translations else Point3 0 0 0
  386.                local rot = if BoneArray[i].KeyFrames[x].rotations != undefined then Quat BoneArray[i].KeyFrames[x].rotations.x BoneArray[i].KeyFrames[x].rotations.y BoneArray[i].KeyFrames[x].rotations.z BoneArray[i].KeyFrames[x].rotations.w else Quat 0 0 0 1
  387.                local scal = if BoneArray[i].KeyFrames[x].scales != undefined then BoneArray[i].KeyFrames[x].scales else Point3 1 1 1
  388.                --if current_bone.parent != undefined then rot *= current_bone.parent.rotation.controller.value
  389.  
  390.                animate on at time (x-1) _pos.value = pos
  391.                animate on at time (x-1) _rot.value = rot
  392.                animate on at time (x-1) _scale.value = scal
  393.  
  394.                addNewKey ctime (x-1)
  395.  
  396.                animate on at time (x-1) ctime.value = 1
  397.            )
  398.        )
  399.  
  400.        if current_bone != undefined then
  401.        (
  402.            setBeforeORT current_bone.rotation.controller #constant
  403.            setAfterORT current_bone.rotation.controller #constant
  404.            setBeforeORT current_bone.position.controller #constant
  405.            setAfterORT current_bone.position.controller #constant
  406.            setBeforeORT current_bone.scale.controller #constant
  407.            setAfterORT current_bone.scale.controller #constant
  408.  
  409.            for k in ctime.keys do
  410.            (
  411.                t = k.time
  412.  
  413.                animate on at time t current_bone.rotation.controller.value = _rot.value
  414.                animate on at time t current_bone.position.controller.value = _pos.value
  415.  
  416.                --pb = undefined --current_bone.parent
  417.                --tm = if pb == undefined then matrix3 1 else at time t pb.transform
  418.                --animate on at time t current_bone.transform *= tm
  419.  
  420.                animate on at time t current_bone.scale.controller.value = _scale.value
  421.  
  422.                --addEaseCurve current_bone.rotation.controller.value _rot.value
  423.                --addEaseCurve current_bone.position.controller.value _pos.value
  424.            )
  425.  
  426.            --deletekeys current_bone.scale.controller.keys #allkeys
  427.  
  428.        )
  429.    )
  430. )
  431. else
  432. (
  433.    Print("Bad SEAnim file, magic was invalid")
  434. )
  435.  
  436. fclose f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement