Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/bone_attachment_3d.cpp
20901 views
1
/**************************************************************************/
2
/* bone_attachment_3d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "bone_attachment_3d.h"
32
#include "bone_attachment_3d.compat.inc"
33
34
#include "core/config/engine.h"
35
36
void BoneAttachment3D::_validate_property(PropertyInfo &p_property) const {
37
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "bone_name") {
38
// Because it is a constant function, we cannot use the get_skeleton function.
39
const Skeleton3D *parent = nullptr;
40
if (use_external_skeleton) {
41
if (external_skeleton_node_cache.is_valid()) {
42
parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
43
}
44
} else {
45
parent = Object::cast_to<Skeleton3D>(get_parent());
46
}
47
48
if (parent) {
49
p_property.hint = PROPERTY_HINT_ENUM;
50
p_property.hint_string = parent->get_concatenated_bone_names();
51
} else {
52
p_property.hint = PROPERTY_HINT_NONE;
53
p_property.hint_string = "";
54
}
55
}
56
57
if (p_property.name == "external_skeleton" && !use_external_skeleton) {
58
p_property.usage = PROPERTY_USAGE_NONE;
59
}
60
}
61
62
PackedStringArray BoneAttachment3D::get_configuration_warnings() const {
63
PackedStringArray warnings = Node3D::get_configuration_warnings();
64
65
if (use_external_skeleton) {
66
if (external_skeleton_node_cache.is_null()) {
67
warnings.push_back(RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."));
68
}
69
} else {
70
Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
71
if (!parent) {
72
warnings.push_back(RTR("Parent node is not a Skeleton3D node! Please use an external Skeleton3D if you intend to use the BoneAttachment3D without it being a child of a Skeleton3D node."));
73
}
74
}
75
76
if (bone_idx == -1) {
77
warnings.push_back(RTR("BoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."));
78
}
79
80
return warnings;
81
}
82
83
void BoneAttachment3D::_update_external_skeleton_cache() {
84
external_skeleton_node_cache = ObjectID();
85
if (has_node(external_skeleton_node)) {
86
Node *node = get_node(external_skeleton_node);
87
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Node cannot be found!");
88
89
// Make sure it's a Skeleton3D.
90
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
91
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!");
92
93
external_skeleton_node_cache = node->get_instance_id();
94
} else {
95
if (external_skeleton_node.is_empty()) {
96
BoneAttachment3D *parent_attachment = Object::cast_to<BoneAttachment3D>(get_parent());
97
if (parent_attachment) {
98
parent_attachment->_update_external_skeleton_cache();
99
if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) {
100
Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node);
101
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!");
102
103
// Make sure it's a Skeleton3D.
104
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
105
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!");
106
107
external_skeleton_node_cache = node->get_instance_id();
108
external_skeleton_node = get_path_to(node);
109
}
110
}
111
}
112
}
113
}
114
115
void BoneAttachment3D::_check_bind() {
116
Skeleton3D *sk = get_skeleton();
117
118
if (sk && !bound) {
119
if (bone_idx <= -1) {
120
bone_idx = sk->find_bone(bone_name);
121
}
122
if (bone_idx != -1) {
123
sk->connect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
124
bound = true;
125
on_skeleton_update();
126
}
127
}
128
}
129
130
Skeleton3D *BoneAttachment3D::get_skeleton() {
131
if (use_external_skeleton) {
132
if (external_skeleton_node_cache.is_valid()) {
133
return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
134
} else {
135
_update_external_skeleton_cache();
136
if (external_skeleton_node_cache.is_valid()) {
137
return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
138
}
139
}
140
} else {
141
return Object::cast_to<Skeleton3D>(get_parent());
142
}
143
return nullptr;
144
}
145
146
void BoneAttachment3D::_check_unbind() {
147
if (bound) {
148
Skeleton3D *sk = get_skeleton();
149
150
if (sk) {
151
sk->disconnect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
152
}
153
bound = false;
154
}
155
}
156
157
void BoneAttachment3D::_transform_changed() {
158
if (!is_inside_tree()) {
159
return;
160
}
161
162
if (override_pose && !overriding) {
163
Skeleton3D *sk = get_skeleton();
164
165
ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!");
166
ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
167
168
Transform3D our_trans = get_transform();
169
if (use_external_skeleton) {
170
our_trans = sk->get_global_transform().affine_inverse() * get_global_transform();
171
}
172
173
overriding = true;
174
sk->set_bone_global_pose(bone_idx, our_trans);
175
sk->force_update_all_dirty_bones();
176
}
177
overriding = false;
178
}
179
180
void BoneAttachment3D::set_bone_name(const String &p_name) {
181
bone_name = p_name;
182
Skeleton3D *sk = get_skeleton();
183
if (sk) {
184
set_bone_idx(sk->find_bone(bone_name));
185
}
186
}
187
188
String BoneAttachment3D::get_bone_name() const {
189
return bone_name;
190
}
191
192
void BoneAttachment3D::set_bone_idx(const int &p_idx) {
193
if (is_inside_tree()) {
194
_check_unbind();
195
}
196
197
bone_idx = p_idx;
198
199
Skeleton3D *sk = get_skeleton();
200
if (sk) {
201
if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
202
WARN_PRINT("Bone index " + itos(bone_idx) + " out of range! Cannot connect BoneAttachment to node!");
203
bone_idx = -1;
204
} else {
205
bone_name = sk->get_bone_name(bone_idx);
206
}
207
}
208
209
if (is_inside_tree()) {
210
_check_bind();
211
} else {
212
on_skeleton_update();
213
}
214
215
notify_property_list_changed();
216
}
217
218
int BoneAttachment3D::get_bone_idx() const {
219
return bone_idx;
220
}
221
222
void BoneAttachment3D::set_override_pose(bool p_override_pose) {
223
if (override_pose == p_override_pose) {
224
return;
225
}
226
227
override_pose = p_override_pose;
228
set_notify_transform(override_pose);
229
set_process_internal(override_pose);
230
if (!override_pose && bone_idx >= 0) {
231
Skeleton3D *sk = get_skeleton();
232
if (sk) {
233
sk->reset_bone_pose(bone_idx);
234
}
235
}
236
237
notify_property_list_changed();
238
}
239
240
bool BoneAttachment3D::get_override_pose() const {
241
return override_pose;
242
}
243
244
void BoneAttachment3D::set_use_external_skeleton(bool p_use_external_skeleton) {
245
use_external_skeleton = p_use_external_skeleton;
246
247
if (use_external_skeleton) {
248
_check_unbind();
249
_update_external_skeleton_cache();
250
_check_bind();
251
_transform_changed();
252
}
253
254
notify_property_list_changed();
255
}
256
257
bool BoneAttachment3D::get_use_external_skeleton() const {
258
return use_external_skeleton;
259
}
260
261
void BoneAttachment3D::set_external_skeleton(NodePath p_external_skeleton) {
262
external_skeleton_node = p_external_skeleton;
263
_update_external_skeleton_cache();
264
notify_property_list_changed();
265
}
266
267
NodePath BoneAttachment3D::get_external_skeleton() const {
268
return external_skeleton_node;
269
}
270
271
void BoneAttachment3D::_notification(int p_what) {
272
switch (p_what) {
273
case NOTIFICATION_ENTER_TREE: {
274
if (use_external_skeleton) {
275
_update_external_skeleton_cache();
276
}
277
_check_bind();
278
} break;
279
280
case NOTIFICATION_EXIT_TREE: {
281
_check_unbind();
282
} break;
283
284
case NOTIFICATION_TRANSFORM_CHANGED: {
285
_transform_changed();
286
} break;
287
288
case NOTIFICATION_INTERNAL_PROCESS: {
289
if (_override_dirty) {
290
_override_dirty = false;
291
}
292
} break;
293
}
294
}
295
296
void BoneAttachment3D::on_skeleton_update() {
297
if (updating) {
298
return;
299
}
300
updating = true;
301
if (bone_idx >= 0) {
302
Skeleton3D *sk = get_skeleton();
303
if (sk) {
304
if (!override_pose) {
305
if (use_external_skeleton) {
306
if (sk->is_inside_tree()) {
307
set_global_transform(sk->get_global_transform() * sk->get_bone_global_pose(bone_idx));
308
// Else, do nothing, the transform will be set when the skeleton enters the tree:
309
// Skeleton3D::_notification(NOTIFICATION_ENTER_TREE) -> calls Skeleton3D::_notification(NOTIFICATION_UPDATE_SKELETON)
310
// -> emits skeleton_updated signal -> connected to BoneAttachment3D::on_skeleton_update()
311
}
312
} else {
313
set_transform(sk->get_bone_global_pose(bone_idx));
314
}
315
} else {
316
if (!_override_dirty) {
317
_transform_changed();
318
_override_dirty = true;
319
}
320
}
321
}
322
}
323
updating = false;
324
}
325
326
#ifdef TOOLS_ENABLED
327
void BoneAttachment3D::notify_skeleton_bones_renamed(Node *p_base_scene, Skeleton3D *p_skeleton, Dictionary p_rename_map) {
328
const Skeleton3D *parent = nullptr;
329
if (use_external_skeleton) {
330
if (external_skeleton_node_cache.is_valid()) {
331
parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
332
}
333
} else {
334
parent = Object::cast_to<Skeleton3D>(get_parent());
335
}
336
if (parent && parent == p_skeleton) {
337
StringName bn = p_rename_map[bone_name];
338
if (bn) {
339
set_bone_name(bn);
340
}
341
}
342
}
343
344
void BoneAttachment3D::notify_rebind_required() {
345
// Ensures bindings are properly updated after a scene reload.
346
_check_unbind();
347
if (use_external_skeleton) {
348
_update_external_skeleton_cache();
349
}
350
bone_idx = -1;
351
_check_bind();
352
}
353
#endif // TOOLS_ENABLED
354
355
BoneAttachment3D::BoneAttachment3D() {
356
set_physics_interpolation_mode(PHYSICS_INTERPOLATION_MODE_OFF);
357
}
358
359
void BoneAttachment3D::_bind_methods() {
360
ClassDB::bind_method(D_METHOD("get_skeleton"), &BoneAttachment3D::get_skeleton);
361
362
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
363
ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);
364
365
ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &BoneAttachment3D::set_bone_idx);
366
ClassDB::bind_method(D_METHOD("get_bone_idx"), &BoneAttachment3D::get_bone_idx);
367
368
ClassDB::bind_method(D_METHOD("on_skeleton_update"), &BoneAttachment3D::on_skeleton_update);
369
370
ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment3D::set_override_pose);
371
ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment3D::get_override_pose);
372
373
ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment3D::set_use_external_skeleton);
374
ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment3D::get_use_external_skeleton);
375
ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &BoneAttachment3D::set_external_skeleton);
376
ClassDB::bind_method(D_METHOD("get_external_skeleton"), &BoneAttachment3D::get_external_skeleton);
377
378
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
379
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx");
380
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_pose"), "set_override_pose", "get_override_pose");
381
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_external_skeleton"), "set_use_external_skeleton", "get_use_external_skeleton");
382
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_external_skeleton", "get_external_skeleton");
383
}
384
385