Blender V4.3
bpy_rna.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#pragma once
10
11/* --- bpy build options --- */
13#ifdef WITH_PYTHON_SAFETY
14
19# define USE_WEAKREFS
20
21/* method to invalidate removed py data, XXX, slow to remove objects, otherwise no overhead */
22// #define USE_PYRNA_INVALIDATE_GC
23
24/* different method */
25# define USE_PYRNA_INVALIDATE_WEAKREF
26
27/* support for inter references, currently only needed for corner case */
28# define USE_PYRNA_STRUCT_REFERENCE
29
30#else /* WITH_PYTHON_SAFETY */
31
32/* default, no defines! */
33
34#endif /* !WITH_PYTHON_SAFETY */
35
36/* Sanity checks on above defines. */
37#if defined(USE_PYRNA_INVALIDATE_WEAKREF) && !defined(USE_WEAKREFS)
38# define USE_WEAKREFS
39#endif
40
41#if defined(USE_PYRNA_INVALIDATE_GC) && defined(USE_PYRNA_INVALIDATE_WEAKREF)
42# error "Only 1 reference check method at a time!"
43#endif
44
45/* only used by operator introspection get_rna(), this is only used for doc gen
46 * so prefer the leak to the memory bloat for now. */
47// #define PYRNA_FREE_SUPPORT
48
49/* use real collection iterators rather than faking with a list
50 * this is needed so enums can be iterated over without crashing,
51 * since finishing the iteration frees temp allocated enums */
52#define USE_PYRNA_ITER
53
54/* --- end bpy build options --- */
55
56struct ID;
57
62extern PyTypeObject pyrna_struct_meta_idprop_Type;
63extern PyTypeObject pyrna_struct_Type;
64extern PyTypeObject pyrna_prop_Type;
65extern PyTypeObject pyrna_prop_array_Type;
66extern PyTypeObject pyrna_prop_collection_Type;
67extern PyTypeObject pyrna_func_Type;
68
69#define BPy_StructRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_struct_Type))
70#define BPy_StructRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_struct_Type)
71#define BPy_PropertyRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_prop_Type))
72#define BPy_PropertyRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_prop_Type)
73
74#define PYRNA_STRUCT_CHECK_OBJ(obj) \
75 if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
76 return NULL; \
77 } \
78 (void)0
79#define PYRNA_STRUCT_CHECK_INT(obj) \
80 if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
81 return -1; \
82 } \
83 (void)0
84
85#define PYRNA_PROP_CHECK_OBJ(obj) \
86 if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
87 return NULL; \
88 } \
89 (void)0
90#define PYRNA_PROP_CHECK_INT(obj) \
91 if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
92 return -1; \
93 } \
94 (void)0
95
96#define PYRNA_STRUCT_CHECK_OBJ_UNLESS(obj, unless) \
97 { \
98 const BPy_StructRNA *_obj = obj; \
99 if (UNLIKELY(pyrna_struct_validity_check_only(_obj) == -1) && !(unless)) { \
100 pyrna_struct_validity_exception_only(_obj); \
101 return NULL; \
102 } \
103 } \
104 (void)0
105
106#define PYRNA_STRUCT_IS_VALID(pysrna) (LIKELY(((BPy_StructRNA *)(pysrna))->ptr.type != NULL))
107#define PYRNA_PROP_IS_VALID(pysrna) (LIKELY(((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL))
108
109/* 'in_weakreflist' MUST be aligned */
110
112 PyObject_HEAD /* Required Python macro. */
113#ifdef USE_WEAKREFS
114 PyObject *in_weakreflist;
115#endif
117};
118
120 PyObject_HEAD /* Required Python macro. */
121#ifdef USE_WEAKREFS
122 PyObject *in_weakreflist;
123#endif
125#ifdef USE_PYRNA_STRUCT_REFERENCE
126 /* generic PyObject we hold a reference to, example use:
127 * hold onto the collection iterator to prevent it from freeing allocated data we may use */
128 PyObject *reference;
129#endif /* !USE_PYRNA_STRUCT_REFERENCE */
130
131#ifdef PYRNA_FREE_SUPPORT
133 bool freeptr;
134#endif /* PYRNA_FREE_SUPPORT */
135};
136
138 PyObject_HEAD /* Required Python macro. */
139#ifdef USE_WEAKREFS
140 PyObject *in_weakreflist;
141#endif
144};
145
147 PyObject_HEAD /* Required Python macro. */
148#ifdef USE_WEAKREFS
149 PyObject *in_weakreflist;
150#endif
153
154 /* Arystan: this is a hack to allow sub-item r/w access like: face.uv[n][m] */
159};
160
162 PyObject_HEAD /* Required Python macro. */
163#ifdef USE_WEAKREFS
164 PyObject *in_weakreflist;
165#endif
166
167 /* collection iterator specific parts */
169};
170
172 PyObject_HEAD /* Required Python macro. */
173#ifdef USE_WEAKREFS
174 PyObject *in_weakreflist;
175#endif
178};
179
180StructRNA *srna_from_self(PyObject *self, const char *error_prefix);
181StructRNA *pyrna_struct_as_srna(PyObject *self, bool parent, const char *error_prefix);
182
183void BPY_rna_init(void);
184void BPY_rna_exit(void);
185PyObject *BPY_rna_module(void);
186void BPY_update_rna_module(void);
187// PyObject *BPY_rna_doc(void);
188PyObject *BPY_rna_types(void);
189void BPY_rna_types_finalize_external_types(PyObject *submodule);
190
194
195/* Made public for other modules which don't deal closely with RNA. */
196PyObject *pyrna_id_CreatePyObject(ID *id);
197bool pyrna_id_FromPyObject(PyObject *obj, ID **id);
198bool pyrna_id_CheckPyObject(PyObject *obj);
199
200/* operators also need this to set args */
201int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, bool all_args, const char *error_prefix);
202PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
203
204int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class);
205
206const PointerRNA *pyrna_struct_as_ptr(PyObject *py_obj, const StructRNA *srna);
207const PointerRNA *pyrna_struct_as_ptr_or_null(PyObject *py_obj, const StructRNA *srna);
208
220
227int pyrna_struct_as_ptr_parse(PyObject *o, void *p);
231int pyrna_struct_as_ptr_or_null_parse(PyObject *o, void *p);
232
233void pyrna_struct_type_extend_capi(StructRNA *srna, PyMethodDef *method, PyGetSetDef *getset);
234
235/* Called before stopping Python. */
236
237void pyrna_alloc_types(void);
238void pyrna_free_types(void);
239
240/* Primitive type conversion. */
241
243 PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix);
245 PropertyRNA *prop,
246 int arraydim,
247 int arrayoffset,
248 int index,
249 PyObject *py,
250 const char *error_prefix);
251PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
252
256 PropertyRNA *prop,
257 int index);
259int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value);
260
261bool pyrna_write_check(void);
262void pyrna_write_set(bool val);
263
265
269
271
272/* bpy.utils.(un)register_class */
273extern PyMethodDef meth_bpy_register_class;
274extern PyMethodDef meth_bpy_unregister_class;
275
276/* bpy.utils._bl_owner_(get/set) */
277extern PyMethodDef meth_bpy_owner_id_set;
278extern PyMethodDef meth_bpy_owner_id_get;
279
PyObject * self
const PointerRNA * pyrna_struct_as_ptr(PyObject *py_obj, const StructRNA *srna)
Definition bpy_rna.cc:8177
int pyrna_py_to_array(PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix)
PyTypeObject pyrna_struct_meta_idprop_Type
Definition bpy_rna.cc:6816
PyTypeObject pyrna_prop_Type
Definition bpy_rna.cc:6943
void pyrna_invalidate(BPy_DummyPointerRNA *self)
Definition bpy_rna.cc:147
void BPY_rna_init(void)
Definition bpy_rna.cc:7828
void pyrna_alloc_types(void)
Definition bpy_rna.cc:9064
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, bool all_args, const char *error_prefix)
Definition bpy_rna.cc:1486
PyMethodDef meth_bpy_owner_id_set
Definition bpy_rna.cc:9536
bool pyrna_id_CheckPyObject(PyObject *obj)
Definition bpy_rna.cc:7823
bool pyrna_write_check(void)
Definition bpy_rna.cc:369
PyObject * pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
PyObject * pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
Definition bpy_rna.cc:651
void pyrna_struct_type_extend_capi(StructRNA *srna, PyMethodDef *method, PyGetSetDef *getset)
Definition bpy_rna.cc:9454
PyObject * BPY_rna_module(void)
Definition bpy_rna.cc:7904
PyMethodDef meth_bpy_owner_id_get
Definition bpy_rna.cc:9530
int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
void pyrna_struct_validity_exception_only(const BPy_StructRNA *pysrna)
Definition bpy_rna.cc:120
void pyrna_free_types(void)
Definition bpy_rna.cc:9099
void BPY_rna_types_finalize_external_types(PyObject *submodule)
Definition bpy_rna.cc:8071
void BPY_update_rna_module(void)
Definition bpy_rna.cc:7916
int pyrna_struct_as_ptr_or_null_parse(PyObject *o, void *p)
Definition bpy_rna.cc:8210
int pyrna_struct_as_ptr_parse(PyObject *o, void *p)
Definition bpy_rna.cc:8199
int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix)
StructRNA * srna_from_self(PyObject *self, const char *error_prefix)
Definition bpy_rna.cc:8223
PyObject * pyrna_id_CreatePyObject(ID *id)
Definition bpy_rna.cc:7802
int pyrna_struct_validity_check(const BPy_StructRNA *pysrna)
Definition bpy_rna.cc:126
int pyrna_prop_validity_check(const BPy_PropertyRNA *self)
Definition bpy_rna.cc:135
int pyrna_struct_validity_check_only(const BPy_StructRNA *pysrna)
Definition bpy_rna.cc:112
const PointerRNA * pyrna_struct_as_ptr_or_null(PyObject *py_obj, const StructRNA *srna)
Definition bpy_rna.cc:8191
PyTypeObject pyrna_prop_array_Type
Definition bpy_rna.cc:6999
PyTypeObject pyrna_struct_Type
Definition bpy_rna.cc:6876
StructRNA * pyrna_struct_as_srna(PyObject *self, bool parent, const char *error_prefix)
Definition bpy_rna.cc:8124
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
Definition bpy_rna.cc:1405
PyTypeObject pyrna_func_Type
Definition bpy_rna.cc:7170
int pyrna_deferred_register_class(StructRNA *srna, PyTypeObject *py_class)
Definition bpy_rna.cc:8442
PyObject * BPY_rna_types(void)
Definition bpy_rna.cc:8042
PyMethodDef meth_bpy_unregister_class
Definition bpy_rna.cc:9336
PyObject * pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition bpy_rna.cc:7752
void pyrna_write_set(bool val)
Definition bpy_rna.cc:374
PyMethodDef meth_bpy_register_class
Definition bpy_rna.cc:9153
bool pyrna_id_FromPyObject(PyObject *obj, ID **id)
Definition bpy_rna.cc:7812
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition bpy_rna.cc:7694
void BPY_rna_exit(void)
Definition bpy_rna.cc:7881
PyObject * pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
BPy_StructRNA * bpy_context_module
Definition bpy_rna.cc:91
PyObject * pyrna_struct_CreatePyObject_with_primitive_support(PointerRNA *ptr)
Definition bpy_rna.cc:7731
PyTypeObject pyrna_prop_collection_Type
Definition bpy_rna.cc:7055
PyObject * pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop)
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:116
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:176
FunctionRNA * func
Definition bpy_rna.hh:177
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:151
PropertyRNA * prop
Definition bpy_rna.hh:152
PyObject_HEAD CollectionPropertyIterator iter
Definition bpy_rna.hh:168
PropertyRNA * prop
Definition bpy_rna.hh:143
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:142
StructRNA * type
Definition bpy_rna.hh:216
const PointerRNA * ptr
Definition bpy_rna.hh:218
PyObject_HEAD PointerRNA ptr
Definition bpy_rna.hh:124
Definition DNA_ID.h:413
PointerRNA * ptr
Definition wm_files.cc:4126