Blender  V2.93
node_geo_attribute_separate_xyz.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include "UI_interface.h"
18 #include "UI_resources.h"
19 
20 #include "node_geometry_util.hh"
21 
23  {SOCK_GEOMETRY, N_("Geometry")},
24  {SOCK_STRING, N_("Vector")},
25  {SOCK_VECTOR, N_("Vector"), 0.0f, 0.0f, 0.0f, 0.0f, -FLT_MAX, FLT_MAX},
26  {SOCK_STRING, N_("Result X")},
27  {SOCK_STRING, N_("Result Y")},
28  {SOCK_STRING, N_("Result Z")},
29  {-1, ""},
30 };
31 
33  {SOCK_GEOMETRY, N_("Geometry")},
34  {-1, ""},
35 };
36 
38  bContext *UNUSED(C),
39  PointerRNA *ptr)
40 {
41  uiLayoutSetPropSep(layout, true);
42  uiLayoutSetPropDecorate(layout, false);
43  uiItemR(layout, ptr, "input_type", 0, IFACE_("Type"), ICON_NONE);
44 }
45 
46 namespace blender::nodes {
47 
49 {
51  sizeof(NodeAttributeSeparateXYZ), __func__);
53  node->storage = data;
54 }
55 
57 {
58  NodeAttributeSeparateXYZ *node_storage = (NodeAttributeSeparateXYZ *)node->storage;
60  *node, "Vector", (GeometryNodeAttributeInputMode)node_storage->input_type);
61 }
62 
63 static void extract_input(const int index, const Span<float3> &input, MutableSpan<float> result)
64 {
65  for (const int i : result.index_range()) {
66  /* Get the component of the float3. (0: X, 1: Y, 2: Z). */
67  const float component = input[i][index];
68  result[i] = component;
69  }
70 }
71 
74  StringRef result_name_x,
75  StringRef result_name_y,
76  StringRef result_name_z)
77 {
78  /* Use the highest priority domain from any existing attribute outputs. */
79  Vector<AttributeDomain, 3> output_domains;
80  ReadAttributePtr attribute_x = component.attribute_try_get_for_read(result_name_x);
81  ReadAttributePtr attribute_y = component.attribute_try_get_for_read(result_name_y);
82  ReadAttributePtr attribute_z = component.attribute_try_get_for_read(result_name_z);
83  if (attribute_x) {
84  output_domains.append(attribute_x->domain());
85  }
86  if (attribute_y) {
87  output_domains.append(attribute_y->domain());
88  }
89  if (attribute_z) {
90  output_domains.append(attribute_z->domain());
91  }
92  if (output_domains.size() > 0) {
93  return bke::attribute_domain_highest_priority(output_domains);
94  }
95 
96  /* Otherwise use the domain of the input attribute, or the default. */
97  return params.get_highest_priority_input_domain({"Vector"}, component, ATTR_DOMAIN_POINT);
98 }
99 
101 {
102  const std::string result_name_x = params.get_input<std::string>("Result X");
103  const std::string result_name_y = params.get_input<std::string>("Result Y");
104  const std::string result_name_z = params.get_input<std::string>("Result Z");
105  if (result_name_x.empty() && result_name_y.empty() && result_name_z.empty()) {
106  return;
107  }
108 
109  /* The node is only for float3 to float conversions. */
110  const CustomDataType input_type = CD_PROP_FLOAT3;
111  const CustomDataType result_type = CD_PROP_FLOAT;
112  const AttributeDomain result_domain = get_result_domain(
113  component, params, result_name_x, result_name_y, result_name_z);
114 
115  ReadAttributePtr attribute_input = params.get_input_attribute(
116  "Vector", component, result_domain, input_type, nullptr);
117  if (!attribute_input) {
118  return;
119  }
120  const Span<float3> input_span = attribute_input->get_span<float3>();
121 
122  OutputAttributePtr attribute_result_x = component.attribute_try_get_for_output(
123  result_name_x, result_domain, result_type);
124  OutputAttributePtr attribute_result_y = component.attribute_try_get_for_output(
125  result_name_y, result_domain, result_type);
126  OutputAttributePtr attribute_result_z = component.attribute_try_get_for_output(
127  result_name_z, result_domain, result_type);
128 
129  /* Only extract the components for the outputs with a given attribute. */
130  if (attribute_result_x) {
131  extract_input(0, input_span, attribute_result_x->get_span_for_write_only<float>());
132  attribute_result_x.apply_span_and_save();
133  }
134  if (attribute_result_y) {
135  extract_input(1, input_span, attribute_result_y->get_span_for_write_only<float>());
136  attribute_result_y.apply_span_and_save();
137  }
138  if (attribute_result_z) {
139  extract_input(2, input_span, attribute_result_z->get_span_for_write_only<float>());
140  attribute_result_z.apply_span_and_save();
141  }
142 }
143 
145 {
146  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
147 
148  geometry_set = geometry_set_realize_instances(geometry_set);
149 
150  if (geometry_set.has<MeshComponent>()) {
152  }
153  if (geometry_set.has<PointCloudComponent>()) {
155  }
156 
157  params.set_output("Geometry", geometry_set);
158 }
159 
160 } // namespace blender::nodes
161 
163 {
164  static bNodeType ntype;
165 
167  &ntype, GEO_NODE_ATTRIBUTE_SEPARATE_XYZ, "Attribute Separate XYZ", NODE_CLASS_ATTRIBUTE, 0);
173  &ntype, "NodeAttributeSeparateXYZ", node_free_standard_storage, node_copy_standard_storage);
176  nodeRegisterType(&ntype);
177 }
AttributeDomain
Definition: BKE_attribute.h:41
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
#define GEO_NODE_ATTRIBUTE_SEPARATE_XYZ
Definition: BKE_node.h:1404
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4623
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_ATTRIBUTE
Definition: BKE_node.h:360
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
#define UNUSED(x)
#define IFACE_(msgid)
#define N_(msgid)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
CustomDataType
@ CD_PROP_FLOAT
@ CD_PROP_FLOAT3
@ SOCK_VECTOR
@ SOCK_GEOMETRY
@ SOCK_STRING
GeometryNodeAttributeInputMode
@ GEO_NODE_ATTRIBUTE_INPUT_ATTRIBUTE
#define C
Definition: RandGen.cpp:39
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
int64_t size() const
Definition: BLI_vector.hh:662
void append(const T &value)
Definition: BLI_vector.hh:438
fn::GMutableSpan get_span_for_write_only()
OperationNode * node
void * tree
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
AttributeDomain attribute_domain_highest_priority(Span< AttributeDomain > domains)
GeometrySet geometry_set_realize_instances(const GeometrySet &geometry_set)
std::unique_ptr< ReadAttribute > ReadAttributePtr
static void separate_attribute(GeometryComponent &component, const GeoNodeExecParams &params)
static void geo_node_attribute_separate_xyz_init(bNodeTree *UNUSED(tree), bNode *node)
void update_attribute_input_socket_availabilities(bNode &node, const StringRef name, const GeometryNodeAttributeInputMode mode, const bool name_is_available)
static AttributeDomain get_result_domain(const GeometryComponent &component, StringRef source_name, StringRef result_name)
static void geo_node_attribute_separate_xyz_exec(GeoNodeExecParams params)
static void extract_input(const int index, const Span< float3 > &input, MutableSpan< float > result)
static void geo_node_attribute_separate_xyz_update(bNodeTree *UNUSED(ntree), bNode *node)
static void geo_node_attribute_separate_xyz_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static bNodeSocketTemplate geo_node_attribute_separate_xyz_in[]
static bNodeSocketTemplate geo_node_attribute_separate_xyz_out[]
void register_node_type_geo_attribute_separate_xyz()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) const
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:253
PointerRNA * ptr
Definition: wm_files.c:3157