diff --git a/docs/LUAScript.md b/docs/LUAScript.md index c5d26aac6..6d3f73503 100644 --- a/docs/LUAScript.md +++ b/docs/LUAScript.md @@ -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 @@ -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)`: diff --git a/src/modules/voxelgenerator/LUAApi.cpp b/src/modules/voxelgenerator/LUAApi.cpp index a01a0c94d..3ee078285 100644 --- a/src/modules/voxelgenerator/LUAApi.cpp +++ b/src/modules/voxelgenerator/LUAApi.cpp @@ -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(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(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(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(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(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()) { @@ -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); @@ -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"); @@ -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} diff --git a/src/modules/voxelgenerator/lua/scripts/animate.lua b/src/modules/voxelgenerator/lua/scripts/animate.lua index fd74ab0b9..06759c365 100644 --- a/src/modules/voxelgenerator/lua/scripts/animate.lua +++ b/src/modules/voxelgenerator/lua/scripts/animate.lua @@ -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}, @@ -183,6 +184,10 @@ 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 @@ -190,12 +195,13 @@ local function createAnimation(node, context) 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,