Merge pull request #95643 from Flynsarmy/get_bone_attach_skele

Add `BoneAttachment3D::get_skeleton()` method
This commit is contained in:
Rémi Verschelde 2024-08-20 11:04:04 +02:00
commit 5ebfc8a6ba
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 19 additions and 10 deletions

View File

@ -15,6 +15,12 @@
Returns the [NodePath] to the external [Skeleton3D] node, if one has been set. Returns the [NodePath] to the external [Skeleton3D] node, if one has been set.
</description> </description>
</method> </method>
<method name="get_skeleton">
<return type="Skeleton3D" />
<description>
Get parent or external [Skeleton3D] node if found.
</description>
</method>
<method name="get_use_external_skeleton" qualifiers="const"> <method name="get_use_external_skeleton" qualifiers="const">
<return type="bool" /> <return type="bool" />
<description> <description>

View File

@ -33,7 +33,7 @@
void BoneAttachment3D::_validate_property(PropertyInfo &p_property) const { void BoneAttachment3D::_validate_property(PropertyInfo &p_property) const {
if (p_property.name == "bone_name") { if (p_property.name == "bone_name") {
// Because it is a constant function, we cannot use the _get_skeleton_3d function. // Because it is a constant function, we cannot use the get_skeleton function.
const Skeleton3D *parent = nullptr; const Skeleton3D *parent = nullptr;
if (use_external_skeleton) { if (use_external_skeleton) {
if (external_skeleton_node_cache.is_valid()) { if (external_skeleton_node_cache.is_valid()) {
@ -134,7 +134,7 @@ void BoneAttachment3D::_update_external_skeleton_cache() {
} }
void BoneAttachment3D::_check_bind() { void BoneAttachment3D::_check_bind() {
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk && !bound) { if (sk && !bound) {
if (bone_idx <= -1) { if (bone_idx <= -1) {
@ -148,7 +148,7 @@ void BoneAttachment3D::_check_bind() {
} }
} }
Skeleton3D *BoneAttachment3D::_get_skeleton3d() { Skeleton3D *BoneAttachment3D::get_skeleton() {
if (use_external_skeleton) { if (use_external_skeleton) {
if (external_skeleton_node_cache.is_valid()) { if (external_skeleton_node_cache.is_valid()) {
return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache)); return Object::cast_to<Skeleton3D>(ObjectDB::get_instance(external_skeleton_node_cache));
@ -166,7 +166,7 @@ Skeleton3D *BoneAttachment3D::_get_skeleton3d() {
void BoneAttachment3D::_check_unbind() { void BoneAttachment3D::_check_unbind() {
if (bound) { if (bound) {
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk) { if (sk) {
sk->disconnect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update)); sk->disconnect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
@ -181,7 +181,7 @@ void BoneAttachment3D::_transform_changed() {
} }
if (override_pose && !overriding) { if (override_pose && !overriding) {
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!"); ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!");
ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!"); ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
@ -200,7 +200,7 @@ void BoneAttachment3D::_transform_changed() {
void BoneAttachment3D::set_bone_name(const String &p_name) { void BoneAttachment3D::set_bone_name(const String &p_name) {
bone_name = p_name; bone_name = p_name;
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk) { if (sk) {
set_bone_idx(sk->find_bone(bone_name)); set_bone_idx(sk->find_bone(bone_name));
} }
@ -217,7 +217,7 @@ void BoneAttachment3D::set_bone_idx(const int &p_idx) {
bone_idx = p_idx; bone_idx = p_idx;
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk) { if (sk) {
if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) { if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!"); WARN_PRINT("Bone index out of range! Cannot connect BoneAttachment to node!");
@ -247,7 +247,7 @@ void BoneAttachment3D::set_override_pose(bool p_override) {
set_notify_transform(override_pose); set_notify_transform(override_pose);
set_process_internal(override_pose); set_process_internal(override_pose);
if (!override_pose && bone_idx >= 0) { if (!override_pose && bone_idx >= 0) {
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk) { if (sk) {
sk->reset_bone_pose(bone_idx); sk->reset_bone_pose(bone_idx);
} }
@ -318,7 +318,7 @@ void BoneAttachment3D::on_skeleton_update() {
} }
updating = true; updating = true;
if (bone_idx >= 0) { if (bone_idx >= 0) {
Skeleton3D *sk = _get_skeleton3d(); Skeleton3D *sk = get_skeleton();
if (sk) { if (sk) {
if (!override_pose) { if (!override_pose) {
if (use_external_skeleton) { if (use_external_skeleton) {
@ -369,6 +369,8 @@ BoneAttachment3D::BoneAttachment3D() {
} }
void BoneAttachment3D::_bind_methods() { void BoneAttachment3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_skeleton"), &BoneAttachment3D::get_skeleton);
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name); ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name); ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);

View File

@ -57,7 +57,6 @@ class BoneAttachment3D : public Node3D {
bool updating = false; bool updating = false;
void _transform_changed(); void _transform_changed();
void _update_external_skeleton_cache(); void _update_external_skeleton_cache();
Skeleton3D *_get_skeleton3d();
protected: protected:
void _validate_property(PropertyInfo &p_property) const; void _validate_property(PropertyInfo &p_property) const;
@ -79,6 +78,8 @@ public:
virtual PackedStringArray get_configuration_warnings() const override; virtual PackedStringArray get_configuration_warnings() const override;
Skeleton3D *get_skeleton();
void set_bone_name(const String &p_name); void set_bone_name(const String &p_name);
String get_bone_name() const; String get_bone_name() const;