Blender  V3.3
bpy_rna.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #pragma once
8 
9 /* --- bpy build options --- */
10 #ifdef WITH_PYTHON_SAFETY
11 
16 # define USE_WEAKREFS
17 
18 /* method to invalidate removed py data, XXX, slow to remove objects, otherwise no overhead */
19 /* #define USE_PYRNA_INVALIDATE_GC */
20 
21 /* different method */
22 # define USE_PYRNA_INVALIDATE_WEAKREF
23 
24 /* support for inter references, currently only needed for corner case */
25 # define USE_PYRNA_STRUCT_REFERENCE
26 
27 #else /* WITH_PYTHON_SAFETY */
28 
29 /* default, no defines! */
30 
31 #endif /* !WITH_PYTHON_SAFETY */
32 
33 /* sanity checks on above defs */
34 #if defined(USE_PYRNA_INVALIDATE_WEAKREF) && !defined(USE_WEAKREFS)
35 # define USE_WEAKREFS
36 #endif
37 
38 #if defined(USE_PYRNA_INVALIDATE_GC) && defined(USE_PYRNA_INVALIDATE_WEAKREF)
39 # error "Only 1 reference check method at a time!"
40 #endif
41 
42 /* only used by operator introspection get_rna(), this is only used for doc gen
43  * so prefer the leak to the memory bloat for now. */
44 // #define PYRNA_FREE_SUPPORT
45 
46 /* use real collection iterators rather than faking with a list
47  * this is needed so enums can be iterated over without crashing,
48  * since finishing the iteration frees temp allocated enums */
49 #define USE_PYRNA_ITER
50 
51 /* --- end bpy build options --- */
52 
53 struct ID;
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 extern PyTypeObject pyrna_struct_meta_idprop_Type;
60 extern PyTypeObject pyrna_struct_Type;
61 extern PyTypeObject pyrna_prop_Type;
62 extern PyTypeObject pyrna_prop_array_Type;
63 extern PyTypeObject pyrna_prop_collection_Type;
64 extern PyTypeObject pyrna_func_Type;
65 
66 #define BPy_StructRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_struct_Type))
67 #define BPy_StructRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_struct_Type)
68 #define BPy_PropertyRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_prop_Type))
69 #define BPy_PropertyRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_prop_Type)
70 
71 #define PYRNA_STRUCT_CHECK_OBJ(obj) \
72  if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
73  return NULL; \
74  } \
75  (void)0
76 #define PYRNA_STRUCT_CHECK_INT(obj) \
77  if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
78  return -1; \
79  } \
80  (void)0
81 
82 #define PYRNA_PROP_CHECK_OBJ(obj) \
83  if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
84  return NULL; \
85  } \
86  (void)0
87 #define PYRNA_PROP_CHECK_INT(obj) \
88  if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
89  return -1; \
90  } \
91  (void)0
92 
93 #define PYRNA_STRUCT_IS_VALID(pysrna) (LIKELY(((BPy_StructRNA *)(pysrna))->ptr.type != NULL))
94 #define PYRNA_PROP_IS_VALID(pysrna) (LIKELY(((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL))
95 
96 /* 'in_weakreflist' MUST be aligned */
97 
98 typedef struct {
99  PyObject_HEAD /* Required Python macro. */
100 #ifdef USE_WEAKREFS
101  PyObject *in_weakreflist;
102 #endif
105 
106 typedef struct {
107  PyObject_HEAD /* Required Python macro. */
108 #ifdef USE_WEAKREFS
109  PyObject *in_weakreflist;
110 #endif
112 #ifdef USE_PYRNA_STRUCT_REFERENCE
113  /* generic PyObject we hold a reference to, example use:
114  * hold onto the collection iterator to prevent it from freeing allocated data we may use */
115  PyObject *reference;
116 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
117 
118 #ifdef PYRNA_FREE_SUPPORT
119  bool freeptr; /* needed in some cases if ptr.data is created on the fly, free when deallocing */
120 #endif /* PYRNA_FREE_SUPPORT */
121 } BPy_StructRNA;
122 
123 typedef struct {
124  PyObject_HEAD /* Required Python macro. */
125 #ifdef USE_WEAKREFS
126  PyObject *in_weakreflist;
127 #endif
131 
132 typedef struct {
133  PyObject_HEAD /* Required Python macro. */
134 #ifdef USE_WEAKREFS
135  PyObject *in_weakreflist;
136 #endif
139 
140  /* Arystan: this is a hack to allow sub-item r/w access like: face.uv[n][m] */
142  int arraydim;
146 
147 typedef struct {
148  PyObject_HEAD /* Required Python macro. */
149 #ifdef USE_WEAKREFS
150  PyObject *in_weakreflist;
151 #endif
152 
153  /* collection iterator specific parts */
156 
157 typedef struct {
158  PyObject_HEAD /* Required Python macro. */
159 #ifdef USE_WEAKREFS
160  PyObject *in_weakreflist;
161 #endif
165 
166 StructRNA *srna_from_self(PyObject *self, const char *error_prefix);
167 StructRNA *pyrna_struct_as_srna(PyObject *self, bool parent, const char *error_prefix);
168 
169 void BPY_rna_init(void);
170 void BPY_rna_exit(void);
171 PyObject *BPY_rna_module(void);
172 void BPY_update_rna_module(void);
173 // PyObject *BPY_rna_doc(void);
174 PyObject *BPY_rna_types(void);
175 
178 
179 /* extern'd by other modules which don't deal closely with RNA */
180 PyObject *pyrna_id_CreatePyObject(struct ID *id);
181 bool pyrna_id_FromPyObject(PyObject *obj, struct ID **id);
182 bool pyrna_id_CheckPyObject(PyObject *obj);
183 
184 /* operators also need this to set args */
185 int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, bool all_args, const char *error_prefix);
186 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
187 
188 int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class);
189 
190 const PointerRNA *pyrna_struct_as_ptr(PyObject *py_obj, const StructRNA *srna);
191 const PointerRNA *pyrna_struct_as_ptr_or_null(PyObject *py_obj, const StructRNA *srna);
192 
202  const PointerRNA *ptr;
203 };
204 
211 int pyrna_struct_as_ptr_parse(PyObject *o, void *p);
215 int pyrna_struct_as_ptr_or_null_parse(PyObject *o, void *p);
216 
217 void pyrna_struct_type_extend_capi(struct StructRNA *srna,
218  struct PyMethodDef *py_method,
219  struct PyGetSetDef *py_getset);
220 
221 /* called before stopping python */
222 void pyrna_alloc_types(void);
223 void pyrna_free_types(void);
224 
225 /* primitive type conversion */
227  PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix);
229  PropertyRNA *prop,
230  int arraydim,
231  int arrayoffset,
232  int index,
233  PyObject *py,
234  const char *error_prefix);
235 PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
236 
237 PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop);
239  PointerRNA *ptr,
240  PropertyRNA *prop,
241  int index);
243 int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value);
244 
245 bool pyrna_write_check(void);
246 void pyrna_write_set(bool val);
247 
251 
252 /* bpy.utils.(un)register_class */
253 extern PyMethodDef meth_bpy_register_class;
254 extern PyMethodDef meth_bpy_unregister_class;
255 
256 /* bpy.utils._bl_owner_(get/set) */
257 extern PyMethodDef meth_bpy_owner_id_set;
258 extern PyMethodDef meth_bpy_owner_id_get;
259 
261 
262 #ifdef __cplusplus
263 }
264 #endif
PyObject * pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:7596
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.c:6487
PyTypeObject pyrna_prop_Type
Definition: bpy_rna.c:6664
void pyrna_invalidate(BPy_DummyPointerRNA *self)
Definition: bpy_rna.c:123
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:1372
void BPY_rna_init(void)
Definition: bpy_rna.c:7673
void pyrna_alloc_types(void)
Definition: bpy_rna.c:8853
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, bool all_args, const char *error_prefix)
Definition: bpy_rna.c:1453
int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
Definition: bpy_rna.c:101
PyMethodDef meth_bpy_owner_id_set
Definition: bpy_rna.c:9307
bool pyrna_id_CheckPyObject(PyObject *obj)
Definition: bpy_rna.c:7668
const PointerRNA * pyrna_struct_as_ptr_or_null(PyObject *py_obj, const StructRNA *srna)
Definition: bpy_rna.c:7974
bool pyrna_write_check(void)
Definition: bpy_rna.c:344
void pyrna_struct_type_extend_capi(struct StructRNA *srna, struct PyMethodDef *py_method, struct PyGetSetDef *py_getset)
Definition: bpy_rna.c:9228
bool pyrna_id_FromPyObject(PyObject *obj, struct ID **id)
Definition: bpy_rna.c:7657
PyMethodDef meth_bpy_owner_id_get
Definition: bpy_rna.c:9301
StructRNA * pyrna_struct_as_srna(PyObject *self, bool parent, const char *error_prefix)
Definition: bpy_rna.c:7907
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7505
int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
void pyrna_free_types(void)
Definition: bpy_rna.c:8885
void BPY_update_rna_module(void)
Definition: bpy_rna.c:7762
int pyrna_struct_as_ptr_or_null_parse(PyObject *o, void *p)
Definition: bpy_rna.c:7993
int pyrna_struct_as_ptr_parse(PyObject *o, void *p)
Definition: bpy_rna.c:7982
int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix)
PyObject * BPY_rna_module(void)
Definition: bpy_rna.c:7749
PyObject * pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
const PointerRNA * pyrna_struct_as_ptr(PyObject *py_obj, const StructRNA *srna)
Definition: bpy_rna.c:7960
PyTypeObject pyrna_prop_array_Type
Definition: bpy_rna.c:6748
PyObject * pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:626
StructRNA * srna_from_self(PyObject *self, const char *error_prefix)
Definition: bpy_rna.c:8005
PyTypeObject pyrna_struct_Type
Definition: bpy_rna.c:6571
int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8230
PyTypeObject pyrna_func_Type
Definition: bpy_rna.c:7003
PyMethodDef meth_bpy_unregister_class
Definition: bpy_rna.c:9111
PyObject * pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop)
void pyrna_write_set(bool val)
Definition: bpy_rna.c:349
PyObject * pyrna_id_CreatePyObject(struct ID *id)
Definition: bpy_rna.c:7646
PyMethodDef meth_bpy_register_class
Definition: bpy_rna.c:8937
int pyrna_prop_validity_check(BPy_PropertyRNA *self)
Definition: bpy_rna.c:111
PyObject * pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
void BPY_rna_exit(void)
Definition: bpy_rna.c:7726
PyObject * BPY_rna_types(void)
Definition: bpy_rna.c:7877
BPy_StructRNA * bpy_context_module
Definition: bpy_rna.c:87
PyTypeObject pyrna_prop_collection_Type
Definition: bpy_rna.c:6831
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:103
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:162
FunctionRNA * func
Definition: bpy_rna.h:163
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:137
PropertyRNA * prop
Definition: bpy_rna.h:138
PyObject_HEAD CollectionPropertyIterator iter
Definition: bpy_rna.h:154
PropertyRNA * prop
Definition: bpy_rna.h:129
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:128
StructRNA * type
Definition: bpy_rna.h:200
const PointerRNA * ptr
Definition: bpy_rna.h:202
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:111
Definition: DNA_ID.h:368
PointerRNA * ptr
Definition: wm_files.c:3480