1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-14 01:19:59 +00:00

Merge branch 'luagetbonetransform' into 'master'

Draft: A simple lua api for fetching armature bone position and rotation by bone's name

See merge request OpenMW/openmw!4581
This commit is contained in:
MaxYari 2025-03-12 09:48:32 +00:00
commit 075c61041a

View File

@ -2,8 +2,11 @@
#include <array>
#include <osg/Matrix>
#include <components/detournavigator/agentbounds.hpp>
#include <components/lua/luastate.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include <components/settings/values.hpp>
#include "apps/openmw/mwbase/environment.hpp"
@ -170,6 +173,31 @@ namespace MWLua
MWBase::Environment::get().getWindowManager()->setSelectedEnchantItem(*it);
}
static std::optional<osg::Matrix> findBoneWorldTransform(
const osg::ref_ptr<osg::Node> parentNode, const std::string_view boneName)
{
if (!parentNode)
return std::nullopt;
osg::Group* parentNodeGroup = parentNode->asGroup();
if (!parentNodeGroup)
return std::nullopt;
for (unsigned int i = 0; i < parentNodeGroup->getNumChildren(); ++i)
{
osg::ref_ptr<osg::Node> child = parentNodeGroup->getChild(i);
// asMatrixTransform will break if its not a bone
if (child->getName() == boneName)
{
return osg::computeLocalToWorld(child->getParentalNodePaths()[0]);
}
return findBoneWorldTransform(child, boneName);
}
return std::nullopt;
}
void addActorBindings(sol::table actor, const Context& context)
{
sol::state_view lua = context.sol();
@ -195,7 +223,33 @@ namespace MWLua
{ "CarriedRight", MWWorld::InventoryStore::Slot_CarriedRight },
{ "CarriedLeft", MWWorld::InventoryStore::Slot_CarriedLeft },
{ "Ammunition", MWWorld::InventoryStore::Slot_Ammunition } }));
actor["getBonePosition"] = [](const Object& o, std::string_view boneName) -> sol::optional<osg::Vec3f> {
const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isActor())
throw std::runtime_error("Actor expected");
std::optional<osg::Matrix> boneTransform
= findBoneWorldTransform(o.ptr().getRefData().getBaseNode(), boneName);
if (!boneTransform.has_value())
return sol::nullopt;
return static_cast<osg::Vec3f>(boneTransform.value().getTrans());
};
actor["getBoneRotation"] = [](const Object& o, std::string_view boneName) -> sol::optional<osg::Quat> {
const MWWorld::Class& cls = o.ptr().getClass();
if (!cls.isActor())
throw std::runtime_error("Actor expected");
std::optional<osg::Matrix> boneTransform
= findBoneWorldTransform(o.ptr().getRefData().getBaseNode(), boneName);
if (!boneTransform.has_value())
return sol::nullopt;
return boneTransform.value().getRotate();
// return Misc::Convert::makeOsgQuat(pos.rot);
};
actor["getStance"] = [](const Object& o) {
const MWWorld::Class& cls = o.ptr().getClass();
if (cls.isActor())