Merge branch 'luagetbonetransform' into 'master'

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-09-21 02:16:57 +00:00
commit 9680dd4c85

View File

@ -2,8 +2,11 @@
#include <array> #include <array>
#include <osg/Matrix>
#include <components/detournavigator/agentbounds.hpp> #include <components/detournavigator/agentbounds.hpp>
#include <components/lua/luastate.hpp> #include <components/lua/luastate.hpp>
#include <components/sceneutil/positionattitudetransform.hpp>
#include <components/settings/values.hpp> #include <components/settings/values.hpp>
#include "apps/openmw/mwbase/environment.hpp" #include "apps/openmw/mwbase/environment.hpp"
@ -170,6 +173,31 @@ namespace MWLua
MWBase::Environment::get().getWindowManager()->setSelectedEnchantItem(*it); 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) void addActorBindings(sol::table actor, const Context& context)
{ {
sol::state_view lua = context.sol(); sol::state_view lua = context.sol();
@ -195,7 +223,33 @@ namespace MWLua
{ "CarriedRight", MWWorld::InventoryStore::Slot_CarriedRight }, { "CarriedRight", MWWorld::InventoryStore::Slot_CarriedRight },
{ "CarriedLeft", MWWorld::InventoryStore::Slot_CarriedLeft }, { "CarriedLeft", MWWorld::InventoryStore::Slot_CarriedLeft },
{ "Ammunition", MWWorld::InventoryStore::Slot_Ammunition } })); { "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) { actor["getStance"] = [](const Object& o) {
const MWWorld::Class& cls = o.ptr().getClass(); const MWWorld::Class& cls = o.ptr().getClass();
if (cls.isActor()) if (cls.isActor())