Skip to content

Commit

Permalink
VOXELGENERATOR: extend the animation lua script
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhardy committed Sep 21, 2024
1 parent 4edd44a commit 9b9bbcc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
12 changes: 10 additions & 2 deletions docs/LUAScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ The functions are:

* `getByUUID(uuid)`: Returns `nil` if no node with the given uuid exists

* `activeAnimation()`: Return the current active animation

* `setAnimation(string)`: Activate the animation

* `addAnimation(string)`: Add a new animation

* `duplicateAnimation(animation, newAnimationName)`: Add a new animation by duplicating the given animation

* `hasAnimation(string)`: Check if the animation exists

* `nodeIds()`: Returns a table with all node ids of the current scene graph.

```lua
Expand Down Expand Up @@ -183,8 +193,6 @@ end

* `removeKeyFrame(keyFrameIdx)`: Remove the existing key frame at the given index. Throws an error if the index is invalid or the key frame doesn't exist.

* `setAnimation(string)`: Activate the animation

* `setPalette(palette, [remap])`: Change the palette or if remap is given and is true it remaps to the new palette

* `setPivot(vec3)`, `setPivot(x, y, z)`:
Expand Down
56 changes: 40 additions & 16 deletions src/modules/voxelgenerator/LUAApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,41 @@ static int luaVoxel_scenegraph_get_node_by_id(lua_State* s) {
return luaVoxel_pushscenegraphnode(s, node);
}

static int luaVoxel_scenegraph_addanimation(lua_State* s) {
scenegraph::SceneGraph* sceneGraph = lua::LUA::globalData<scenegraph::SceneGraph>(s, luaVoxel_globalscenegraph());
const char *name = luaL_checkstring(s, 1);
lua_pushboolean(s, sceneGraph->addAnimation(name));
return 1;
}

static int luaVoxel_scenegraph_hasanimation(lua_State* s) {
scenegraph::SceneGraph* sceneGraph = lua::LUA::globalData<scenegraph::SceneGraph>(s, luaVoxel_globalscenegraph());
const char *name = luaL_checkstring(s, 1);
lua_pushboolean(s, sceneGraph->hasAnimation(name));
return 1;
}

static int luaVoxel_scenegraph_setanimation(lua_State* s) {
scenegraph::SceneGraph* sceneGraph = lua::LUA::globalData<scenegraph::SceneGraph>(s, luaVoxel_globalscenegraph());
const char *name = luaL_checkstring(s, 1);
lua_pushboolean(s, sceneGraph->setAnimation(name));
return 1;
}

static int luaVoxel_scenegraph_activeanimation(lua_State* s) {
scenegraph::SceneGraph* sceneGraph = lua::LUA::globalData<scenegraph::SceneGraph>(s, luaVoxel_globalscenegraph());
lua_pushstring(s, sceneGraph->activeAnimation().c_str());
return 1;
}

static int luaVoxel_scenegraph_duplicateanimation(lua_State* s) {
scenegraph::SceneGraph* sceneGraph = lua::LUA::globalData<scenegraph::SceneGraph>(s, luaVoxel_globalscenegraph());
const char *animation = luaL_checkstring(s, 1);
const char *newName = luaL_checkstring(s, 2);
lua_pushboolean(s, sceneGraph->duplicateAnimation(animation, newName));
return 1;
}

static int luaVoxel_scenegraphnode_volume(lua_State* s) {
LuaSceneGraphNode* node = luaVoxel_toscenegraphnode(s, 1);
if (!node->node->isModelNode()) {
Expand Down Expand Up @@ -1310,20 +1345,6 @@ static int luaVoxel_scenegraphnode_addframe(lua_State* s) {
return 1;
}

static int luaVoxel_scenegraphnode_addanimation(lua_State* s) {
LuaSceneGraphNode* node = luaVoxel_toscenegraphnode(s, 1);
const char *name = luaL_checkstring(s, 2);
lua_pushboolean(s, node->node->addAnimation(name));
return 1;
}

static int luaVoxel_scenegraphnode_setanimation(lua_State* s) {
LuaSceneGraphNode* node = luaVoxel_toscenegraphnode(s, 1);
const char *name = luaL_checkstring(s, 2);
lua_pushboolean(s, node->node->setAnimation(name));
return 1;
}

static int luaVoxel_keyframe_index(lua_State *s) {
LuaKeyFrame *keyFrame = luaVoxel_tokeyframe(s, 1);
lua_pushinteger(s, keyFrame->keyFrameIdx);
Expand Down Expand Up @@ -1603,6 +1624,11 @@ static void prepareState(lua_State* s) {
{"getByUUID", luaVoxel_scenegraph_get_node_by_uuid},
{"nodeIds", luaVoxel_scenegraph_get_all_node_ids},
{"updateTransforms", luaVoxel_scenegraph_updatetransforms},
{"addAnimation", luaVoxel_scenegraph_addanimation},
{"setAnimation", luaVoxel_scenegraph_setanimation},
{"duplicateAnimation", luaVoxel_scenegraph_duplicateanimation},
{"hasAnimation", luaVoxel_scenegraph_hasanimation},
{"activeAnimation", luaVoxel_scenegraph_activeanimation},
{nullptr, nullptr}
};
clua_registerfuncsglobal(s, sceneGraphFuncs, luaVoxel_metascenegraph(), "g_scenegraph");
Expand All @@ -1628,8 +1654,6 @@ static void prepareState(lua_State* s) {
{"hasKeyFrameForFrame", luaVoxel_scenegraphnode_hasframe},
{"removeKeyFrameForFrame", luaVoxel_scenegraphnode_removekeyframeforframe},
{"removeKeyFrame", luaVoxel_scenegraphnode_removekeyframe},
{"addAnimation", luaVoxel_scenegraphnode_addanimation},
{"setAnimation", luaVoxel_scenegraphnode_setanimation},
{"__tostring", luaVoxel_scenegraphnode_tostring},
{"__gc", luaVoxel_scenegraphnode_gc},
{nullptr, nullptr}
Expand Down
8 changes: 7 additions & 1 deletion src/modules/voxelgenerator/lua/scripts/animate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function arguments()
-- NEW_ANIM: add new animation ids to the enum values
-- if values are animation specific, please mark them as such by mentioning the animation name like this "(walk)"
{ name = 'animation', desc = 'The animation to create', type = 'enum', enum = 'walk,jump,all', default = 'walk'},
{ name = 'createAnim', desc = 'Create the animation by duplicating the current animation or add to current animation', type = 'bool', default = 'true' },
{ name = 'maxKeyFrames', desc = 'The maximum number of keyframes to create', type = 'int', default = 6, min = 1, max = 100},
{ name = 'frameDuration', desc = 'How many frames does each key frame last', type = 'int', default = 20, min = 1, max = 1000},
{ name = 'timeFactor', desc = 'How fast the animation should be', type = 'float', default = 12.0, min = 0.0, max = 100.0},
Expand Down Expand Up @@ -183,19 +184,24 @@ local function createAnimation(node, context)
if animFunc == nil then
error("No animation callback registered for: " .. context.animation)
end
if context.createAnim then
g_scenegraph.duplicateAnimation(g_scenegraph.activeAnimation(), context.animation)
g_scenegraph.setAnimation(context.animation)
end
for keyframe = 0, context.maxKeyFrames - 1 do
if not animFunc(node, keyframe, context) then
break
end
end
end

function main(_, _, _, animation, maxKeyFrames, frameDuration, timeFactor, handAngleFactor, footAngleFactor)
function main(_, _, _, animation, createAnim, maxKeyFrames, frameDuration, timeFactor, handAngleFactor, footAngleFactor)
if not isValidAnimation(animation) then
error("Unknown animation: " .. animation)
end
-- NEW_ANIM: Add new context variables here if your animation needs them
context = {
createAnim = createAnim,
animation = animation,
maxKeyFrames = maxKeyFrames,
frameDuration = frameDuration,
Expand Down

0 comments on commit 9b9bbcc

Please sign in to comment.