Blender V4.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"
14
15#include "UI_interface.hh"
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").hide_label();
33 b.add_input<decl::Bool>("As Instance")
34 .description(
35 "Output the entire object as single instance. "
36 "This allows instancing non-geometry object types");
37 b.add_output<decl::Matrix>("Transform")
38 .description(
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 uiItemR(layout, ptr, "transform_space", UI_ITEM_R_EXPAND, nullptr, 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.get_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(
89 NodeWarningType::Error,
90 TIP_("Can't 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_id(&object->id) == DEG_get_original_id(const_cast<ID *>(&self_object->id)))
107 {
108 params.error_message_add(
109 NodeWarningType::Error,
110 params.user_data()->call_data->operator_data ?
111 TIP_("Geometry cannot be retrieved from the edited object itself") :
112 TIP_("Geometry cannot be retrieved from the modifier object"));
113 params.set_default_remaining_outputs();
114 return;
115 }
116 BLI_assert(object != self_object);
117
118 if (!object_geometry_evaluated) {
119 params.error_message_add(NodeWarningType::Error,
120 TIP_("Can't 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(
130 NodeWarningType::Error,
131 TIP_("Can't 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.get_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{
164 NodeGeometryObjectInfo *data = MEM_cnew<NodeGeometryObjectInfo>(__func__);
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, GEO_NODE_OBJECT_INFO, "Object Info", NODE_CLASS_INPUT);
203 ntype.initfunc = node_node_init;
205 &ntype, "NodeGeometryObjectInfo", node_free_standard_storage, node_copy_standard_storage);
208 ntype.declare = node_declare;
210
211 node_rna(ntype.rna_ext.srna);
212}
214
215} // namespace blender::nodes::node_geo_object_info_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1799
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
#define BLI_assert(a)
Definition BLI_assert.h:50
#define TIP_(msgid)
bool DEG_object_transform_is_evaluated(const Object &object)
ID * DEG_get_original_id(ID *id)
bool DEG_object_geometry_is_evaluated(const Object &object)
@ 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)
void uiItemR(uiLayout *layout, PointerRNA *ptr, const char *propname, eUI_Item_Flag flag, const char *name, int icon)
@ UI_ITEM_R_EXPAND
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
GeometrySet object_get_evaluated_geometry_set(const Object &object)
void node_type_storage(bNodeType *ntype, const char *storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:4632
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
std::optional< TransformGeometryErrors > transform_geometry(bke::GeometrySet &geometry, const float4x4 &transform)
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)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:46
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:58
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
StructRNA * srna
Definition RNA_types.hh:780
Definition DNA_ID.h:413
static GeometrySet from_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:218
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:267
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:238
NodeDeclareFunction declare
Definition BKE_node.hh:347
PointerRNA * ptr
Definition wm_files.cc:4126