Blender V5.3
node_geo_bone_info.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_matrix.hh"
6
7#include "BKE_action.hh"
8#include "BKE_armature.hh"
9
11
12#include "NOD_rna_define.hh"
14
16#include "UI_resources.hh"
17
18#include "COM_node_operation.hh"
19
20#include "node_geometry_util.hh"
21
22#include <fmt/format.h>
23
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30
31 b.add_output<decl::Matrix>("Pose"_ustr)
32 .description("Evaluated final transform of the bone in armature space");
33 b.add_output<decl::Matrix>("Local Pose"_ustr)
34 .description("Difference between the pose and rest pose relative to the parent bone");
35 b.add_output<decl::Matrix>("Transform Pose"_ustr)
36 .description("Matrix representing the bone's location, rotation, and scale properties");
37 b.add_output<decl::Matrix>("Rest Pose"_ustr)
38 .description("Original transform of the bone in armature space, defined in edit mode");
39 b.add_output<decl::Float>("Rest Length"_ustr).description("Original length of the bone");
40 b.add_output<decl::Bool>("Exists"_ustr).description("Whether the bone exists in the armature");
41
42 {
43 auto &p = b.add_panel("Envelope"_ustr).default_closed(true);
44 p.add_output<decl::Float>("Envelope Distance"_ustr)
45 .description("Original envelope distance to the bone");
46 p.add_output<decl::Float>("Radius Head"_ustr)
47 .description("Original radius of the head of the bone");
48 p.add_output<decl::Float>("Radius Tail"_ustr)
49 .description("Original radius of the tail of the bone");
50 }
51
52 b.add_input<decl::Object>("Armature"_ustr)
53 .optional_label()
54 .description("Armature object to retrieve the bone information from");
55 b.add_input<decl::String>("Bone Name"_ustr)
56 .optional_label()
57 .description("Name of the bone to retrieve");
58}
59
60static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
61{
62 const bNodeTree &node_tree = *id_cast<const bNodeTree *>(ptr->owner_id);
63 if (node_tree.type == NTREE_GEOMETRY) {
64 layout.prop(ptr, "transform_space", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
65 }
66}
67
69{
70 const eNodeSocketDatatype other_type = params.other_socket().type;
71
72 if (params.in_out() == SOCK_OUT) {
73 if (ELEM(other_type, SOCK_MATRIX, SOCK_ROTATION)) {
74 params.add_item(IFACE_("Pose"), [](LinkSearchOpParams &params) {
75 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
76 params.update_and_connect_available_socket(node, "Pose"_ustr);
77 });
78 params.add_item(IFACE_("Local Pose"), [](LinkSearchOpParams &params) {
79 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
80 params.update_and_connect_available_socket(node, "Local Pose"_ustr);
81 });
82 params.add_item(IFACE_("Transform Pose"), [](LinkSearchOpParams &params) {
83 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
84 params.update_and_connect_available_socket(node, "Transform Pose"_ustr);
85 });
86 params.add_item(IFACE_("Rest Pose"), [](LinkSearchOpParams &params) {
87 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
88 params.update_and_connect_available_socket(node, "Rest Pose"_ustr);
89 });
90 }
91 if (params.node_tree().typeinfo->validate_link(other_type, SOCK_FLOAT)) {
92 params.add_item(IFACE_("Rest Length"), [](LinkSearchOpParams &params) {
93 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
94 params.update_and_connect_available_socket(node, "Rest Length"_ustr);
95 });
96 params.add_item(IFACE_("Envelope Distance"), [](LinkSearchOpParams &params) {
97 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
98 params.update_and_connect_available_socket(node, "Envelope Distance"_ustr);
99 });
100 params.add_item(IFACE_("Radius Head"), [](LinkSearchOpParams &params) {
101 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
102 params.update_and_connect_available_socket(node, "Radius Head"_ustr);
103 });
104 params.add_item(IFACE_("Radius Tail"), [](LinkSearchOpParams &params) {
105 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
106 params.update_and_connect_available_socket(node, "Radius Tail"_ustr);
107 });
108 }
109 }
110 else {
111 if (other_type == SOCK_STRING) {
112 params.add_item(IFACE_("Bone Name"), [](LinkSearchOpParams &params) {
113 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
114 params.update_and_connect_available_socket(node, "Bone Name"_ustr);
115 });
116 }
117 if (other_type == SOCK_OBJECT) {
118 params.add_item(IFACE_("Armature"), [](LinkSearchOpParams &params) {
119 bNode &node = params.add_node("GeometryNodeBoneInfo"_ustr);
120 params.update_and_connect_available_socket(node, "Armature"_ustr);
121 });
122 }
123 }
124}
125
126static void node_node_init(bNodeTree * /*tree*/, bNode *node)
127{
129}
130
141
142/* Computes the bone info of the bone with the given pose channel in the given armature object.
143 * If provided, the bone will be transformed into the given transformation space. */
144static BoneInfo compute_bone_info(const Object &object,
145 const bPoseChannel &pchan,
146 const float4x4 &space_transformation = float4x4::identity())
147{
148 const Bone *bone = pchan.bone_get(object);
149 const float4x4 pose = space_transformation * float4x4(pchan.pose_mat);
150 const float4x4 rest_pose = space_transformation * float4x4(bone->arm_mat);
151
152 const float4x4 parent_pose = pchan.parent ? float4x4(pchan.parent->pose_mat) :
154 const float4x4 parent_rest_pose = bone->parent ? float4x4(bone->parent->arm_mat) :
156 const float4x4 local_pose = math::invert(rest_pose) * parent_rest_pose *
157 math::invert(parent_pose) * pose;
158
159 float4x4 transform_pose;
160 BKE_pchan_to_mat4({&pchan, bone}, transform_pose.ptr());
161
162 return {
163 pose,
164 local_pose,
165 transform_pose,
166 rest_pose,
167 bone->length,
168 bone->dist,
169 bone->rad_head,
170 bone->rad_tail,
171 };
172}
173
175{
176 Object *object = params.extract_input<Object *>("Armature"_ustr);
177 if (!object) {
179 return;
180 }
181 if (object->type != OB_ARMATURE) {
183 params.error_message_add(NodeWarningType::Error, TIP_("Object is not an armature"));
184 return;
185 }
186 const std::string bone_name = params.extract_input<std::string>("Bone Name"_ustr);
187 if (bone_name.empty()) {
189 return;
190 }
191 if (!object->pose) {
193 params.error_message_add(NodeWarningType::Error, TIP_("Object has no pose"));
194 return;
195 }
196
197 const bool transform_space_relative = (params.node().custom1 ==
199 float4x4 geometry_transform = float4x4::identity();
200 if (transform_space_relative) {
201 const Object *self_object = params.self_object();
202 const bool self_transform_evaluated = DEG_object_transform_is_evaluated(*self_object);
203 const bool object_transform_evaluated = DEG_object_transform_is_evaluated(*object);
204 if (!self_transform_evaluated || !object_transform_evaluated) {
205 params.error_message_add(
207 TIP_("Cannot access object's transforms because it's not evaluated yet. "
208 "This can happen when there is a dependency cycle"));
210 return;
211 }
212 geometry_transform = self_object->world_to_object() * object->object_to_world();
213 }
214
215 const bPoseChannel *pchan = BKE_pose_channel_find_name(object->pose, bone_name.c_str());
216 if (!pchan) {
218 if (!params.output_is_required("Exists"_ustr)) {
219 params.error_message_add(
221 fmt::format(fmt::runtime(TIP_("Bone \"{}\" not found")), bone_name));
222 }
223 return;
224 }
225
226 const BoneInfo values = compute_bone_info(*object, *pchan, geometry_transform);
227 params.set_output("Pose"_ustr, values.pose);
228 params.set_output("Local Pose"_ustr, values.local_pose);
229 params.set_output("Transform Pose"_ustr, values.transform_pose);
230 params.set_output("Rest Pose"_ustr, values.rest_pose);
231 params.set_output("Rest Length"_ustr, values.rest_length);
232 params.set_output("Exists"_ustr, true);
233 params.set_output("Envelope Distance"_ustr, values.envelope);
234 params.set_output("Radius Head"_ustr, values.radius_head);
235 params.set_output("Radius Tail"_ustr, values.radius_tail);
236}
237
239 public:
241
242 void execute() override
243 {
244 const Object *object = this->get_input("Armature").get_single_value<Object *>();
245 if (!object) {
247 return;
248 }
249
250 if (object->type != OB_ARMATURE) {
252 this->add_warning(NodeWarningType::Error, TIP_("Object is not an armature"));
253 return;
254 }
255
256 const std::string bone_name = this->get_input("Bone Name").get_single_value<std::string>();
257 if (bone_name.empty()) {
259 return;
260 }
261
262 if (!object->pose) {
264 this->add_warning(NodeWarningType::Error, TIP_("Object has no pose"));
265 return;
266 }
267
268 const bPoseChannel *pchan = BKE_pose_channel_find_name(object->pose, bone_name.c_str());
269 if (!pchan) {
271 if (!this->get_result("Exists").should_compute()) {
273 fmt::format(fmt::runtime(TIP_("Bone \"{}\" not found")), bone_name));
274 }
275 return;
276 }
277
278 const BoneInfo values = compute_bone_info(*object, *pchan);
279
280 compositor::Result &pose_result = this->get_result("Pose");
281 if (pose_result.should_compute()) {
282 pose_result.allocate_single_value();
283 pose_result.set_single_value(values.pose);
284 }
285
286 compositor::Result &local_pose_result = this->get_result("Local Pose");
287 if (local_pose_result.should_compute()) {
288 local_pose_result.allocate_single_value();
289 local_pose_result.set_single_value(values.local_pose);
290 }
291
292 compositor::Result &transform_pose_result = this->get_result("Transform Pose");
293 if (transform_pose_result.should_compute()) {
294 transform_pose_result.allocate_single_value();
295 transform_pose_result.set_single_value(values.transform_pose);
296 }
297
298 compositor::Result &rest_pose_result = this->get_result("Rest Pose");
299 if (rest_pose_result.should_compute()) {
300 rest_pose_result.allocate_single_value();
301 rest_pose_result.set_single_value(values.rest_pose);
302 }
303
304 compositor::Result &rest_length_result = this->get_result("Rest Length");
305 if (rest_length_result.should_compute()) {
306 rest_length_result.allocate_single_value();
307 rest_length_result.set_single_value(values.rest_length);
308 }
309
310 compositor::Result &exists_result = this->get_result("Exists");
311 if (exists_result.should_compute()) {
312 exists_result.allocate_single_value();
313 exists_result.set_single_value(true);
314 }
315
316 compositor::Result &envelope_result = this->get_result("Envelope Distance");
317 if (envelope_result.should_compute()) {
318 envelope_result.allocate_single_value();
319 envelope_result.set_single_value(values.envelope);
320 }
321
322 compositor::Result &radius_head_result = this->get_result("Radius Head");
323 if (radius_head_result.should_compute()) {
324 radius_head_result.allocate_single_value();
325 radius_head_result.set_single_value(values.radius_head);
326 }
327
328 compositor::Result &radius_tail_result = this->get_result("Radius Tail");
329 if (radius_tail_result.should_compute()) {
330 radius_tail_result.allocate_single_value();
331 radius_tail_result.set_single_value(values.radius_tail);
332 }
333 }
334};
335
337 const bNode &node)
338{
339 return new BoneInfoOperation(context, node);
340}
341
342static void node_rna(StructRNA *srna)
343{
344 static const EnumPropertyItem rna_node_geometry_object_info_transform_space_items[] = {
346 "ORIGINAL",
347 0,
348 "Original",
349 "Output the bone pose relative to the armature object transform"},
351 "RELATIVE",
352 0,
353 "Relative",
354 "Bring the bone pose into the modified object"},
355 {0, nullptr, 0, nullptr, nullptr},
356 };
357
358 PropertyRNA *prop = RNA_def_node_enum(srna,
359 "transform_space",
360 "Transform Space",
361 "The transformation of the vector and geometry outputs",
362 rna_node_geometry_object_info_transform_space_items,
366}
367
368static void node_register()
369{
370 static bke::bNodeType ntype;
371 geo_cmp_node_type_base(&ntype, "GeometryNodeBoneInfo"_ustr);
372 ntype.ui_name = "Bone Info";
373 ntype.ui_description = "Retrieve information of armature bones";
374 ntype.nclass = NODE_CLASS_INPUT;
375 ntype.declare = node_declare;
376 ntype.initfunc = node_node_init;
382
383 node_rna(ntype.rna_ext.srna);
384}
386
387} // namespace blender::nodes::node_geo_bone_info_cc
Blender kernel action and pose functionality.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define ELEM(...)
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Result & get_input(StringRef identifier) const
Definition operation.cc:35
bool should_compute() const
Definition result.cc:1124
void set_single_value(const T &value)
const T & get_single_value() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
CartesianBasis invert(const CartesianBasis &basis)
static void node_geo_exec(GeoNodeExecParams params)
static void node_rna(StructRNA *srna)
static BoneInfo compute_bone_info(const Object &object, const bPoseChannel &pchan, const float4x4 &space_transformation=float4x4::identity())
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static void node_node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static compositor::NodeOperation * get_compositor_operation(compositor::Context &context, const bNode &node)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void BKE_pchan_to_mat4(bke::PChanBoneConst pchanbone, float r_chanmat[4][4])
MatBase< float, 4, 4 > float4x4
bool DEG_object_transform_is_evaluated(const Object &object)
void rna_Node_update_relations(Main *bmain, Scene *scne, PointerRNA *ptr)
@ GEO_NODE_TRANSFORM_SPACE_ORIGINAL
@ GEO_NODE_TRANSFORM_SPACE_RELATIVE
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
bPoseChannel * BKE_pose_channel_find_name(const bPose *pose, const char *name)
void geo_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
struct Bone * parent
const c_style_mat & ptr() const
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
eNodeTree_Type type
struct bPoseChannel * parent
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)