Blender V5.0
rna_pointcloud.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
8
9#include "RNA_define.hh"
10#include "RNA_enum_types.hh"
11
12#include "rna_internal.hh"
13
14#include "BKE_attribute.h"
15
16#ifdef RNA_RUNTIME
17
18# include <fmt/format.h>
19
20# include "BLI_math_vector.h"
21# include "BLI_virtual_array.hh"
22
23# include "BKE_customdata.hh"
24# include "BKE_pointcloud.hh"
25
26# include "DEG_depsgraph.hh"
27
28# include "WM_api.hh"
29# include "WM_types.hh"
30
31using blender::float3;
32
33static PointCloud *rna_pointcloud(const PointerRNA *ptr)
34{
35 return (PointCloud *)ptr->owner_id;
36}
37
38static float3 *get_pointcloud_positions(PointCloud *pointcloud)
39{
40 return pointcloud->positions_for_write().data();
41}
42
43static const float3 *get_pointcloud_positions_const(const PointCloud *pointcloud)
44{
45 return pointcloud->positions().data();
46}
47
48static int rna_Point_index_get_const(const PointerRNA *ptr)
49{
50 const PointCloud *pointcloud = rna_pointcloud(ptr);
51 const float3 *co = static_cast<const float3 *>(ptr->data);
52 const float3 *positions = get_pointcloud_positions_const(pointcloud);
53 return int(co - positions);
54}
55
56static int rna_Point_index_get(PointerRNA *ptr)
57{
58 return rna_Point_index_get_const(ptr);
59}
60
61static int rna_PointCloud_points_length(PointerRNA *ptr)
62{
63 const PointCloud *pointcloud = rna_pointcloud(ptr);
64 return pointcloud->totpoint;
65}
66
67static void rna_PointCloud_points_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
68{
69 PointCloud *pointcloud = rna_pointcloud(ptr);
71 ptr,
72 get_pointcloud_positions(pointcloud),
73 sizeof(float3),
74 pointcloud->totpoint,
75 false,
76 nullptr);
77}
78
79bool rna_PointCloud_points_lookup_int(PointerRNA *ptr, int index, PointerRNA *r_ptr)
80{
81 PointCloud *pointcloud = rna_pointcloud(ptr);
82 if (index < 0 || index >= pointcloud->totpoint) {
83 return false;
84 }
86 *ptr, &RNA_Point, &get_pointcloud_positions(pointcloud)[index], *r_ptr);
87 return true;
88}
89
90static void rna_Point_location_get(PointerRNA *ptr, float value[3])
91{
92 copy_v3_v3(value, *static_cast<const float3 *>(ptr->data));
93}
94
95static void rna_Point_location_set(PointerRNA *ptr, const float value[3])
96{
97 *static_cast<float3 *>(ptr->data) = float3(value);
98}
99
100static float rna_Point_radius_get(PointerRNA *ptr)
101{
102 const PointCloud *pointcloud = rna_pointcloud(ptr);
103 return pointcloud->radius().get(rna_Point_index_get_const(ptr));
104}
105
106static void rna_Point_radius_set(PointerRNA *ptr, float value)
107{
108 PointCloud *pointcloud = rna_pointcloud(ptr);
109 pointcloud->radius_for_write()[rna_Point_index_get_const(ptr)] = value;
110}
111
112static std::optional<std::string> rna_Point_path(const PointerRNA *ptr)
113{
114 return fmt::format("points[{}]", rna_Point_index_get_const(ptr));
115}
116
117static void rna_PointCloud_update_data(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
118{
119 ID *id = ptr->owner_id;
120
121 /* cheating way for importers to avoid slow updates */
122 if (id->us > 0) {
123 DEG_id_tag_update(id, 0);
125 }
126}
127
128#else
129
130static void rna_def_point(BlenderRNA *brna)
131{
132 StructRNA *srna;
133 PropertyRNA *prop;
134
135 srna = RNA_def_struct(brna, "Point", nullptr);
136 RNA_def_struct_ui_text(srna, "Point", "Point in a point cloud");
137 RNA_def_struct_path_func(srna, "rna_Point_path");
138
139 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
140 RNA_def_property_array(prop, 3);
141 RNA_def_property_float_funcs(prop, "rna_Point_location_get", "rna_Point_location_set", nullptr);
142 RNA_def_property_ui_text(prop, "Location", "");
143 RNA_def_property_update(prop, 0, "rna_PointCloud_update_data");
144
145 prop = RNA_def_property(srna, "radius", PROP_FLOAT, PROP_DISTANCE);
146 RNA_def_property_float_funcs(prop, "rna_Point_radius_get", "rna_Point_radius_set", nullptr);
147 RNA_def_property_ui_text(prop, "Radius", "");
148 RNA_def_property_update(prop, 0, "rna_PointCloud_update_data");
149
150 prop = RNA_def_property(srna, "index", PROP_INT, PROP_UNSIGNED);
152 RNA_def_property_int_funcs(prop, "rna_Point_index_get", nullptr, nullptr);
153 RNA_def_property_ui_text(prop, "Index", "Index of this point");
154}
155
157{
158 StructRNA *srna;
159 PropertyRNA *prop;
160
161 srna = RNA_def_struct(brna, "PointCloud", "ID");
162 RNA_def_struct_ui_text(srna, "Point Cloud", "Point cloud data-block");
163 RNA_def_struct_ui_icon(srna, ICON_POINTCLOUD_DATA);
164
165 /* geometry */
166 prop = RNA_def_property(srna, "points", PROP_COLLECTION, PROP_NONE);
167 RNA_def_property_struct_type(prop, "Point");
170 "rna_PointCloud_points_begin",
171 "rna_iterator_array_next",
172 "rna_iterator_array_end",
173 "rna_iterator_array_get",
174 "rna_PointCloud_points_length",
175 "rna_PointCloud_points_lookup_int",
176 nullptr,
177 nullptr);
178 RNA_def_property_ui_text(prop, "Points", "");
179
180 /* materials */
181 prop = RNA_def_property(srna, "materials", PROP_COLLECTION, PROP_NONE);
182 RNA_def_property_collection_sdna(prop, nullptr, "mat", "totcol");
183 RNA_def_property_struct_type(prop, "Material");
184 RNA_def_property_ui_text(prop, "Materials", "");
185 RNA_def_property_srna(prop, "IDMaterials"); /* see rna_ID.cc */
187 nullptr,
188 nullptr,
189 nullptr,
190 nullptr,
191 nullptr,
192 nullptr,
193 nullptr,
194 "rna_IDMaterials_assign_int");
195
197
198 /* common */
200}
201
203{
204 rna_def_point(brna);
205 rna_def_pointcloud(brna);
206}
207
208#endif
Generic geometry attributes built on CustomData.
CustomData interface, see also DNA_customdata_types.h.
General operations for point clouds.
MINLINE void copy_v3_v3(float r[3], const float a[3])
void DEG_id_tag_update(ID *id, unsigned int flags)
@ PROP_FLOAT
Definition RNA_types.hh:164
@ PROP_INT
Definition RNA_types.hh:163
@ PROP_COLLECTION
Definition RNA_types.hh:168
@ PROPOVERRIDE_IGNORE
Definition RNA_types.hh:523
@ PROP_EDITABLE
Definition RNA_types.hh:306
@ PROP_DISTANCE
Definition RNA_types.hh:256
@ PROP_NONE
Definition RNA_types.hh:233
@ PROP_TRANSLATION
Definition RNA_types.hh:261
@ PROP_UNSIGNED
Definition RNA_types.hh:249
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
VecBase< float, 3 > float3
VecBase< float, 3 > float3
void rna_iterator_array_begin(CollectionPropertyIterator *iter, PointerRNA *ptr, void *data, size_t itemsize, int64_t length, bool free_ptr, IteratorSkipFunc skip)
void rna_pointer_create_with_ancestors(const PointerRNA &parent, StructRNA *type, void *data, PointerRNA &r_ptr)
void rna_def_animdata_common(StructRNA *srna)
void rna_def_attributes_common(StructRNA *srna, const AttributeOwnerType type)
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_property_collection_funcs(PropertyRNA *prop, const char *begin, const char *next, const char *end, const char *get, const char *length, const char *lookupint, const char *lookupstring, const char *assignint)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_array(PropertyRNA *prop, int length)
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
void RNA_def_property_collection_sdna(PropertyRNA *prop, const char *structname, const char *propname, const char *lengthpropname)
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_int_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
void RNA_def_property_override_flag(PropertyRNA *prop, PropertyOverrideFlag flag)
static void rna_def_point(BlenderRNA *brna)
void RNA_def_pointcloud(BlenderRNA *brna)
static void rna_def_pointcloud(BlenderRNA *brna)
Definition DNA_ID.h:414
int us
Definition DNA_ID.h:443
void WM_main_add_notifier(uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4238