|
Blender
V2.59
|
00001 /* 00002 * $Id: bpy_app_handlers.c 37799 2011-06-24 23:14:26Z gsrb3d $ 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 #include <Python.h> 00030 #include "BLI_utildefines.h" 00031 #include "BLI_callbacks.h" 00032 00033 #include "RNA_types.h" 00034 #include "RNA_access.h" 00035 #include "bpy_rna.h" 00036 #include "bpy_app_handlers.h" 00037 00038 void bpy_app_generic_callback(struct Main *main, struct ID *id, void *arg); 00039 00040 static PyTypeObject BlenderAppCbType; 00041 00042 static PyStructSequence_Field app_cb_info_fields[]= { 00043 {(char *)"render_pre", NULL}, 00044 {(char *)"render_post", NULL}, 00045 {(char *)"load_pre", NULL}, 00046 {(char *)"load_post", NULL}, 00047 {(char *)"save_pre", NULL}, 00048 {(char *)"save_post", NULL}, 00049 {NULL} 00050 }; 00051 00052 static PyStructSequence_Desc app_cb_info_desc= { 00053 (char *)"bpy.app.handlers", /* name */ 00054 (char *)"This module contains callbacks", /* doc */ 00055 app_cb_info_fields, /* fields */ 00056 (sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field)) - 1 00057 }; 00058 00059 /* 00060 #if (BLI_CB_EVT_TOT != ((sizeof(app_cb_info_fields)/sizeof(PyStructSequence_Field)))) 00061 # error "Callbacks are out of sync" 00062 #endif 00063 */ 00064 00065 static PyObject *py_cb_array[BLI_CB_EVT_TOT]= {0}; 00066 00067 static PyObject *make_app_cb_info(void) 00068 { 00069 PyObject *app_cb_info; 00070 int pos= 0; 00071 00072 app_cb_info= PyStructSequence_New(&BlenderAppCbType); 00073 if (app_cb_info == NULL) { 00074 return NULL; 00075 } 00076 00077 for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) { 00078 if(app_cb_info_fields[pos].name == NULL) { 00079 Py_FatalError("invalid callback slots 1"); 00080 } 00081 PyStructSequence_SET_ITEM(app_cb_info, pos, (py_cb_array[pos]= PyList_New(0))); 00082 } 00083 if(app_cb_info_fields[pos].name != NULL) { 00084 Py_FatalError("invalid callback slots 2"); 00085 } 00086 00087 return app_cb_info; 00088 } 00089 00090 PyObject *BPY_app_handlers_struct(void) 00091 { 00092 PyObject *ret; 00093 00094 PyStructSequence_InitType(&BlenderAppCbType, &app_cb_info_desc); 00095 00096 ret= make_app_cb_info(); 00097 00098 /* prevent user from creating new instances */ 00099 BlenderAppCbType.tp_init= NULL; 00100 BlenderAppCbType.tp_new= NULL; 00101 00102 /* assign the C callbacks */ 00103 if(ret) { 00104 static bCallbackFuncStore funcstore_array[BLI_CB_EVT_TOT]= {{0}}; 00105 bCallbackFuncStore *funcstore; 00106 int pos= 0; 00107 00108 for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) { 00109 funcstore= &funcstore_array[pos]; 00110 funcstore->func= bpy_app_generic_callback; 00111 funcstore->alloc= 0; 00112 funcstore->arg= SET_INT_IN_POINTER(pos); 00113 BLI_add_cb(funcstore, pos); 00114 } 00115 } 00116 00117 return ret; 00118 } 00119 00120 void BPY_app_handlers_reset(void) 00121 { 00122 int pos= 0; 00123 00124 for(pos= 0; pos < BLI_CB_EVT_TOT; pos++) { 00125 PyList_SetSlice(py_cb_array[pos], 0, PY_SSIZE_T_MAX, NULL); 00126 } 00127 } 00128 00129 /* the actual callback - not necessarily called from py */ 00130 void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg) 00131 { 00132 PyObject *cb_list= py_cb_array[GET_INT_FROM_POINTER(arg)]; 00133 Py_ssize_t cb_list_len; 00134 if((cb_list_len= PyList_GET_SIZE(cb_list)) > 0) { 00135 PyGILState_STATE gilstate= PyGILState_Ensure(); 00136 00137 PyObject* args= PyTuple_New(1); // save python creating each call 00138 PyObject* func; 00139 PyObject* ret; 00140 Py_ssize_t pos; 00141 00142 /* setup arguments */ 00143 if(id) { 00144 PointerRNA id_ptr; 00145 RNA_id_pointer_create(id, &id_ptr); 00146 PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr)); 00147 } 00148 else { 00149 PyTuple_SET_ITEM(args, 0, Py_None); 00150 Py_INCREF(Py_None); 00151 } 00152 00153 // Iterate the list and run the callbacks 00154 for (pos=0; pos < cb_list_len; pos++) { 00155 func= PyList_GET_ITEM(cb_list, pos); 00156 ret= PyObject_Call(func, args, NULL); 00157 if (ret==NULL) { 00158 PyErr_Print(); 00159 PyErr_Clear(); 00160 } 00161 else { 00162 Py_DECREF(ret); 00163 } 00164 } 00165 00166 Py_DECREF(args); 00167 00168 PyGILState_Release(gilstate); 00169 } 00170 }