Blender  V2.93
bpy_rna.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #pragma once
22 
23 /* --- bpy build options --- */
24 #ifdef WITH_PYTHON_SAFETY
25 
30 # define USE_WEAKREFS
31 
32 /* method to invalidate removed py data, XXX, slow to remove objects, otherwise no overhead */
33 /* #define USE_PYRNA_INVALIDATE_GC */
34 
35 /* different method */
36 # define USE_PYRNA_INVALIDATE_WEAKREF
37 
38 /* support for inter references, currently only needed for corner case */
39 # define USE_PYRNA_STRUCT_REFERENCE
40 
41 #else /* WITH_PYTHON_SAFETY */
42 
43 /* default, no defines! */
44 
45 #endif /* !WITH_PYTHON_SAFETY */
46 
47 /* sanity checks on above defs */
48 #if defined(USE_PYRNA_INVALIDATE_WEAKREF) && !defined(USE_WEAKREFS)
49 # define USE_WEAKREFS
50 #endif
51 
52 #if defined(USE_PYRNA_INVALIDATE_GC) && defined(USE_PYRNA_INVALIDATE_WEAKREF)
53 # error "Only 1 reference check method at a time!"
54 #endif
55 
56 /* only used by operator introspection get_rna(), this is only used for doc gen
57  * so prefer the leak to the memory bloat for now. */
58 // #define PYRNA_FREE_SUPPORT
59 
60 /* use real collection iterators rather than faking with a list
61  * this is needed so enums can be iterated over without crashing,
62  * since finishing the iteration frees temp allocated enums */
63 #define USE_PYRNA_ITER
64 
65 /* --- end bpy build options --- */
66 
67 struct ID;
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 extern PyTypeObject pyrna_struct_meta_idprop_Type;
74 extern PyTypeObject pyrna_struct_Type;
75 extern PyTypeObject pyrna_prop_Type;
76 extern PyTypeObject pyrna_prop_array_Type;
77 extern PyTypeObject pyrna_prop_collection_Type;
78 extern PyTypeObject pyrna_func_Type;
79 
80 #define BPy_StructRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_struct_Type))
81 #define BPy_StructRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_struct_Type)
82 #define BPy_PropertyRNA_Check(v) (PyObject_TypeCheck(v, &pyrna_prop_Type))
83 #define BPy_PropertyRNA_CheckExact(v) (Py_TYPE(v) == &pyrna_prop_Type)
84 
85 #define PYRNA_STRUCT_CHECK_OBJ(obj) \
86  if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
87  return NULL; \
88  } \
89  (void)0
90 #define PYRNA_STRUCT_CHECK_INT(obj) \
91  if (UNLIKELY(pyrna_struct_validity_check(obj) == -1)) { \
92  return -1; \
93  } \
94  (void)0
95 
96 #define PYRNA_PROP_CHECK_OBJ(obj) \
97  if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
98  return NULL; \
99  } \
100  (void)0
101 #define PYRNA_PROP_CHECK_INT(obj) \
102  if (UNLIKELY(pyrna_prop_validity_check(obj) == -1)) { \
103  return -1; \
104  } \
105  (void)0
106 
107 #define PYRNA_STRUCT_IS_VALID(pysrna) (LIKELY(((BPy_StructRNA *)(pysrna))->ptr.type != NULL))
108 #define PYRNA_PROP_IS_VALID(pysrna) (LIKELY(((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL))
109 
110 /* 'in_weakreflist' MUST be aligned */
111 
112 typedef struct {
113  PyObject_HEAD /* required python macro */
114 #ifdef USE_WEAKREFS
115  PyObject *in_weakreflist;
116 #endif
119 
120 typedef struct {
121  PyObject_HEAD /* required python macro */
122 #ifdef USE_WEAKREFS
123  PyObject *in_weakreflist;
124 #endif
126 #ifdef USE_PYRNA_STRUCT_REFERENCE
127  /* generic PyObject we hold a reference to, example use:
128  * hold onto the collection iterator to prevent it from freeing allocated data we may use */
129  PyObject *reference;
130 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
131 
132 #ifdef PYRNA_FREE_SUPPORT
133  bool freeptr; /* needed in some cases if ptr.data is created on the fly, free when deallocing */
134 #endif /* PYRNA_FREE_SUPPORT */
135 } BPy_StructRNA;
136 
137 typedef struct {
138  PyObject_HEAD /* required python macro */
139 #ifdef USE_WEAKREFS
140  PyObject *in_weakreflist;
141 #endif
145 
146 typedef struct {
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] */
156  int arraydim;
160 
161 typedef struct {
162  PyObject_HEAD /* required python macro */
163 #ifdef USE_WEAKREFS
164  PyObject *in_weakreflist;
165 #endif
166 
167  /* collection iterator specific parts */
170 
171 typedef struct {
172  PyObject_HEAD /* required python macro */
173 #ifdef USE_WEAKREFS
174  PyObject *in_weakreflist;
175 #endif
179 
180 StructRNA *srna_from_self(PyObject *self, const char *error_prefix);
181 StructRNA *pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix);
182 
183 void BPY_rna_init(void);
184 PyObject *BPY_rna_module(void);
185 void BPY_update_rna_module(void);
186 /*PyObject *BPY_rna_doc(void);*/
187 PyObject *BPY_rna_types(void);
188 
191 
192 /* extern'd by other modules which don't deal closely with RNA */
193 PyObject *pyrna_id_CreatePyObject(struct ID *id);
194 bool pyrna_id_FromPyObject(PyObject *obj, struct ID **id);
195 bool pyrna_id_CheckPyObject(PyObject *obj);
196 
197 /* operators also need this to set args */
199  PyObject *kw,
200  const bool all_args,
201  const char *error_prefix);
202 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
203 
205  PyObject *value,
206  int type_size,
207  bool type_convert_sign,
208  int bitmap_size,
209  const char *error_prefix);
210 PyObject *pyrna_enum_bitfield_to_py(const struct EnumPropertyItem *items, int value);
212  PyObject *value,
213  int *r_value,
214  const char *error_prefix);
215 
217  const char *identifier,
218  int *value,
219  const char *error_prefix);
220 
221 int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class);
222 
223 void pyrna_struct_type_extend_capi(struct StructRNA *srna,
224  struct PyMethodDef *py_method,
225  struct PyGetSetDef *py_getset);
226 
227 /* called before stopping python */
228 void pyrna_alloc_types(void);
229 void pyrna_free_types(void);
230 
231 /* primitive type conversion */
233  PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix);
235  PropertyRNA *prop,
236  int arraydim,
237  int arrayoffset,
238  int index,
239  PyObject *py,
240  const char *error_prefix);
241 PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
242 
243 PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop);
245  PointerRNA *ptr,
246  PropertyRNA *prop,
247  int index);
249 int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value);
250 
251 bool pyrna_write_check(void);
252 void pyrna_write_set(bool val);
253 
257 
258 /* bpy.utils.(un)register_class */
259 extern PyMethodDef meth_bpy_register_class;
260 extern PyMethodDef meth_bpy_unregister_class;
261 
262 /* bpy.utils._bl_owner_(get/set) */
263 extern PyMethodDef meth_bpy_owner_id_set;
264 extern PyMethodDef meth_bpy_owner_id_get;
265 
267 
268 #ifdef __cplusplus
269 }
270 #endif
unsigned int uint
Definition: BLI_sys_types.h:83
PyObject * pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:7560
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:6456
PyTypeObject pyrna_prop_Type
Definition: bpy_rna.c:6633
void pyrna_invalidate(BPy_DummyPointerRNA *self)
Definition: bpy_rna.c:133
PyObject * pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:1537
void BPY_rna_init(void)
Definition: bpy_rna.c:7638
void pyrna_alloc_types(void)
Definition: bpy_rna.c:8743
int pyrna_struct_validity_check(BPy_StructRNA *pysrna)
Definition: bpy_rna.c:111
PyMethodDef meth_bpy_owner_id_set
Definition: bpy_rna.c:9200
bool pyrna_id_CheckPyObject(PyObject *obj)
Definition: bpy_rna.c:7633
bool pyrna_write_check(void)
Definition: bpy_rna.c:362
void pyrna_struct_type_extend_capi(struct StructRNA *srna, struct PyMethodDef *py_method, struct PyGetSetDef *py_getset)
Definition: bpy_rna.c:9121
bool pyrna_id_FromPyObject(PyObject *obj, struct ID **id)
Definition: bpy_rna.c:7622
int pyrna_enum_value_from_id(const EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix)
Definition: bpy_rna.c:804
PyMethodDef meth_bpy_owner_id_get
Definition: bpy_rna.c:9194
PyObject * pyrna_struct_CreatePyObject(PointerRNA *ptr)
Definition: bpy_rna.c:7469
int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value)
StructRNA * pyrna_struct_as_srna(PyObject *self, const bool parent, const char *error_prefix)
Definition: bpy_rna.c:7849
int pyrna_set_to_enum_bitfield(const struct EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix)
void pyrna_free_types(void)
Definition: bpy_rna.c:8775
void BPY_update_rna_module(void)
Definition: bpy_rna.c:7701
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:7688
PyObject * pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index)
PyTypeObject pyrna_prop_array_Type
Definition: bpy_rna.c:6717
PyObject * pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop)
Definition: bpy_rna.c:644
StructRNA * srna_from_self(PyObject *self, const char *error_prefix)
Definition: bpy_rna.c:7906
PyTypeObject pyrna_struct_Type
Definition: bpy_rna.c:6540
int pyrna_deferred_register_class(struct StructRNA *srna, PyTypeObject *py_class)
Definition: bpy_rna.c:8122
PyTypeObject pyrna_func_Type
Definition: bpy_rna.c:6972
int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, const bool all_args, const char *error_prefix)
Definition: bpy_rna.c:1622
PyMethodDef meth_bpy_unregister_class
Definition: bpy_rna.c:9001
PyObject * pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop)
void pyrna_write_set(bool val)
Definition: bpy_rna.c:367
PyObject * pyrna_id_CreatePyObject(struct ID *id)
Definition: bpy_rna.c:7611
PyMethodDef meth_bpy_register_class
Definition: bpy_rna.c:8827
int pyrna_prop_validity_check(BPy_PropertyRNA *self)
Definition: bpy_rna.c:121
PyObject * pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index)
uint * pyrna_set_to_enum_bitmap(const struct EnumPropertyItem *items, PyObject *value, int type_size, bool type_convert_sign, int bitmap_size, const char *error_prefix)
PyObject * pyrna_enum_bitfield_to_py(const struct EnumPropertyItem *items, int value)
PyObject * BPY_rna_types(void)
Definition: bpy_rna.c:7819
BPy_StructRNA * bpy_context_module
Definition: bpy_rna.c:97
PyTypeObject pyrna_prop_collection_Type
Definition: bpy_rna.c:6800
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:117
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:176
FunctionRNA * func
Definition: bpy_rna.h:177
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:151
PropertyRNA * prop
Definition: bpy_rna.h:152
PyObject_HEAD CollectionPropertyIterator iter
Definition: bpy_rna.h:168
PropertyRNA * prop
Definition: bpy_rna.h:143
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:142
PyObject_HEAD PointerRNA ptr
Definition: bpy_rna.h:125
Definition: DNA_ID.h:273
PointerRNA * ptr
Definition: wm_files.c:3157