Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/3d/bone_attachment_3d.cpp
9905 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
void BoneAttachment3D::_validate_property(PropertyInfo &p_property) const {
35
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "bone_name") {
36
// Because it is a constant function, we cannot use the get_skeleton function.
37
const Skeleton3D *parent = nullptr;
38
if (use_external_skeleton) {
39
if (external_skeleton_node_cache.is_valid()) {
40
parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
41
}
42
} else {
43
parent = Object::cast_to<Skeleton3D>(get_parent());
44
}
45
46
if (parent) {
47
p_property.hint = PROPERTY_HINT_ENUM;
48
p_property.hint_string = parent->get_concatenated_bone_names();
49
} else {
50
p_property.hint = PROPERTY_HINT_NONE;
51
p_property.hint_string = "";
52
}
53
}
54
55
if (p_property.name == "external_skeleton" && !use_external_skeleton) {
56
p_property.usage = PROPERTY_USAGE_NONE;
57
}
58
}
59
60
PackedStringArray BoneAttachment3D::get_configuration_warnings() const {
61
PackedStringArray warnings = Node3D::get_configuration_warnings();
62
63
if (use_external_skeleton) {
64
if (external_skeleton_node_cache.is_null()) {
65
warnings.push_back(RTR("External Skeleton3D node not set! Please set a path to an external Skeleton3D node."));
66
}
67
} else {
68
Skeleton3D *parent = Object::cast_to<Skeleton3D>(get_parent());
69
if (!parent) {
70
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."));
71
}
72
}
73
74
if (bone_idx == -1) {
75
warnings.push_back(RTR("BoneAttachment3D node is not bound to any bones! Please select a bone to attach this node."));
76
}
77
78
return warnings;
79
}
80
81
void BoneAttachment3D::_update_external_skeleton_cache() {
82
external_skeleton_node_cache = ObjectID();
83
if (has_node(external_skeleton_node)) {
84
Node *node = get_node(external_skeleton_node);
85
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Node cannot be found!");
86
87
// Make sure it's a Skeleton3D.
88
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
89
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Skeleton3D Nodepath does not point to a Skeleton3D node!");
90
91
external_skeleton_node_cache = node->get_instance_id();
92
} else {
93
if (external_skeleton_node.is_empty()) {
94
BoneAttachment3D *parent_attachment = Object::cast_to<BoneAttachment3D>(get_parent());
95
if (parent_attachment) {
96
parent_attachment->_update_external_skeleton_cache();
97
if (parent_attachment->has_node(parent_attachment->external_skeleton_node)) {
98
Node *node = parent_attachment->get_node(parent_attachment->external_skeleton_node);
99
ERR_FAIL_NULL_MSG(node, "Cannot update external skeleton cache: Parent's Skeleton3D node cannot be found!");
100
101
// Make sure it's a Skeleton3D.
102
Skeleton3D *sk = Object::cast_to<Skeleton3D>(node);
103
ERR_FAIL_NULL_MSG(sk, "Cannot update external skeleton cache: Parent Skeleton3D Nodepath does not point to a Skeleton3D node!");
104
105
external_skeleton_node_cache = node->get_instance_id();
106
external_skeleton_node = get_path_to(node);
107
}
108
}
109
}
110
}
111
}
112
113
void BoneAttachment3D::_check_bind() {
114
Skeleton3D *sk = get_skeleton();
115
116
if (sk && !bound) {
117
if (bone_idx <= -1) {
118
bone_idx = sk->find_bone(bone_name);
119
}
120
if (bone_idx != -1) {
121
sk->connect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
122
bound = true;
123
on_skeleton_update();
124
}
125
}
126
}
127
128
Skeleton3D *BoneAttachment3D::get_skeleton() {
129
if (use_external_skeleton) {
130
if (external_skeleton_node_cache.is_valid()) {
131
return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
132
} else {
133
_update_external_skeleton_cache();
134
if (external_skeleton_node_cache.is_valid()) {
135
return ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
136
}
137
}
138
} else {
139
return Object::cast_to<Skeleton3D>(get_parent());
140
}
141
return nullptr;
142
}
143
144
void BoneAttachment3D::_check_unbind() {
145
if (bound) {
146
Skeleton3D *sk = get_skeleton();
147
148
if (sk) {
149
sk->disconnect(SceneStringName(skeleton_updated), callable_mp(this, &BoneAttachment3D::on_skeleton_update));
150
}
151
bound = false;
152
}
153
}
154
155
void BoneAttachment3D::_transform_changed() {
156
if (!is_inside_tree()) {
157
return;
158
}
159
160
if (override_pose && !overriding) {
161
Skeleton3D *sk = get_skeleton();
162
163
ERR_FAIL_NULL_MSG(sk, "Cannot override pose: Skeleton not found!");
164
ERR_FAIL_INDEX_MSG(bone_idx, sk->get_bone_count(), "Cannot override pose: Bone index is out of range!");
165
166
Transform3D our_trans = get_transform();
167
if (use_external_skeleton) {
168
our_trans = sk->get_global_transform().affine_inverse() * get_global_transform();
169
}
170
171
overriding = true;
172
sk->set_bone_global_pose(bone_idx, our_trans);
173
sk->force_update_all_dirty_bones();
174
}
175
overriding = false;
176
}
177
178
void BoneAttachment3D::set_bone_name(const String &p_name) {
179
bone_name = p_name;
180
Skeleton3D *sk = get_skeleton();
181
if (sk) {
182
set_bone_idx(sk->find_bone(bone_name));
183
}
184
}
185
186
String BoneAttachment3D::get_bone_name() const {
187
return bone_name;
188
}
189
190
void BoneAttachment3D::set_bone_idx(const int &p_idx) {
191
if (is_inside_tree()) {
192
_check_unbind();
193
}
194
195
bone_idx = p_idx;
196
197
Skeleton3D *sk = get_skeleton();
198
if (sk) {
199
if (bone_idx <= -1 || bone_idx >= sk->get_bone_count()) {
200
WARN_PRINT("Bone index " + itos(bone_idx) + " out of range! Cannot connect BoneAttachment to node!");
201
bone_idx = -1;
202
} else {
203
bone_name = sk->get_bone_name(bone_idx);
204
}
205
}
206
207
if (is_inside_tree()) {
208
_check_bind();
209
} else {
210
on_skeleton_update();
211
}
212
213
notify_property_list_changed();
214
}
215
216
int BoneAttachment3D::get_bone_idx() const {
217
return bone_idx;
218
}
219
220
void BoneAttachment3D::set_override_pose(bool p_override_pose) {
221
if (override_pose == p_override_pose) {
222
return;
223
}
224
225
override_pose = p_override_pose;
226
set_notify_transform(override_pose);
227
set_process_internal(override_pose);
228
if (!override_pose && bone_idx >= 0) {
229
Skeleton3D *sk = get_skeleton();
230
if (sk) {
231
sk->reset_bone_pose(bone_idx);
232
}
233
}
234
235
notify_property_list_changed();
236
}
237
238
bool BoneAttachment3D::get_override_pose() const {
239
return override_pose;
240
}
241
242
void BoneAttachment3D::set_use_external_skeleton(bool p_use_external_skeleton) {
243
use_external_skeleton = p_use_external_skeleton;
244
245
if (use_external_skeleton) {
246
_check_unbind();
247
_update_external_skeleton_cache();
248
_check_bind();
249
_transform_changed();
250
}
251
252
notify_property_list_changed();
253
}
254
255
bool BoneAttachment3D::get_use_external_skeleton() const {
256
return use_external_skeleton;
257
}
258
259
void BoneAttachment3D::set_external_skeleton(NodePath p_external_skeleton) {
260
external_skeleton_node = p_external_skeleton;
261
_update_external_skeleton_cache();
262
notify_property_list_changed();
263
}
264
265
NodePath BoneAttachment3D::get_external_skeleton() const {
266
return external_skeleton_node;
267
}
268
269
void BoneAttachment3D::_notification(int p_what) {
270
switch (p_what) {
271
case NOTIFICATION_ENTER_TREE: {
272
if (use_external_skeleton) {
273
_update_external_skeleton_cache();
274
}
275
_check_bind();
276
} break;
277
278
case NOTIFICATION_EXIT_TREE: {
279
_check_unbind();
280
} break;
281
282
case NOTIFICATION_TRANSFORM_CHANGED: {
283
_transform_changed();
284
} break;
285
286
case NOTIFICATION_INTERNAL_PROCESS: {
287
if (_override_dirty) {
288
_override_dirty = false;
289
}
290
} break;
291
}
292
}
293
294
void BoneAttachment3D::on_skeleton_update() {
295
if (updating) {
296
return;
297
}
298
updating = true;
299
if (bone_idx >= 0) {
300
Skeleton3D *sk = get_skeleton();
301
if (sk) {
302
if (!override_pose) {
303
if (use_external_skeleton) {
304
if (sk->is_inside_tree()) {
305
set_global_transform(sk->get_global_transform() * sk->get_bone_global_pose(bone_idx));
306
// Else, do nothing, the transform will be set when the skeleton enters the tree:
307
// Skeleton3D::_notification(NOTIFICATION_ENTER_TREE) -> calls Skeleton3D::_notification(NOTIFICATION_UPDATE_SKELETON)
308
// -> emits skeleton_updated signal -> connected to BoneAttachment3D::on_skeleton_update()
309
}
310
} else {
311
set_transform(sk->get_bone_global_pose(bone_idx));
312
}
313
} else {
314
if (!_override_dirty) {
315
_transform_changed();
316
_override_dirty = true;
317
}
318
}
319
}
320
}
321
updating = false;
322
}
323
324
#ifdef TOOLS_ENABLED
325
void BoneAttachment3D::notify_skeleton_bones_renamed(Node *p_base_scene, Skeleton3D *p_skeleton, Dictionary p_rename_map) {
326
const Skeleton3D *parent = nullptr;
327
if (use_external_skeleton) {
328
if (external_skeleton_node_cache.is_valid()) {
329
parent = ObjectDB::get_instance<Skeleton3D>(external_skeleton_node_cache);
330
}
331
} else {
332
parent = Object::cast_to<Skeleton3D>(get_parent());
333
}
334
if (parent && parent == p_skeleton) {
335
StringName bn = p_rename_map[bone_name];
336
if (bn) {
337
set_bone_name(bn);
338
}
339
}
340
}
341
342
void BoneAttachment3D::notify_rebind_required() {
343
// Ensures bindings are properly updated after a scene reload.
344
_check_unbind();
345
if (use_external_skeleton) {
346
_update_external_skeleton_cache();
347
}
348
bone_idx = -1;
349
_check_bind();
350
}
351
#endif // TOOLS_ENABLED
352
353
BoneAttachment3D::BoneAttachment3D() {
354
set_physics_interpolation_mode(PHYSICS_INTERPOLATION_MODE_OFF);
355
}
356
357
void BoneAttachment3D::_bind_methods() {
358
ClassDB::bind_method(D_METHOD("get_skeleton"), &BoneAttachment3D::get_skeleton);
359
360
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &BoneAttachment3D::set_bone_name);
361
ClassDB::bind_method(D_METHOD("get_bone_name"), &BoneAttachment3D::get_bone_name);
362
363
ClassDB::bind_method(D_METHOD("set_bone_idx", "bone_idx"), &BoneAttachment3D::set_bone_idx);
364
ClassDB::bind_method(D_METHOD("get_bone_idx"), &BoneAttachment3D::get_bone_idx);
365
366
ClassDB::bind_method(D_METHOD("on_skeleton_update"), &BoneAttachment3D::on_skeleton_update);
367
368
ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment3D::set_override_pose);
369
ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment3D::get_override_pose);
370
371
ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment3D::set_use_external_skeleton);
372
ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment3D::get_use_external_skeleton);
373
ClassDB::bind_method(D_METHOD("set_external_skeleton", "external_skeleton"), &BoneAttachment3D::set_external_skeleton);
374
ClassDB::bind_method(D_METHOD("get_external_skeleton"), &BoneAttachment3D::get_external_skeleton);
375
376
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "bone_name"), "set_bone_name", "get_bone_name");
377
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_idx"), "set_bone_idx", "get_bone_idx");
378
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_pose"), "set_override_pose", "get_override_pose");
379
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_external_skeleton"), "set_use_external_skeleton", "get_use_external_skeleton");
380
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "external_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_external_skeleton", "get_external_skeleton");
381
}
382
383