Blender  V2.59
bpy_rna.h
Go to the documentation of this file.
00001 /*
00002  * $Id: bpy_rna.h 39075 2011-08-05 16:29:38Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Campbell Barton
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00029 #ifndef BPY_RNA_H
00030 #define BPY_RNA_H
00031 
00032 /* --- bpy build options --- */
00033 #ifdef WITH_PYTHON_SAFETY
00034 
00035 /* play it safe and keep optional for now, need to test further now this affects looping on 10000's of verts for eg. */
00036 #define USE_WEAKREFS
00037 
00038 /* method to invalidate removed py data, XXX, slow to remove objects, otherwise no overhead */
00039 /* #define USE_PYRNA_INVALIDATE_GC */
00040 
00041 /* different method */
00042 #define USE_PYRNA_INVALIDATE_WEAKREF
00043 
00044 /* support for inter references, currently only needed for corner case */
00045 #define USE_PYRNA_STRUCT_REFERENCE
00046 
00047 /* use real collection iterators rather than faking with a list */
00048 #define USE_PYRNA_ITER
00049 
00050 #else /* WITH_PYTHON_SAFETY */
00051 
00052  /* default, no defines! */
00053 
00054 #endif /* !WITH_PYTHON_SAFETY */
00055 
00056 
00057 /* sanity checks on above defs */
00058 #if defined(USE_PYRNA_INVALIDATE_WEAKREF) && !defined(USE_WEAKREFS)
00059 #define USE_WEAKREFS
00060 #endif
00061 
00062 #if defined(USE_PYRNA_INVALIDATE_GC) && defined(USE_PYRNA_INVALIDATE_WEAKREF)
00063 #error "Only 1 reference check method at a time!"
00064 #endif
00065 
00066 /* only used by operator introspection get_rna(), this is only used for doc gen
00067  * so prefer the leak to the memory bloat for now. */
00068 // #define PYRNA_FREE_SUPPORT
00069 
00070 /* --- end bpy build options --- */
00071 
00072 struct ID;
00073 
00074 extern PyTypeObject pyrna_struct_meta_idprop_Type;
00075 extern PyTypeObject pyrna_struct_Type;
00076 extern PyTypeObject pyrna_prop_Type;
00077 extern PyTypeObject pyrna_prop_array_Type;
00078 extern PyTypeObject pyrna_prop_collection_Type;
00079 extern PyTypeObject pyrna_func_Type;
00080 
00081 #define BPy_StructRNA_Check(v)                  (PyObject_TypeCheck(v, &pyrna_struct_Type))
00082 #define BPy_StructRNA_CheckExact(v)             (Py_TYPE(v) == &pyrna_struct_Type)
00083 #define BPy_PropertyRNA_Check(v)                (PyObject_TypeCheck(v, &pyrna_prop_Type))
00084 #define BPy_PropertyRNA_CheckExact(v)   (Py_TYPE(v) == &pyrna_prop_Type)
00085 
00086 #define PYRNA_STRUCT_CHECK_OBJ(obj) if(pyrna_struct_validity_check(obj) == -1) { return NULL; }
00087 #define PYRNA_STRUCT_CHECK_INT(obj) if(pyrna_struct_validity_check(obj) == -1) { return -1; }
00088 
00089 #define PYRNA_PROP_CHECK_OBJ(obj) if(pyrna_prop_validity_check(obj) == -1) { return NULL; }
00090 #define PYRNA_PROP_CHECK_INT(obj) if(pyrna_prop_validity_check(obj) == -1) { return -1; }
00091 
00092 #define PYRNA_STRUCT_IS_VALID(pysrna) (((BPy_StructRNA *)(pysrna))->ptr.type != NULL)
00093 #define PYRNA_PROP_IS_VALID(pysrna) (((BPy_PropertyRNA *)(pysrna))->ptr.type != NULL)
00094 
00095 /* 'in_weakreflist' MUST be aligned */
00096 
00097 typedef struct {
00098         PyObject_HEAD /* required python macro   */
00099 #ifdef USE_WEAKREFS
00100         PyObject *in_weakreflist;
00101 #endif
00102         PointerRNA      ptr;
00103 } BPy_DummyPointerRNA;
00104 
00105 typedef struct {
00106         PyObject_HEAD /* required python macro   */
00107 #ifdef USE_WEAKREFS
00108         PyObject *in_weakreflist;
00109 #endif
00110         PointerRNA ptr;
00111 #ifdef USE_PYRNA_STRUCT_REFERENCE
00112         /* generic PyObject we hold a reference to, example use:
00113          * hold onto the collection iterator to prevent it from freeing allocated data we may use */
00114         PyObject *reference;
00115 #endif /* !USE_PYRNA_STRUCT_REFERENCE */
00116 
00117 #ifdef PYRNA_FREE_SUPPORT
00118         int freeptr; /* needed in some cases if ptr.data is created on the fly, free when deallocing */
00119 #endif /* PYRNA_FREE_SUPPORT */
00120 } BPy_StructRNA;
00121 
00122 typedef struct {
00123         PyObject_HEAD /* required python macro   */
00124 #ifdef USE_WEAKREFS
00125         PyObject *in_weakreflist;
00126 #endif
00127         PointerRNA ptr;
00128         PropertyRNA *prop;
00129 } BPy_PropertyRNA;
00130 
00131 typedef struct {
00132         PyObject_HEAD /* required python macro   */
00133 #ifdef USE_WEAKREFS
00134         PyObject *in_weakreflist;
00135 #endif
00136         PointerRNA ptr;
00137         PropertyRNA *prop;
00138 
00139         /* Arystan: this is a hack to allow sub-item r/w access like: face.uv[n][m] */
00140         int arraydim; /* array dimension, e.g: 0 for face.uv, 2 for face.uv[n][m], etc. */
00141         int arrayoffset; /* array first item offset, e.g. if face.uv is [4][2], arrayoffset for face.uv[n] is 2n */
00142 } BPy_PropertyArrayRNA;
00143 
00144 typedef struct {
00145         PyObject_HEAD /* required python macro   */
00146 #ifdef USE_WEAKREFS
00147         PyObject *in_weakreflist;
00148 #endif
00149 
00150         /* collection iterator spesific parts */
00151         CollectionPropertyIterator iter;
00152 } BPy_PropertyCollectionIterRNA;
00153 
00154 typedef struct {
00155         PyObject_HEAD /* required python macro   */
00156 #ifdef USE_WEAKREFS
00157         PyObject *in_weakreflist;
00158 #endif
00159         PointerRNA ptr;
00160         FunctionRNA *func;
00161 } BPy_FunctionRNA;
00162 
00163 /* cheap trick */
00164 #define BPy_BaseTypeRNA BPy_PropertyRNA
00165 
00166 StructRNA *srna_from_self(PyObject *self, const char *error_prefix);
00167 StructRNA *pyrna_struct_as_srna(PyObject *self, int parent, const char *error_prefix);
00168 
00169 void      BPY_rna_init(void);
00170 PyObject *BPY_rna_module(void);
00171 void      BPY_update_rna_module(void);
00172 /*PyObject *BPY_rna_doc(void);*/
00173 PyObject *BPY_rna_types(void);
00174 
00175 PyObject *pyrna_struct_CreatePyObject(PointerRNA *ptr);
00176 PyObject *pyrna_prop_CreatePyObject(PointerRNA *ptr, PropertyRNA *prop);
00177 
00178 /* operators also need this to set args */
00179 int pyrna_pydict_to_props(PointerRNA *ptr, PyObject *kw, int all_args, const char *error_prefix);
00180 PyObject *pyrna_prop_to_py(PointerRNA *ptr, PropertyRNA *prop);
00181 
00182 PyObject *pyrna_enum_bitfield_to_py(struct EnumPropertyItem *items, int value);
00183 int pyrna_set_to_enum_bitfield(EnumPropertyItem *items, PyObject *value, int *r_value, const char *error_prefix);
00184 
00185 int pyrna_enum_value_from_id(EnumPropertyItem *item, const char *identifier, int *value, const char *error_prefix);
00186 
00187 int pyrna_deferred_register_class(struct StructRNA *srna, PyObject *py_class);
00188 
00189 /* called before stopping python */
00190 void pyrna_alloc_types(void);
00191 void pyrna_free_types(void);
00192 
00193 /* primitive type conversion */
00194 int pyrna_py_to_array(PointerRNA *ptr, PropertyRNA *prop, char *param_data, PyObject *py, const char *error_prefix);
00195 int pyrna_py_to_array_index(PointerRNA *ptr, PropertyRNA *prop, int arraydim, int arrayoffset, int index, PyObject *py, const char *error_prefix);
00196 PyObject *pyrna_array_index(PointerRNA *ptr, PropertyRNA *prop, int index);
00197 
00198 PyObject *pyrna_py_from_array(PointerRNA *ptr, PropertyRNA *prop);
00199 PyObject *pyrna_py_from_array_index(BPy_PropertyArrayRNA *self, PointerRNA *ptr, PropertyRNA *prop, int index);
00200 PyObject *pyrna_math_object_from_array(PointerRNA *ptr, PropertyRNA *prop);
00201 int pyrna_array_contains_py(PointerRNA *ptr, PropertyRNA *prop, PyObject *value);
00202 
00203 int pyrna_write_check(void);
00204 void pyrna_write_set(int val);
00205 
00206 int pyrna_struct_validity_check(BPy_StructRNA *pysrna);
00207 int pyrna_prop_validity_check(BPy_PropertyRNA *self);
00208 
00209 /* bpy.utils.(un)register_class */
00210 extern PyMethodDef meth_bpy_register_class;
00211 extern PyMethodDef meth_bpy_unregister_class;
00212 
00213 #endif