Blender V5.3
bpy_geometry_set.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
15
16#include <sstream>
17
18#include "BKE_duplilist.hh"
19#include "BKE_geometry_set.hh"
21#include "BKE_idtype.hh"
22#include "BKE_instances.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_mesh_wrapper.hh"
25#include "BKE_pointcloud.hh"
26
28
29#include "DNA_ID.h"
31#include "DNA_mesh_types.h"
32#include "DNA_object_types.h"
34
35#include "RNA_enum_types.hh"
36#include "RNA_prototypes.hh"
37
38#include "bpy_geometry_set.hh"
39#include "bpy_rna.hh"
40
42
43namespace blender {
44
46
47extern PyTypeObject bpy_geometry_set_Type;
48
54
56{
57 BPy_GeometrySet *self = reinterpret_cast<BPy_GeometrySet *>(
59 if (self == nullptr) {
60 return nullptr;
61 }
62 new (&self->geometry) GeometrySet(std::move(geometry));
63 self->instances_pointcloud = nullptr;
64 /* We can't safely give access to shared geometries via the Python API currently, because
65 * constness can't be enforced. Therefore, ensure that this Python object has its own copy of
66 * each data-block. Note that attributes may still be shared with other data in Blender. */
67 self->geometry.ensure_no_shared_components();
68 return self;
69}
70
71static PyObject *BPy_GeometrySet_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
72{
73 static const char *kwlist[] = {nullptr};
74 if (!PyArg_ParseTupleAndKeywords(args, kwds, "", const_cast<char **>(kwlist))) {
75 return nullptr;
76 }
77 return reinterpret_cast<PyObject *>(python_object_from_geometry_set());
78}
79
81{
82 std::destroy_at(&self->geometry);
83 if (self->instances_pointcloud) {
84 BKE_id_free(nullptr, self->instances_pointcloud);
85 }
86 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
87}
88
90 /* Wrap. */
91 bpy_geometry_set_from_evaluated_object_doc,
92 ".. staticmethod:: from_evaluated_object(evaluated_object)\n"
93 "\n"
94 " Create a geometry set from the evaluated geometry of an evaluated object.\n"
95 " Typically, it's more convenient to use :func:`bpy.types.Object.evaluated_geometry`.\n"
96 "\n"
97 " :param evaluated_object: The evaluated object to create a geometry set from.\n"
98 " :type evaluated_object: bpy.types.Object\n");
100 PyObject *args,
101 PyObject *kwds)
102{
103 static const char *kwlist[] = {"evaluated_object", nullptr};
104 PyObject *py_evaluated_object;
105 if (!PyArg_ParseTupleAndKeywords(args,
106 kwds,
107 "O" /* `evaluated_object` */
108 ":from_evaluated_object",
109 const_cast<char **>(kwlist),
110 &py_evaluated_object))
111 {
112 return nullptr;
113 }
114 ID *evaluated_object_id = nullptr;
115 if (!pyrna_id_FromPyObject(py_evaluated_object, &evaluated_object_id)) {
116 PyErr_Format(
117 PyExc_TypeError, "Expected an Object, not %.200s", Py_TYPE(py_evaluated_object)->tp_name);
118 return nullptr;
119 }
120
121 if (GS(evaluated_object_id->name) != ID_OB) {
122 PyErr_Format(PyExc_TypeError,
123 "Expected an Object, not %.200s",
124 BKE_idtype_idcode_to_name(GS(evaluated_object_id->name)));
125 return nullptr;
126 }
127 Object *evaluated_object = reinterpret_cast<Object *>(evaluated_object_id);
128 if (!DEG_is_evaluated(evaluated_object)) {
129 PyErr_SetString(PyExc_TypeError, "Expected an evaluated object");
130 return nullptr;
131 }
132 const bool valid_object_type = DEG_object_has_geometry_component(evaluated_object);
133 if (!valid_object_type) {
134 const char *ob_type_name = "<unknown>";
135 RNA_enum_name_from_value(rna_enum_object_type_items, evaluated_object->type, &ob_type_name);
136 PyErr_Format(PyExc_TypeError, "Expected a geometry object, not %.200s", ob_type_name);
137 return nullptr;
138 }
139 if (!DEG_object_geometry_is_evaluated(*evaluated_object)) {
140 PyErr_SetString(PyExc_TypeError,
141 "Object geometry is not yet evaluated, is the depsgraph evaluated?");
142 return nullptr;
143 }
144 Depsgraph *depsgraph = DEG_get_depsgraph_by_id(*evaluated_object_id);
145 if (!depsgraph) {
146 PyErr_SetString(PyExc_TypeError, "Object is not owned by a depsgraph");
147 return nullptr;
148 }
149
150 const bool is_instance_collection = evaluated_object->type == OB_EMPTY &&
151 evaluated_object->instance_collection;
153 if (is_instance_collection) {
154 bke::Instances *instances = new bke::Instances(1);
155 const int handle = instances->add_new_reference(
156 bke::InstanceReference{*evaluated_object->instance_collection});
157 instances->reference_handles_for_write().first() = handle;
158 instances->transforms_for_write().first() = float4x4::identity();
159 geometry.replace_instances(instances);
160 }
161 else {
162 bke::Instances instances = object_duplilist_legacy_instances(*depsgraph, *evaluated_object);
163 geometry = bke::object_get_evaluated_geometry_set(*evaluated_object, false);
164 if (instances.instances_num() > 0) {
165 geometry.replace_instances(new bke::Instances(std::move(instances)));
166 }
167 }
169 return self;
170}
171
173{
174 std::stringstream ss;
175 ss << self->geometry;
176 std::string str = ss.str();
178}
179
181 /* Wrap. */
182 bpy_geometry_set_get_instances_pointcloud_doc,
183 ".. method:: instances_pointcloud()\n"
184 "\n"
185 " Get a pointcloud that encodes information about the instances of the geometry.\n"
186 " The returned pointcloud should not be modified.\n"
187 " There is a point per instance and per-instance data is stored in point attributes.\n"
188 " The local transforms are stored in the ``instance_transform`` attribute.\n"
189 " The data instanced by each point is referenced by the ``.reference_index`` attribute,\n"
190 " indexing into the list returned by :func:`bpy.types.GeometrySet.instance_references`.\n"
191 "\n"
192 " :rtype: bpy.types.PointCloud\n");
194{
195 const bke::Instances *instances = self->geometry.get_instances();
196 if (!instances) {
197 Py_RETURN_NONE;
198 }
199 if (self->instances_pointcloud == nullptr) {
200 const int instances_num = instances->instances_num();
201 PointCloud *pointcloud = BKE_pointcloud_new_nomain(PointCloudType::Points, instances_num);
202 bke::instance_position_varray(*instances).materialize(pointcloud->positions_for_write());
206 {},
207 IndexMask(instances_num),
208 pointcloud->attributes_for_write());
209 self->instances_pointcloud = pointcloud;
210 }
212}
213
215 /* Wrap. */
216 bpy_geometry_set_get_instance_references_doc,
217 ".. method:: instance_references()\n"
218 "\n"
219 " This returns a list of geometries that is indexed by the ``.reference_index``\n"
220 " attribute of the pointcloud returned by \n"
221 " :func:`bpy.types.GeometrySet.instances_pointcloud`.\n"
222 " It may contain other geometry sets, objects, collections and None values.\n"
223 "\n"
224 " :rtype: list[None | bpy.types.Object | bpy.types.Collection | bpy.types.GeometrySet]\n");
226{
227 const bke::Instances *instances = self->geometry.get_instances();
228 if (!instances) {
229 return PyList_New(0);
230 }
231 const Span<bke::InstanceReference> references = instances->references();
232 PyObject *py_references = PyList_New(references.size());
233 for (const int i : references.index_range()) {
234 const bke::InstanceReference &reference = references[i];
235 switch (reference.type()) {
237 PyList_SET_ITEM(py_references, i, Py_NewRef(Py_None));
238 break;
239 }
241 Object &object = reference.object();
242 PyList_SET_ITEM(py_references, i, pyrna_id_CreatePyObject(&object.id));
243 break;
244 }
246 Collection &collection = reference.collection();
247 PyList_SET_ITEM(py_references, i, pyrna_id_CreatePyObject(&collection.id));
248 break;
249 }
251 const bke::GeometrySet &geometry_set = reference.geometry_set();
252 PyList_SET_ITEM(py_references, i, python_object_from_geometry_set(geometry_set));
253 break;
254 }
255 }
256 }
257 return py_references;
258}
259
261 /* Wrap. */
262 bpy_geometry_set_name_doc,
263 "The name of the geometry set. It can be used for debugging purposes and is not unique.\n"
264 "\n"
265 ":type: str\n");
266static PyObject *BPy_GeometrySet_get_name(BPy_GeometrySet *self, void * /*closure*/)
267{
268 return PyC_UnicodeFromStdStr(self->geometry.name());
269}
270
271static int BPy_GeometrySet_set_name(BPy_GeometrySet *self, PyObject *value, void * /*closure*/)
272{
273 if (!PyUnicode_Check(value)) {
274 PyErr_SetString(PyExc_TypeError, "expected a string");
275 return -1;
276 }
277 const char *name = PyUnicode_AsUTF8(value);
278 self->geometry.set_name(name);
279 return 0;
280}
281
283 /* Wrap. */
284 bpy_geometry_set_mesh_doc,
285 "The mesh data-block in the geometry set.\n"
286 "\n"
287 ":type: :class:`bpy.types.Mesh`\n");
288static PyObject *BPy_GeometrySet_get_mesh(BPy_GeometrySet *self, void * /*closure*/)
289{
290 Mesh *base_mesh = self->geometry.get_mesh_for_write();
291 if (!base_mesh) {
292 Py_RETURN_NONE;
293 }
295 return pyrna_id_CreatePyObject(reinterpret_cast<ID *>(mesh));
296}
297
299 /* Wrap. */
300 bpy_geometry_set_mesh_base_doc,
301 "The mesh data-block in the geometry set without final subdivision.\n"
302 "\n"
303 ":type: :class:`bpy.types.Mesh`\n");
304static PyObject *BPy_GeometrySet_get_mesh_base(BPy_GeometrySet *self, void * /*closure*/)
305{
306 Mesh *base_mesh = self->geometry.get_mesh_for_write();
307 return pyrna_id_CreatePyObject(reinterpret_cast<ID *>(base_mesh));
308}
309
311 /* Wrap. */
312 bpy_geometry_set_pointcloud_doc,
313 "The point cloud data-block in the geometry set.\n"
314 "\n"
315 ":type: :class:`bpy.types.PointCloud`\n");
316static PyObject *BPy_GeometrySet_get_pointcloud(BPy_GeometrySet *self, void * /*closure*/)
317{
319 reinterpret_cast<ID *>(self->geometry.get_pointcloud_for_write()));
320}
321
323 /* Wrap. */
324 bpy_geometry_set_curves_doc,
325 "The curves data-block in the geometry set.\n"
326 "\n"
327 ":type: :class:`bpy.types.Curves`\n");
328static PyObject *BPy_GeometrySet_get_curves(BPy_GeometrySet *self, void * /*closure*/)
329{
330 return pyrna_id_CreatePyObject(reinterpret_cast<ID *>(self->geometry.get_curves_for_write()));
331}
332
334 /* Wrap. */
335 bpy_geometry_set_volume_doc,
336 "The volume data-block in the geometry set.\n"
337 "\n"
338 ":type: :class:`bpy.types.Volume`\n");
339static PyObject *BPy_GeometrySet_get_volume(BPy_GeometrySet *self, void * /*closure*/)
340{
341 return pyrna_id_CreatePyObject(reinterpret_cast<ID *>(self->geometry.get_volume_for_write()));
342}
343
345 /* Wrap. */
346 bpy_geometry_set_grease_pencil_doc,
347 "The Grease Pencil data-block in the geometry set.\n"
348 "\n"
349 ":type: :class:`bpy.types.GreasePencil`\n");
350static PyObject *BPy_GeometrySet_get_grease_pencil(BPy_GeometrySet *self, void * /*closure*/)
351{
353 reinterpret_cast<ID *>(self->geometry.get_grease_pencil_for_write()));
354}
355
356static PyGetSetDef BPy_GeometrySet_getseters[] = {
357 {
358 "name",
359 reinterpret_cast<getter>(BPy_GeometrySet_get_name),
360 reinterpret_cast<setter>(BPy_GeometrySet_set_name),
361 bpy_geometry_set_name_doc,
362 nullptr,
363 },
364 {
365 "mesh",
366 reinterpret_cast<getter>(BPy_GeometrySet_get_mesh),
367 nullptr,
368 bpy_geometry_set_mesh_doc,
369 nullptr,
370 },
371 {
372 "mesh_base",
373 reinterpret_cast<getter>(BPy_GeometrySet_get_mesh_base),
374 nullptr,
375 bpy_geometry_set_mesh_base_doc,
376 nullptr,
377 },
378 {
379 "pointcloud",
380 reinterpret_cast<getter>(BPy_GeometrySet_get_pointcloud),
381 nullptr,
382 bpy_geometry_set_pointcloud_doc,
383 nullptr,
384 },
385 {
386 "curves",
387 reinterpret_cast<getter>(BPy_GeometrySet_get_curves),
388 nullptr,
389 bpy_geometry_set_curves_doc,
390 nullptr,
391 },
392 {
393 "volume",
394 reinterpret_cast<getter>(BPy_GeometrySet_get_volume),
395 nullptr,
396 bpy_geometry_set_volume_doc,
397 nullptr,
398 },
399 {
400 "grease_pencil",
401 reinterpret_cast<getter>(BPy_GeometrySet_get_grease_pencil),
402 nullptr,
403 bpy_geometry_set_grease_pencil_doc,
404 nullptr,
405 },
406 {nullptr},
407
408};
409
410#ifdef __GNUC__
411# ifdef __clang__
412# pragma clang diagnostic push
413# pragma clang diagnostic ignored "-Wcast-function-type"
414# else
415# pragma GCC diagnostic push
416# pragma GCC diagnostic ignored "-Wcast-function-type"
417# endif
418#endif
419
420static PyMethodDef BPy_GeometrySet_methods[] = {
421 {"from_evaluated_object",
422 reinterpret_cast<PyCFunction>(BPy_GeometrySet_static_from_evaluated_object),
423 METH_VARARGS | METH_KEYWORDS | METH_STATIC,
424 bpy_geometry_set_from_evaluated_object_doc},
425 {"instances_pointcloud",
426 reinterpret_cast<PyCFunction>(BPy_GeometrySet_get_instances_pointcloud),
427 METH_NOARGS,
428 bpy_geometry_set_get_instances_pointcloud_doc},
429 {"instance_references",
430 reinterpret_cast<PyCFunction>(BPy_GeometrySet_get_instance_references),
431 METH_NOARGS,
432 bpy_geometry_set_get_instance_references_doc},
433 {nullptr, nullptr, 0, nullptr},
434};
435
436#ifdef __GNUC__
437# ifdef __clang__
438# pragma clang diagnostic pop
439# else
440# pragma GCC diagnostic pop
441# endif
442#endif
443
445 /* Wrap. */
446 bpy_geometry_set_doc,
447 "Stores potentially multiple geometry components of different types.\n"
448 "For example, it might contain a mesh, curves and nested instances.\n");
449PyTypeObject bpy_geometry_set_Type = {
450 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
451 /*tp_name*/ "GeometrySet",
452 /*tp_basicsize*/ sizeof(BPy_GeometrySet),
453 /*tp_itemsize*/ 0,
454 /*tp_dealloc*/ reinterpret_cast<destructor>(BPy_GeometrySet_dealloc),
455 /*tp_vectorcall_offset*/ 0,
456 /*tp_getattr*/ nullptr,
457 /*tp_setattr*/ nullptr,
458 /*tp_as_async*/ nullptr,
459 /*tp_repr*/ reinterpret_cast<reprfunc>(BPy_GeometrySet_repr),
460 /*tp_as_number*/ nullptr,
461 /*tp_as_sequence*/ nullptr,
462 /*tp_as_mapping*/ nullptr,
463 /*tp_hash*/ nullptr,
464 /*tp_call*/ nullptr,
465 /*tp_str*/ nullptr,
466 /*tp_getattro*/ nullptr,
467 /*tp_setattro*/ nullptr,
468 /*tp_as_buffer*/ nullptr,
469 /*tp_flags*/ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
470 /*tp_doc*/ bpy_geometry_set_doc,
471 /*tp_traverse*/ nullptr,
472 /*tp_clear*/ nullptr,
473 /*tp_richcompare*/ nullptr,
474 /*tp_weaklistoffset*/ 0,
475 /*tp_iter*/ nullptr,
476 /*tp_iternext*/ nullptr,
477 /*tp_methods*/ BPy_GeometrySet_methods,
478 /*tp_members*/ nullptr,
479 /*tp_getset*/ BPy_GeometrySet_getseters,
480 /*tp_base*/ nullptr,
481 /*tp_dict*/ nullptr,
482 /*tp_descr_get*/ nullptr,
483 /*tp_descr_set*/ nullptr,
484 /*tp_dictoffset*/ 0,
485 /*tp_init*/ nullptr,
486 /*tp_alloc*/ nullptr,
487 /*tp_new*/ BPy_GeometrySet_new,
488};
489
491{
492 if (PyType_Ready(&bpy_geometry_set_Type) < 0) {
493 return nullptr;
494 }
495 return reinterpret_cast<PyObject *>(&bpy_geometry_set_Type);
496}
497
498} // namespace blender
General operations for point clouds.
ID and Library types, which are fundamental for SDNA.
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
Collection & collection() const
MutableSpan< int > reference_handles_for_write()
Definition instances.cc:229
Span< InstanceReference > references() const
Definition instances.cc:290
int add_new_reference(InstanceReference reference)
Definition instances.cc:284
bke::AttributeAccessor attributes() const
Definition instances.cc:68
int instances_num() const
Definition instances.cc:408
MutableSpan< float4x4 > transforms_for_write()
Definition instances.cc:242
constexpr T & first() const
#define str(s)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
GeometrySet object_get_evaluated_geometry_set(const Object &object, bool apply_subdiv=true)
VArray< float3 > instance_position_varray(const Instances &instances)
Definition instances.cc:544
BMesh Iterators.
const char * BKE_idtype_idcode_to_name(short idcode)
Definition idtype.cc:261
static PyObject * BPy_GeometrySet_get_instances_pointcloud(BPy_GeometrySet *self)
static int BPy_GeometrySet_set_name(BPy_GeometrySet *self, PyObject *value, void *)
const EnumPropertyItem rna_enum_object_type_items[]
static PyObject * BPy_GeometrySet_get_curves(BPy_GeometrySet *self, void *)
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition bpy_rna.cc:8978
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static PyObject * BPy_GeometrySet_get_pointcloud(BPy_GeometrySet *self, void *)
static PyObject * BPy_GeometrySet_repr(BPy_GeometrySet *self)
Depsgraph * DEG_get_depsgraph_by_id(const ID &id)
PyObject * BPyInit_geometry_set_type()
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
static void BPy_GeometrySet_dealloc(BPy_GeometrySet *self)
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:8988
static BPy_GeometrySet * BPy_GeometrySet_static_from_evaluated_object(PyObject *, PyObject *args, PyObject *kwds)
void BKE_id_free(Main *bmain, void *idv)
bke::Instances object_duplilist_legacy_instances(Depsgraph &depsgraph, Object &ob)
static PyObject * BPy_GeometrySet_get_instance_references(BPy_GeometrySet *self)
static PyObject * BPy_GeometrySet_get_mesh(BPy_GeometrySet *self, void *)
static PyGetSetDef BPy_GeometrySet_getseters[]
static BPy_GeometrySet * python_object_from_geometry_set(GeometrySet geometry={})
static PyObject * BPy_GeometrySet_get_mesh_base(BPy_GeometrySet *self, void *)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
bool DEG_object_has_geometry_component(Object *object)
PyTypeObject bpy_geometry_set_Type
PyObject * PyC_UnicodeFromStdStr(const std::string &str)
static PyObject * BPy_GeometrySet_new(PyTypeObject *, PyObject *args, PyObject *kwds)
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
static PyMethodDef BPy_GeometrySet_methods[]
static PyObject * BPy_GeometrySet_get_grease_pencil(BPy_GeometrySet *self, void *)
static PyObject * BPy_GeometrySet_get_volume(BPy_GeometrySet *self, void *)
bool DEG_is_evaluated(const T *id)
bool DEG_object_geometry_is_evaluated(const Object &object)
Mesh * BKE_mesh_wrapper_ensure_subdivision(Mesh *mesh)
static PyObject * BPy_GeometrySet_get_name(BPy_GeometrySet *self, void *)
PyObject_HEAD GeometrySet geometry
char name[258]
Definition DNA_ID.h:433
struct Collection * instance_collection
StringRefNull name() const
void set_name(std::string name)
PointCloud * get_pointcloud_for_write()
const Instances * get_instances() const
GreasePencil * get_grease_pencil_for_write()