Blender V5.3
node_geo_object_info.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_math_euler.hh"
6#include "BLI_math_matrix.hh"
7
9#include "BKE_instances.hh"
10
11#include "DNA_object_types.h"
12
13#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
20
21#include "GEO_transform.hh"
22
23#include "BLT_translation.hh"
24
25#include "COM_node_operation.hh"
26
27#include "node_geometry_util.hh"
28
30
32
34{
35 const bool is_geometry = b.tree_or_null() ? b.tree_or_null()->type == NTREE_GEOMETRY : true;
36 b.add_input<decl::Object>("Object"_ustr).optional_label();
37 b.add_input<decl::Bool>("As Instance"_ustr)
39 "Output the entire object as single instance. "
40 "This allows instancing non-geometry object types")
41 .available(is_geometry);
42 b.add_output<decl::Matrix>("Transform"_ustr)
44 "Transformation matrix containing the location, rotation and scale of the object");
45 b.add_output<decl::Vector>("Location"_ustr);
46 b.add_output<decl::Rotation>("Rotation"_ustr);
47 b.add_output<decl::Vector>("Scale"_ustr);
48 b.add_output<decl::Geometry>("Geometry"_ustr).available(is_geometry);
49}
50
51static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
52{
53 const bNodeTree &node_tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
54 if (node_tree.type == NTREE_GEOMETRY) {
55 layout.prop(ptr, "transform_space", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
56 }
57}
58
60{
61 const NodeGeometryObjectInfo &storage = node_storage(params.node());
62 const bool transform_space_relative = (storage.transform_space ==
64
65 Object *object = params.extract_input<Object *>("Object"_ustr);
66
67 const Object *self_object = params.self_object();
68 if (object == nullptr) {
70 return;
71 }
72
73 const bool self_transform_evaluated = DEG_object_transform_is_evaluated(*self_object);
74 const bool object_transform_evaluated = DEG_object_transform_is_evaluated(*object);
75 const bool object_geometry_evaluated = DEG_object_geometry_is_evaluated(*object);
76
77 float4x4 output_transform = float4x4::identity();
78 bool show_transform_error = false;
79 if (transform_space_relative) {
80 if (self_transform_evaluated && object_transform_evaluated) {
81 output_transform = self_object->world_to_object() * object->object_to_world();
82 }
83 else {
84 show_transform_error = true;
85 }
86 }
87 else {
88 if (object_transform_evaluated) {
89 output_transform = object->object_to_world();
90 }
91 else {
92 show_transform_error = true;
93 }
94 }
95 if (show_transform_error) {
96 params.error_message_add(
98 TIP_("Cannot access object's transforms because it's not evaluated yet. "
99 "This can happen when there is a dependency cycle"));
100 }
101 float3 location, scale;
102 math::Quaternion rotation;
103 math::to_loc_rot_scale_safe<true>(output_transform, location, rotation, scale);
104
105 params.set_output("Location"_ustr, location);
106 params.set_output("Rotation"_ustr, rotation);
107 params.set_output("Scale"_ustr, scale);
108 params.set_output("Transform"_ustr, output_transform);
109
110 if (!params.output_is_required("Geometry"_ustr)) {
111 return;
112 }
113 /* Compare by `orig_id` because objects may be copied into separate depsgraphs. */
114 if (DEG_get_original(object) == DEG_get_original(self_object)) {
115 params.error_message_add(
118 TIP_("Geometry cannot be retrieved from the edited object itself") :
119 TIP_("Geometry cannot be retrieved from the modifier object"));
121 return;
122 }
123 BLI_assert(object != self_object);
124
125 if (!object_geometry_evaluated) {
126 params.error_message_add(
128 TIP_("Cannot access object's geometry because it's not evaluated yet. "
129 "This can happen when there is a dependency cycle"));
131 return;
132 }
133
134 std::optional<float4x4> geometry_transform;
135 if (transform_space_relative) {
136 if (!self_transform_evaluated || !object_transform_evaluated) {
137 params.error_message_add(
139 TIP_("Cannot access object's transforms because it's not evaluated yet. "
140 "This can happen when there is a dependency cycle"));
142 return;
143 }
144 geometry_transform = self_object->world_to_object() * object->object_to_world();
145 }
146
147 GeometrySet geometry_set;
148 if (params.extract_input<bool>("As Instance"_ustr)) {
149 auto instances = std::make_unique<bke::Instances>(1);
150 instances->reference_handles_for_write().first() = instances->add_reference(*object);
151 if (transform_space_relative) {
152 instances->transforms_for_write().first() = *geometry_transform;
153 }
154 else {
155 instances->transforms_for_write().first() = float4x4::identity();
156 }
157 geometry_set = GeometrySet::from_instances(std::move(instances));
158 }
159 else {
160 geometry_set = bke::object_get_evaluated_geometry_set(*object);
161 if (transform_space_relative) {
162 geometry::transform_geometry(geometry_set, *geometry_transform);
163 }
164 }
165
166 geometry_set.set_name(object->id.name + 2);
167 params.set_output("Geometry"_ustr, geometry_set);
168}
169
170using namespace blender::compositor;
171
173 public:
175
176 void execute() override
177 {
178 const Object *object = this->get_input("Object").get_single_value<Object *>();
179 if (!object) {
181 return;
182 }
183
184 const float4x4 transform = object->object_to_world();
185
186 float3 location, scale;
187 math::Quaternion rotation;
189
190 Result &transform_result = this->get_result("Transform");
191 if (transform_result.should_compute()) {
192 transform_result.allocate_single_value();
193 transform_result.set_single_value(transform);
194 }
195
196 Result &location_result = this->get_result("Location");
197 if (location_result.should_compute()) {
198 location_result.allocate_single_value();
199 location_result.set_single_value(location);
200 }
201
202 Result &rotation_result = this->get_result("Rotation");
203 if (rotation_result.should_compute()) {
204 rotation_result.allocate_single_value();
205 rotation_result.set_single_value(rotation);
206 }
207
208 Result &scale_result = this->get_result("Scale");
209 if (scale_result.should_compute()) {
210 scale_result.allocate_single_value();
211 scale_result.set_single_value(scale);
212 }
213 }
214};
215
217{
218 return new ObjectInfoOperation(context, node);
219}
220
221static void node_node_init(bNodeTree * /*tree*/, bNode *node)
222{
223 NodeGeometryObjectInfo *data = MEM_new<NodeGeometryObjectInfo>(__func__);
224 data->transform_space = GEO_NODE_TRANSFORM_SPACE_ORIGINAL;
225 node->storage = data;
226}
227
228static void node_rna(StructRNA *srna)
229{
230 static const EnumPropertyItem rna_node_geometry_object_info_transform_space_items[] = {
232 "ORIGINAL",
233 0,
234 "Original",
235 "Output the geometry relative to the input object transform, and the location, rotation "
236 "and "
237 "scale relative to the world origin"},
239 "RELATIVE",
240 0,
241 "Relative",
242 "Bring the input object geometry, location, rotation and scale into the modified object, "
243 "maintaining the relative position between the two objects in the scene"},
244 {0, nullptr, 0, nullptr, nullptr},
245 };
246
247 PropertyRNA *prop = RNA_def_node_enum(srna,
248 "transform_space",
249 "Transform Space",
250 "The transformation of the vector and geometry outputs",
251 rna_node_geometry_object_info_transform_space_items,
252 NOD_storage_enum_accessors(transform_space),
255}
256
258{
259 if (params.node_tree().type == NTREE_GEOMETRY) {
261 return;
262 }
263
264 static Set<UString> skip_socket_identifiers = {"As Instance"_ustr, "Geometry"_ustr};
265 nodes::search_filtered_link_ops_for_basic_node(params, skip_socket_identifiers);
266}
267
268static void node_register()
269{
270 static bke::bNodeType ntype;
271
272 geo_cmp_node_type_base(&ntype, "GeometryNodeObjectInfo"_ustr, GEO_NODE_OBJECT_INFO);
273 ntype.ui_name = "Object Info";
274 ntype.ui_description = "Retrieve information from an object";
275 ntype.enum_name_legacy = "OBJECT_INFO";
276 ntype.nclass = NODE_CLASS_INPUT;
277 ntype.initfunc = node_node_init;
279 ntype, "NodeGeometryObjectInfo", node_free_standard_storage, node_copy_standard_storage);
282 ntype.declare = node_declare;
286
287 node_rna(ntype.rna_ext.srna);
288}
290
291} // namespace blender::nodes::node_geo_object_info_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_OBJECT_INFO
#define TIP_(msgid)
Object is a sort of wrapper for general info.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
NodeOperation(Context &context, const bNode &node)
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 error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
GeometrySet object_get_evaluated_geometry_set(const Object &object, bool apply_subdiv=true)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
std::optional< TransformGeometryErrors > transform_geometry(bke::GeometrySet &geometry, const float4x4 &transform)
QuaternionBase< float > Quaternion
void to_loc_rot_scale_safe(const MatBase< T, 4, 4 > &mat, VecBase< T, 3 > &r_location, RotationT &r_rotation, VecBase< T, 3 > &r_scale)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_gather_link_searches(nodes::GatherLinkSearchOpParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
static void node_node_init(bNodeTree *, bNode *node)
void search_link_ops_for_basic_node(GatherLinkSearchOpParams &params)
void search_filtered_link_ops_for_basic_node(GatherLinkSearchOpParams &params, const Set< UString > &skip_socket_identifiers)
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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool DEG_object_transform_is_evaluated(const Object &object)
BMesh const char void * data
void rna_Node_update_relations(Main *bmain, Scene *scne, PointerRNA *ptr)
@ GEO_NODE_TRANSFORM_SPACE_ORIGINAL
@ GEO_NODE_TRANSFORM_SPACE_RELATIVE
T * DEG_get_original(T *id)
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
bool DEG_object_geometry_is_evaluated(const Object &object)
void geo_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeTree_Type type
void set_name(std::string name)
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
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_instances(std::unique_ptr< Instances > instances)
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)