Blender V5.3
rna_meta.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 <cstdlib>
10
11#include "DNA_meta_types.h"
12
13#include "RNA_define.hh"
14#include "RNA_enum_types.hh"
15
16#include "rna_internal.hh"
17
18#ifdef RNA_RUNTIME
19
20# include <fmt/format.h>
21
22# include "MEM_guardedalloc.h"
23
24# include "BLI_listbase.hh"
25# include "BLI_math_rotation_c.hh"
26# include "BLI_math_vector_c.hh"
27
28# include "DNA_object_types.h"
29# include "DNA_scene_types.h"
30
31# include "BKE_main.hh"
32# include "BKE_mball.hh"
33# include "BKE_scene.hh"
34
35# include "DEG_depsgraph.hh"
36
37# include "WM_api.hh"
38# include "WM_types.hh"
39
40namespace blender {
41
42static int rna_Meta_texspace_editable(const PointerRNA *ptr, const char ** /*r_info*/)
43{
44 MetaBall *mb = static_cast<MetaBall *>(ptr->data);
45 return (mb->texspace_flag & MB_TEXSPACE_FLAG_AUTO) ? 0 : int(PROP_EDITABLE);
46}
47
48static void rna_Meta_texspace_location_get(PointerRNA *ptr, float *values)
49{
50 MetaBall *mb = static_cast<MetaBall *>(ptr->data);
51
52 /* tex_space_mball() needs object.. ugh */
53
54 copy_v3_v3(values, mb->texspace_location);
55}
56
57static void rna_Meta_texspace_location_set(PointerRNA *ptr, const float *values)
58{
59 MetaBall *mb = static_cast<MetaBall *>(ptr->data);
60
61 copy_v3_v3(mb->texspace_location, values);
62}
63
64static void rna_Meta_texspace_size_get(PointerRNA *ptr, float *values)
65{
66 MetaBall *mb = static_cast<MetaBall *>(ptr->data);
67
68 /* tex_space_mball() needs object.. ugh */
69
70 copy_v3_v3(values, mb->texspace_size);
71}
72
73static void rna_Meta_texspace_size_set(PointerRNA *ptr, const float *values)
74{
75 MetaBall *mb = static_cast<MetaBall *>(ptr->data);
76
77 copy_v3_v3(mb->texspace_size, values);
78}
79
80static void rna_MetaBall_redraw_data(Main * /*bmain*/, Scene * /*scene*/, PointerRNA *ptr)
81{
82 ID *id = ptr->owner_id;
83
86}
87
88static void rna_MetaBall_update_data(Main *bmain, Scene * /*scene*/, PointerRNA *ptr)
89{
90 MetaBall *mb = id_cast<MetaBall *>(ptr->owner_id);
91
92 /* NOTE: The check on the number of users allows to avoid many repetitive (slow) updates in some
93 * cases, like e.g. importers. Calling `BKE_mball_properties_copy` on an obdata with no users
94 * would be meaningless anyway, as by definition it would not be used by any object, so not part
95 * of any meta-ball group. */
96 if (mb->id.us > 0) {
98
99 DEG_id_tag_update(&mb->id, 0);
101 }
102}
103
104static void rna_MetaBall_update_rotation(Main *bmain, Scene *scene, PointerRNA *ptr)
105{
106 MetaElem *ml = static_cast<MetaElem *>(ptr->data);
107 normalize_qt(ml->quat);
108 rna_MetaBall_update_data(bmain, scene, ptr);
109}
110
111static MetaElem *rna_MetaBall_elements_new(MetaBall *mb, int type)
112{
113 MetaElem *ml = BKE_mball_element_add(mb, type);
114
115 /* cheating way for importers to avoid slow updates */
116 if (mb->id.us > 0) {
117 DEG_id_tag_update(&mb->id, 0);
119 }
120
121 return ml;
122}
123
124static void rna_MetaBall_elements_remove(MetaBall *mb, ReportList *reports, PointerRNA *ml_ptr)
125{
126 MetaElem *ml = static_cast<MetaElem *>(ml_ptr->data);
127
128 if (BLI_remlink_safe(&mb->elems, ml) == false) {
130 reports, RPT_ERROR, "Metaball '%s' does not contain spline given", mb->id.name + 2);
131 return;
132 }
133
134 MEM_delete(ml);
135 ml_ptr->invalidate();
136
137 /* cheating way for importers to avoid slow updates */
138 if (mb->id.us > 0) {
139 DEG_id_tag_update(&mb->id, 0);
141 }
142}
143
144static void rna_MetaBall_elements_clear(MetaBall *mb)
145{
146 mb->elems.free_no_destruct();
147
148 /* cheating way for importers to avoid slow updates */
149 if (mb->id.us > 0) {
150 DEG_id_tag_update(&mb->id, 0);
152 }
153}
154
155static bool rna_Meta_is_editmode_get(PointerRNA *ptr)
156{
157 MetaBall *mb = id_cast<MetaBall *>(ptr->owner_id);
158 return (mb->editelems != nullptr);
159}
160
161static std::optional<std::string> rna_MetaElement_path(const PointerRNA *ptr)
162{
163 const MetaBall *mb = id_cast<MetaBall *>(ptr->owner_id);
164 const MetaElem *ml = static_cast<MetaElem *>(ptr->data);
165 int index = -1;
166
167 if (mb->editelems) {
168 index = BLI_findindex(mb->editelems, ml);
169 }
170 if (index == -1) {
171 index = BLI_findindex(&mb->elems, ml);
172 }
173 if (index == -1) {
174 return std::nullopt;
175 }
176
177 return fmt::format("elements[{}]", index);
178}
179
180} // namespace blender
181
182#else
183
184namespace blender {
185
187{
188 StructRNA *srna;
189 PropertyRNA *prop;
190
191 srna = RNA_def_struct(brna, "MetaElement", nullptr);
192 RNA_def_struct_sdna(srna, "MetaElem");
193 RNA_def_struct_ui_text(srna, "Metaball Element", "Blobby element in a metaball data-block");
194 RNA_def_struct_path_func(srna, "rna_MetaElement_path");
195 RNA_def_struct_ui_icon(srna, ICON_OUTLINER_DATA_META);
196
197 /* enums */
198 prop = RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
200 RNA_def_property_ui_text(prop, "Type", "Metaball type");
201 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
202
203 /* number values */
204 prop = RNA_def_property(srna, "co", PROP_FLOAT, PROP_TRANSLATION);
205 RNA_def_property_float_sdna(prop, nullptr, "x");
206 RNA_def_property_array(prop, 3);
207 RNA_def_property_ui_text(prop, "Location", "");
208 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
209
210 prop = RNA_def_property(srna, "rotation", PROP_FLOAT, PROP_QUATERNION);
211 RNA_def_property_float_sdna(prop, nullptr, "quat");
212 RNA_def_property_ui_text(prop, "Rotation", "Normalized quaternion rotation");
213 RNA_def_property_update(prop, 0, "rna_MetaBall_update_rotation");
214
216 RNA_def_property_float_sdna(prop, nullptr, "rad");
217 RNA_def_property_ui_text(prop, "Radius", "");
218 RNA_def_property_range(prop, 0.0f, FLT_MAX);
219 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
220
221 prop = RNA_def_property(srna, "size_x", PROP_FLOAT, PROP_DISTANCE);
222 RNA_def_property_float_sdna(prop, nullptr, "expx");
224 RNA_def_property_range(prop, 0.0f, 20.0f);
226 prop, "Size X", "Size of element, use of components depends on element type");
227 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
228
229 prop = RNA_def_property(srna, "size_y", PROP_FLOAT, PROP_DISTANCE);
230 RNA_def_property_float_sdna(prop, nullptr, "expy");
232 RNA_def_property_range(prop, 0.0f, 20.0f);
234 prop, "Size Y", "Size of element, use of components depends on element type");
235 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
236
237 prop = RNA_def_property(srna, "size_z", PROP_FLOAT, PROP_DISTANCE);
238 RNA_def_property_float_sdna(prop, nullptr, "expz");
240 RNA_def_property_range(prop, 0.0f, 20.0f);
242 prop, "Size Z", "Size of element, use of components depends on element type");
243 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
244
245 prop = RNA_def_property(srna, "stiffness", PROP_FLOAT, PROP_NONE);
246 RNA_def_property_float_sdna(prop, nullptr, "s");
247 RNA_def_property_range(prop, 0.0f, 10.0f);
248 RNA_def_property_ui_text(prop, "Stiffness", "Stiffness defines how much of the element to fill");
249 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
250
251 /* flags */
252 prop = RNA_def_property(srna, "use_negative", PROP_BOOLEAN, PROP_NONE);
253 RNA_def_property_boolean_sdna(prop, nullptr, "flag", MB_NEGATIVE);
254 RNA_def_property_ui_text(prop, "Negative", "Set metaball as negative one");
255 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
256
257 prop = RNA_def_property(srna, "use_scale_stiffness", PROP_BOOLEAN, PROP_NONE);
259 RNA_def_property_ui_text(prop, "Scale Stiffness", "Scale stiffness instead of radius");
260 RNA_def_property_update(prop, 0, "rna_MetaBall_redraw_data");
261
262 prop = RNA_def_property(srna, "select", PROP_BOOLEAN, PROP_NONE);
263 RNA_def_property_boolean_sdna(prop, nullptr, "flag", 1); /* SELECT */
264 RNA_def_property_ui_text(prop, "Select", "Select element");
265 RNA_def_property_update(prop, 0, "rna_MetaBall_redraw_data");
266
267 prop = RNA_def_property(srna, "hide", PROP_BOOLEAN, PROP_NONE);
268 RNA_def_property_boolean_sdna(prop, nullptr, "flag", MB_HIDE);
269 RNA_def_property_ui_text(prop, "Hide", "Hide element");
270 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
271}
272
273/* mball.elements */
275{
276 StructRNA *srna;
277 PropertyRNA *prop;
278
279 FunctionRNA *func;
280 PropertyRNA *parm;
281
282 RNA_def_property_srna(cprop, "MetaBallElements");
283 srna = RNA_def_struct(brna, "MetaBallElements", nullptr);
284 RNA_def_struct_sdna(srna, "MetaBall");
285 RNA_def_struct_ui_text(srna, "Metaball Elements", "Collection of metaball elements");
286
287 func = RNA_def_function(srna, "new", "rna_MetaBall_elements_new");
288 RNA_def_function_ui_description(func, "Add a new element to the metaball");
289 RNA_def_enum(func,
290 "type",
292 MB_BALL,
293 "",
294 "Type for the new metaball element");
295 parm = RNA_def_pointer(func, "element", "MetaElement", "", "The newly created metaball element");
297 RNA_def_function_return(func, parm);
298
299 func = RNA_def_function(srna, "remove", "rna_MetaBall_elements_remove");
300 RNA_def_function_ui_description(func, "Remove an element from the metaball");
302 parm = RNA_def_pointer(func, "element", "MetaElement", "", "The element to remove");
305
306 func = RNA_def_function(srna, "clear", "rna_MetaBall_elements_clear");
307 RNA_def_function_ui_description(func, "Remove all elements from the metaball");
308
309 prop = RNA_def_property(srna, "active", PROP_POINTER, PROP_NONE);
310 RNA_def_property_pointer_sdna(prop, nullptr, "lastelem");
311 RNA_def_property_ui_text(prop, "Active Element", "Last selected element");
312}
313
314static void rna_def_metaball(BlenderRNA *brna)
315{
316 StructRNA *srna;
317 PropertyRNA *prop;
318 static const EnumPropertyItem prop_update_items[] = {
319 {MB_UPDATE_ALWAYS, "UPDATE_ALWAYS", 0, "Always", "While editing, update metaball always"},
321 "HALFRES",
322 0,
323 "Half",
324 "While editing, update metaball in half resolution"},
325 {MB_UPDATE_FAST, "FAST", 0, "Fast", "While editing, update metaball without polygonization"},
326 {MB_UPDATE_NEVER, "NEVER", 0, "Never", "While editing, don't update metaball at all"},
327 {0, nullptr, 0, nullptr, nullptr},
328 };
329
330 srna = RNA_def_struct(brna, "MetaBall", "ID");
331 RNA_def_struct_ui_text(srna, "MetaBall", "Metaball data-block to define blobby surfaces");
332 RNA_def_struct_ui_icon(srna, ICON_META_DATA);
333
334 prop = RNA_def_property(srna, "elements", PROP_COLLECTION, PROP_NONE);
335 RNA_def_property_collection_sdna(prop, nullptr, "elems", nullptr);
336 RNA_def_property_struct_type(prop, "MetaElement");
337 RNA_def_property_ui_text(prop, "Elements", "Metaball elements");
338 rna_def_metaball_elements(brna, prop);
339
340 /* enums */
341 prop = RNA_def_property(srna, "update_method", PROP_ENUM, PROP_NONE);
342 RNA_def_property_enum_sdna(prop, nullptr, "flag");
343 RNA_def_property_enum_items(prop, prop_update_items);
344 RNA_def_property_ui_text(prop, "Update", "Metaball edit update behavior");
345 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
346
347 /* number values */
348 prop = RNA_def_property(srna, "resolution", PROP_FLOAT, PROP_DISTANCE);
349 RNA_def_property_float_sdna(prop, nullptr, "wiresize");
350 RNA_def_property_range(prop, 0.005f, 10000.0f);
351 RNA_def_property_ui_range(prop, 0.05f, 1000.0f, 2.5f, 3);
352 RNA_def_property_ui_text(prop, "Viewport Size", "Polygonization resolution in the 3D viewport");
353 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
354
355 prop = RNA_def_property(srna, "render_resolution", PROP_FLOAT, PROP_DISTANCE);
356 RNA_def_property_float_sdna(prop, nullptr, "rendersize");
357 RNA_def_property_range(prop, 0.005f, 10000.0f);
358 RNA_def_property_ui_range(prop, 0.025f, 1000.0f, 2.5f, 3);
359 RNA_def_property_ui_text(prop, "Render Size", "Polygonization resolution in rendering");
360 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
361
362 prop = RNA_def_property(srna, "threshold", PROP_FLOAT, PROP_NONE);
363 RNA_def_property_float_sdna(prop, nullptr, "thresh");
364 RNA_def_property_range(prop, 0.0f, 5.0f);
365 RNA_def_property_ui_text(prop, "Threshold", "Influence of metaball elements");
366 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
367
368 /* texture space */
369 prop = RNA_def_property(srna, "use_auto_texspace", PROP_BOOLEAN, PROP_NONE);
370 RNA_def_property_boolean_sdna(prop, nullptr, "texspace_flag", MB_TEXSPACE_FLAG_AUTO);
372 prop,
373 "Auto Texture Space",
374 "Adjust active object's texture space automatically when transforming object");
375
376 prop = RNA_def_property(srna, "texspace_location", PROP_FLOAT, PROP_TRANSLATION);
377 RNA_def_property_array(prop, 3);
378 RNA_def_property_ui_text(prop, "Texture Space Location", "Texture space location");
379 RNA_def_property_editable_func(prop, "rna_Meta_texspace_editable");
381 prop, "rna_Meta_texspace_location_get", "rna_Meta_texspace_location_set", nullptr);
382 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
383
384 prop = RNA_def_property(srna, "texspace_size", PROP_FLOAT, PROP_XYZ);
385 RNA_def_property_array(prop, 3);
387 RNA_def_property_ui_text(prop, "Texture Space Size", "Texture space size");
388 RNA_def_property_editable_func(prop, "rna_Meta_texspace_editable");
390 prop, "rna_Meta_texspace_size_get", "rna_Meta_texspace_size_set", nullptr);
391 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
392
393 /* not supported yet */
394# if 0
395 prop = RNA_def_property(srna, "texspace_rot", PROP_FLOAT, PROP_EULER);
396 RNA_def_property_float(prop, nullptr, "rot");
397 RNA_def_property_ui_text(prop, "Texture Space Rotation", "Texture space rotation");
398 RNA_def_property_editable_func(prop, "rna_Meta_texspace_editable");
400 RNA_def_property_update(prop, 0, "rna_MetaBall_update_data");
401# endif
402
403 /* materials */
404 prop = RNA_def_property(srna, "materials", PROP_COLLECTION, PROP_NONE);
405 RNA_def_property_collection_sdna(prop, nullptr, "mat", "totcol");
406 RNA_def_property_struct_type(prop, "Material");
407 RNA_def_property_ui_text(prop, "Materials", "");
408 RNA_def_property_srna(prop, "IDMaterials"); /* see rna_ID.cc */
410 nullptr,
411 nullptr,
412 nullptr,
413 nullptr,
414 nullptr,
415 nullptr,
416 nullptr,
417 "rna_IDMaterials_assign_int");
418
419 prop = RNA_def_property(srna, "is_editmode", PROP_BOOLEAN, PROP_NONE);
420 RNA_def_property_boolean_funcs(prop, "rna_Meta_is_editmode_get", nullptr);
422 RNA_def_property_ui_text(prop, "Is Editmode", "True when used in editmode");
423
424 /* anim */
426
427 RNA_api_meta(srna);
428}
429
431{
433 rna_def_metaball(brna);
434}
435
436} // namespace blender
437
438#endif
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define RNA_TRANSLATION_PREC_DEFAULT
Definition RNA_types.hh:272
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
#define FLT_MAX
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void RNA_def_property_srna(PropertyRNA *prop, const char *type)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_editable_func(PropertyRNA *prop, const char *editable)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_struct_ui_icon(StructRNA *srna, int icon)
MetaElem * BKE_mball_element_add(MetaBall *mb, int type)
Definition mball.cc:179
void RNA_def_property_boolean_negative_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t booleanbit)
static void rna_def_metaball(BlenderRNA *brna)
Definition rna_meta.cc:314
MINLINE void copy_v3_v3(float r[3], const float a[3])
static void rna_def_metaball_elements(BlenderRNA *brna, PropertyRNA *cprop)
Definition rna_meta.cc:274
void RNA_def_property_float_funcs(PropertyRNA *prop, const char *get, const char *set, const char *range)
void RNA_def_property_pointer_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_collection_sdna(PropertyRNA *prop, const char *structname, const char *propname, const char *lengthpropname)
void RNA_def_property_boolean_funcs(PropertyRNA *prop, const char *get, const char *set)
void RNA_def_function_ui_description(FunctionRNA *func, const char *description)
float normalize_qt(float q[4])
@ PROP_UNIT_LENGTH
Definition RNA_types.hh:222
@ FUNC_USE_REPORTS
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static void rna_def_metaelement(BlenderRNA *brna)
Definition rna_meta.cc:186
void RNA_api_meta(StructRNA *srna)
PropertyRNA * RNA_def_pointer(StructOrFunctionRNA *cont_, const char *identifier, const char *type, const char *ui_name, const char *ui_description)
const EnumPropertyItem rna_enum_metaelem_type_items[]
void RNA_def_property_struct_type(PropertyRNA *prop, const char *type)
void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, int precision)
void RNA_def_property_float_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void rna_def_animdata_common(StructRNA *srna)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void RNA_def_parameter_clear_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
void RNA_def_property_enum_items(PropertyRNA *prop, const EnumPropertyItem *item)
@ PROP_BOOLEAN
Definition RNA_types.hh:210
@ PROP_POINTER
Definition RNA_types.hh:215
@ PROP_COLLECTION
Definition RNA_types.hh:216
void RNA_def_parameter_flags(PropertyRNA *prop, PropertyFlag flag_property, ParameterFlag flag_parameter)
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 DEG_id_tag_update(ID *id, unsigned int flags)
void RNA_def_property_boolean_sdna(PropertyRNA *prop, const char *structname, const char *propname, int64_t booleanbit)
void RNA_def_property_range(PropertyRNA *prop, double min, double max)
void RNA_def_struct_path_func(StructRNA *srna, const char *path)
void RNA_def_function_return(FunctionRNA *func, PropertyRNA *ret)
FunctionRNA * RNA_def_function(StructRNA *srna, const char *identifier_c_str, const char *call)
void BKE_mball_properties_copy(Main *bmain, MetaBall *metaball_src)
Definition mball.cc:340
void RNA_def_property_enum_sdna(PropertyRNA *prop, const char *structname, const char *propname)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_function_flag(FunctionRNA *func, FunctionFlag flag)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_PROPORTIONAL
Definition RNA_types.hh:419
@ PROP_THICK_WRAP
Definition RNA_types.hh:493
@ PROP_NEVER_NULL
Definition RNA_types.hh:447
void RNA_def_property_update(PropertyRNA *prop, int noteflag, const char *func)
@ MB_UPDATE_HALFRES
bool BLI_remlink_safe(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:161
@ PROP_TRANSLATION
Definition RNA_types.hh:318
@ PROP_DISTANCE
Definition RNA_types.hh:313
@ PROP_QUATERNION
Definition RNA_types.hh:324
@ PROP_UNSIGNED
Definition RNA_types.hh:306
@ PARM_REQUIRED
Definition RNA_types.hh:631
@ PARM_RNAPTR
Definition RNA_types.hh:633
void RNA_def_property_array(PropertyRNA *prop, int length)
void RNA_def_meta(BlenderRNA *brna)
Definition rna_meta.cc:430
@ MB_TEXSPACE_FLAG_AUTO