Blender V5.0
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"
14
16#include "UI_resources.hh"
17
19
20#include "GEO_transform.hh"
21
22#include "BLT_translation.hh"
23
24#include "node_geometry_util.hh"
25
27
29
31{
32 b.add_input<decl::Object>("Object").optional_label();
33 b.add_input<decl::Bool>("As Instance")
35 "Output the entire object as single instance. "
36 "This allows instancing non-geometry object types");
37 b.add_output<decl::Matrix>("Transform")
39 "Transformation matrix containing the location, rotation and scale of the object");
40 b.add_output<decl::Vector>("Location");
41 b.add_output<decl::Rotation>("Rotation");
42 b.add_output<decl::Vector>("Scale");
43 b.add_output<decl::Geometry>("Geometry");
44}
45
46static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
47{
48 layout->prop(ptr, "transform_space", UI_ITEM_R_EXPAND, std::nullopt, ICON_NONE);
49}
50
52{
53 const NodeGeometryObjectInfo &storage = node_storage(params.node());
54 const bool transform_space_relative = (storage.transform_space ==
56
57 Object *object = params.extract_input<Object *>("Object");
58
59 const Object *self_object = params.self_object();
60 if (object == nullptr) {
61 params.set_default_remaining_outputs();
62 return;
63 }
64
65 const bool self_transform_evaluated = DEG_object_transform_is_evaluated(*self_object);
66 const bool object_transform_evaluated = DEG_object_transform_is_evaluated(*object);
67 const bool object_geometry_evaluated = DEG_object_geometry_is_evaluated(*object);
68
69 float4x4 output_transform = float4x4::identity();
70 bool show_transform_error = false;
71 if (transform_space_relative) {
72 if (self_transform_evaluated && object_transform_evaluated) {
73 output_transform = self_object->world_to_object() * object->object_to_world();
74 }
75 else {
76 show_transform_error = true;
77 }
78 }
79 else {
80 if (object_transform_evaluated) {
81 output_transform = object->object_to_world();
82 }
83 else {
84 show_transform_error = true;
85 }
86 }
87 if (show_transform_error) {
88 params.error_message_add(
90 TIP_("Cannot access object's transforms because it's not evaluated yet. "
91 "This can happen when there is a dependency cycle"));
92 }
93 float3 location, scale;
94 math::Quaternion rotation;
95 math::to_loc_rot_scale_safe<true>(output_transform, location, rotation, scale);
96
97 params.set_output("Location", location);
98 params.set_output("Rotation", rotation);
99 params.set_output("Scale", scale);
100 params.set_output("Transform", output_transform);
101
102 if (!params.output_is_required("Geometry")) {
103 return;
104 }
105 /* Compare by `orig_id` because objects may be copied into separate depsgraphs. */
106 if (DEG_get_original(object) == DEG_get_original(self_object)) {
107 params.error_message_add(
109 params.user_data()->call_data->operator_data ?
110 TIP_("Geometry cannot be retrieved from the edited object itself") :
111 TIP_("Geometry cannot be retrieved from the modifier object"));
112 params.set_default_remaining_outputs();
113 return;
114 }
115 BLI_assert(object != self_object);
116
117 if (!object_geometry_evaluated) {
118 params.error_message_add(
120 TIP_("Cannot access object's geometry because it's not evaluated yet. "
121 "This can happen when there is a dependency cycle"));
122 params.set_default_remaining_outputs();
123 return;
124 }
125
126 std::optional<float4x4> geometry_transform;
127 if (transform_space_relative) {
128 if (!self_transform_evaluated || !object_transform_evaluated) {
129 params.error_message_add(
131 TIP_("Cannot access object's transforms because it's not evaluated yet. "
132 "This can happen when there is a dependency cycle"));
133 params.set_default_remaining_outputs();
134 return;
135 }
136 geometry_transform = self_object->world_to_object() * object->object_to_world();
137 }
138
139 GeometrySet geometry_set;
140 if (params.extract_input<bool>("As Instance")) {
141 std::unique_ptr<bke::Instances> instances = std::make_unique<bke::Instances>();
142 const int handle = instances->add_reference(*object);
143 if (transform_space_relative) {
144 instances->add_instance(handle, *geometry_transform);
145 }
146 else {
147 instances->add_instance(handle, float4x4::identity());
148 }
149 geometry_set = GeometrySet::from_instances(instances.release());
150 }
151 else {
152 geometry_set = bke::object_get_evaluated_geometry_set(*object);
153 if (transform_space_relative) {
154 geometry::transform_geometry(geometry_set, *geometry_transform);
155 }
156 }
157
158 geometry_set.name = object->id.name + 2;
159 params.set_output("Geometry", geometry_set);
160}
161
162static void node_node_init(bNodeTree * /*tree*/, bNode *node)
163{
165 data->transform_space = GEO_NODE_TRANSFORM_SPACE_ORIGINAL;
166 node->storage = data;
167}
168
169static void node_rna(StructRNA *srna)
170{
171 static const EnumPropertyItem rna_node_geometry_object_info_transform_space_items[] = {
173 "ORIGINAL",
174 0,
175 "Original",
176 "Output the geometry relative to the input object transform, and the location, rotation "
177 "and "
178 "scale relative to the world origin"},
180 "RELATIVE",
181 0,
182 "Relative",
183 "Bring the input object geometry, location, rotation and scale into the modified object, "
184 "maintaining the relative position between the two objects in the scene"},
185 {0, nullptr, 0, nullptr, nullptr},
186 };
187
188 PropertyRNA *prop = RNA_def_node_enum(srna,
189 "transform_space",
190 "Transform Space",
191 "The transformation of the vector and geometry outputs",
192 rna_node_geometry_object_info_transform_space_items,
193 NOD_storage_enum_accessors(transform_space),
196}
197
198static void node_register()
199{
200 static blender::bke::bNodeType ntype;
201
202 geo_node_type_base(&ntype, "GeometryNodeObjectInfo", GEO_NODE_OBJECT_INFO);
203 ntype.ui_name = "Object Info";
204 ntype.ui_description = "Retrieve information from an object";
205 ntype.enum_name_legacy = "OBJECT_INFO";
206 ntype.nclass = NODE_CLASS_INPUT;
207 ntype.initfunc = node_node_init;
209 ntype, "NodeGeometryObjectInfo", node_free_standard_storage, node_copy_standard_storage);
212 ntype.declare = node_declare;
214
215 node_rna(ntype.rna_ext.srna);
216}
218
219} // namespace blender::nodes::node_geo_object_info_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1240
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define GEO_NODE_OBJECT_INFO
#define BLI_assert(a)
Definition BLI_assert.h:46
#define TIP_(msgid)
bool DEG_object_transform_is_evaluated(const Object &object)
bool DEG_object_geometry_is_evaluated(const Object &object)
T * DEG_get_original(T *id)
@ GEO_NODE_TRANSFORM_SPACE_RELATIVE
@ GEO_NODE_TRANSFORM_SPACE_ORIGINAL
Object is a sort of wrapper for general info.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void rna_Node_update_relations(Main *bmain, Scene *scne, PointerRNA *ptr)
#define NOD_storage_enum_accessors(member)
@ UI_ITEM_R_EXPAND
BMesh const char void * data
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
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:5414
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 void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_node_init(bNodeTree *, 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)
MatBase< float, 4, 4 > float4x4
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
StructRNA * srna
void * storage
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
static GeometrySet from_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238