Exporting Animations from Blender & Play them

Exporting Animations from Blender & Play them

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Revolvern
Posts: 2
Joined: 17 Feb 2019, 00:09

Exporting Animations from Blender & Play them

Post by Revolvern » 19 Feb 2019, 14:49

Hello,

Is there any way to export an animation and play it in spring engine? Is there any tutorial? I have tried to get this to work for a month.

Note: No need to answer how to rotate stuff, this is not what I'm asking for. I have a bunch with model's in blender that have been animated and
I'm not planning todo it manually in code.

* The built-in LUA exporter just gives me something like

Code: Select all

return { }
* I have tried other animations then my own, like this one:
https://www.turbosquid.com/FullPreview/ ... ID/1012108
* I have tried blender2lus which gives me a file with a function that is called PlayAnimation, which I can't find used in any other spring mod.

Code: Select all

local Animations = {};
-- you can include externally saved animations like this:
-- Animations['importedAnimation'] = VFS.Include("Scripts/animations/animationscript.lua", scriptEnv)

Animations['idle'] = {

}

function constructSkeleton(unit, piece, offset)
    if (offset == nil) then
        offset = {0,0,0};
    end

    local bones = {};
    local info = Spring.GetUnitPieceInfo(unit,piece);

    for i=1,3 do
        info.offset[i] = offset[i]+info.offset[i];
    end 

    bones[piece] = info.offset;
    local map = Spring.GetUnitPieceMap(unit);
    local children = info.children;

    if (children) then
        for i, childName in pairs(children) do
            local childId = map[childName];
            local childBones = constructSkeleton(unit, childId, info.offset);
            for cid, cinfo in pairs(childBones) do
                bones[cid] = cinfo;
            end
        end
    end        
    return bones;
end

function script.Create()
    local map = Spring.GetUnitPieceMap(unitID);
    local offsets = constructSkeleton(unitID,map.Scene, {0,0,0});
    
    for a,anim in pairs(Animations) do
        for i,keyframe in pairs(anim) do
            local commands = keyframe.commands;
            for k,command in pairs(commands) do
                -- commands are described in (c)ommand,(p)iece,(a)xis,(t)arget,(s)peed format
                -- the t attribute needs to be adjusted for move commands from blender's absolute values
                if (command.c == "move") then
                    local adjusted =  command.t - (offsets[command.p][command.a]);
                    Animations[a][i]['commands'][k].t = command.t - (offsets[command.p][command.a]);
                end
            end
        end
    end
end
            
-- local animCmd = {['idle']=Turn, ['turn']=Turn,['move']=Move};
local animCmd = {['turn']=Turn,['move']=Move};
function PlayAnimation(animname)
    local anim = Animations[animname];
    for i = 1, #anim do
        local commands = anim[i].commands;
        for j = 1,#commands do
            local cmd = commands[j];
            animCmd[cmd.c](cmd.p,cmd.a,cmd.t,cmd.s);
        end
        if(i < #anim) then
            local t = anim[i+1]['time'] - anim[i]['time'];
            Sleep(t*33); -- sleep works on milliseconds
        end
    end
end
1 x

User avatar
Anarchid
Posts: 1377
Joined: 30 Nov 2008, 04:31

Re: Exporting Animations from Blender & Play them

Post by Anarchid » 22 Feb 2019, 15:25

Hi,

At least the deer model you've linked uses Armature based animations. This is not supported by blender2lus, because it isn't really supported by Spring due to a bunch of reasons:

- There's no skinning shader for Spring, so there's no need to use skeletons
- The axis mapping in Blender gets a bit confused when imported into Spring.

You'll need to cut your model into individual Objects, which will have to have their own keyframes. Can you post the actual for-Spring model that you try to animate?
2 x

Post Reply

Return to “Lua Scripts”

cron