static PyObject *BPy_IDGroup_pop(BPy_IDProperty *self, PyObject *args) { IDProperty *idprop; PyObject *pyform; char *key; PyObject *def = NULL; if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) { return NULL; } idprop = IDP_GetPropertyFromGroup(self->prop, key); if (idprop == NULL) { if (def == NULL) { PyErr_SetString(PyExc_KeyError, "item not in group"); return NULL; } return Py_INCREF_RET(def); } pyform = BPy_IDGroup_MapDataToPy(idprop); if (pyform == NULL) { /* ok something bad happened with the pyobject, * so don't remove the prop from the group. if pyform is * NULL, then it already should have raised an exception.*/ return NULL; } IDP_RemoveFromGroup(self->prop, idprop); return pyform; }
/* pgettext helper. */ static PyObject *_py_pgettext(PyObject *args, PyObject *kw, const char *(*_pgettext)(const char *, const char *)) { static const char *kwlist[] = {"msgid", "msgctxt", NULL}; #ifdef WITH_INTERNATIONAL char *msgid, *msgctxt = NULL; if (!PyArg_ParseTupleAndKeywords(args, kw, "s|z:bpy.app.translations.pgettext", (char **)kwlist, &msgid, &msgctxt)) { return NULL; } return PyUnicode_FromString((*_pgettext)(msgctxt ? msgctxt : BLT_I18NCONTEXT_DEFAULT, msgid)); #else PyObject *msgid, *msgctxt; (void)_pgettext; if (!PyArg_ParseTupleAndKeywords(args, kw, "O|O:bpy.app.translations.pgettext", (char **)kwlist, &msgid, &msgctxt)) { return NULL; } return Py_INCREF_RET(msgid); #endif }
PyObject *BaseMathObject_freeze(BaseMathObject *self) { if (self->flag & BASE_MATH_FLAG_IS_WRAP) { PyErr_SetString(PyExc_TypeError, "Cannot freeze wrapped data"); return NULL; } self->flag |= BASE_MATH_FLAG_IS_FROZEN; return Py_INCREF_RET((PyObject *)self); }
/* utility function */ static void BPy_IDGroup_CorrectListLen(IDProperty *prop, PyObject *seq, int len, const char *func) { int j; printf("%s: ID Property Error found and corrected!\n", func); /* fill rest of list with valid references to None */ for (j = len; j < prop->len; j++) { PyList_SET_ITEM(seq, j, Py_INCREF_RET(Py_None)); } /*set correct group length*/ prop->len = len; }
/* the actual callback - not necessarily called from py */ void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg) { PyObject *cb_list = py_cb_array[GET_INT_FROM_POINTER(arg)]; if (PyList_GET_SIZE(cb_list) > 0) { PyGILState_STATE gilstate = PyGILState_Ensure(); PyObject *args = PyTuple_New(1); /* save python creating each call */ PyObject *func; PyObject *ret; Py_ssize_t pos; /* setup arguments */ if (id) { PointerRNA id_ptr; RNA_id_pointer_create(id, &id_ptr); PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr)); } else { PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None)); } /* Iterate the list and run the callbacks * note: don't store the list size since the scripts may remove themselves */ for (pos = 0; pos < PyList_GET_SIZE(cb_list); pos++) { func = PyList_GET_ITEM(cb_list, pos); ret = PyObject_Call(func, args, NULL); if (ret == NULL) { /* Don't set last system variables because they might cause some * dangling pointers to external render engines (when exception * happens during rendering) which will break logic of render pipeline * which expects to be the only user of render engine when rendering * is finished. */ PyErr_PrintEx(0); PyErr_Clear(); } else { Py_DECREF(ret); } } Py_DECREF(args); PyGILState_Release(gilstate); } }
static PyObject *bpy_bmdeformvert_get(BPy_BMDeformVert *self, PyObject *args) { int key; PyObject *def = Py_None; if (!PyArg_ParseTuple(args, "i|O:get", &key, &def)) { return NULL; } else { MDeformWeight *dw = defvert_find_index(self->data, key); if (dw) { return PyFloat_FromDouble(dw->weight); } else { return Py_INCREF_RET(def); } } }
/* the actual callback - not necessarily called from py */ void bpy_app_generic_callback(struct Main *UNUSED(main), struct ID *id, void *arg) { PyObject *cb_list = py_cb_array[GET_INT_FROM_POINTER(arg)]; if (PyList_GET_SIZE(cb_list) > 0) { PyGILState_STATE gilstate = PyGILState_Ensure(); PyObject *args = PyTuple_New(1); /* save python creating each call */ PyObject *func; PyObject *ret; Py_ssize_t pos; /* setup arguments */ if (id) { PointerRNA id_ptr; RNA_id_pointer_create(id, &id_ptr); PyTuple_SET_ITEM(args, 0, pyrna_struct_CreatePyObject(&id_ptr)); } else { PyTuple_SET_ITEM(args, 0, Py_INCREF_RET(Py_None)); } /* Iterate the list and run the callbacks * note: don't store the list size since the scripts may remove themselves */ for (pos = 0; pos < PyList_GET_SIZE(cb_list); pos++) { func = PyList_GET_ITEM(cb_list, pos); ret = PyObject_Call(func, args, NULL); if (ret == NULL) { PyErr_Print(); PyErr_Clear(); } else { Py_DECREF(ret); } } Py_DECREF(args); PyGILState_Release(gilstate); } }
static PyObject *bpy_bmlayercollection_get(BPy_BMLayerCollection *self, PyObject *args) { const char *key; PyObject *def = Py_None; BPY_BM_CHECK_OBJ(self); if (!PyArg_ParseTuple(args, "s|O:get", &key, &def)) { return NULL; } else { CustomData *data; int index; data = bpy_bm_customdata_get(self->bm, self->htype); index = CustomData_get_named_layer(data, self->type, key); /* type relative */ if (index != -1) { return BPy_BMLayerItem_CreatePyObject(self->bm, self->htype, self->type, index); } } return Py_INCREF_RET(def); }
static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args) { wmOperatorType *ot; const char *opname; PyObject *context_dict = NULL; /* optional args */ PyObject *context_dict_back; const char *context_str = NULL; PyObject *ret; int context = WM_OP_EXEC_DEFAULT; /* XXX Todo, work out a better solution for passing on context, * could make a tuple from self and pack the name and Context into it... */ bContext *C = (bContext *)BPy_GetContext(); if (C == NULL) { PyErr_SetString(PyExc_RuntimeError, "Context is None, cant poll any operators"); return NULL; } if (!PyArg_ParseTuple(args, "s|Os:_bpy.ops.poll", &opname, &context_dict, &context_str)) return NULL; ot = WM_operatortype_find(opname, true); if (ot == NULL) { PyErr_Format(PyExc_AttributeError, "Polling operator \"bpy.ops.%s\" error, " "could not be found", opname); return NULL; } if (context_str) { if (RNA_enum_value_from_id(rna_enum_operator_context_items, context_str, &context) == 0) { char *enum_str = BPy_enum_as_string(rna_enum_operator_context_items); PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s.poll\" error, " "expected a string enum in (%s)", opname, enum_str); MEM_freeN(enum_str); return NULL; } } if (context_dict == NULL || context_dict == Py_None) { context_dict = NULL; } else if (!PyDict_Check(context_dict)) { PyErr_Format(PyExc_TypeError, "Calling operator \"bpy.ops.%s.poll\" error, " "custom context expected a dict or None, got a %.200s", opname, Py_TYPE(context_dict)->tp_name); return NULL; } context_dict_back = CTX_py_dict_get(C); CTX_py_dict_set(C, (void *)context_dict); Py_XINCREF(context_dict); /* so we done loose it */ /* main purpose of thsi function */ ret = WM_operator_poll_context((bContext *)C, ot, context) ? Py_True : Py_False; /* restore with original context dict, probably NULL but need this for nested operator calls */ Py_XDECREF(context_dict); CTX_py_dict_set(C, (void *)context_dict_back); return Py_INCREF_RET(ret); }
static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args)) { Main *bmain = CTX_data_main(BPy_GetContext()); Main *mainl = NULL; int err = 0; BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, true); /* here appending/linking starts */ mainl = BLO_library_link_begin(bmain, &(self->blo_handle), self->relpath); { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); // printf("lib: %s\n", name_plural); if (ls && PyList_Check(ls)) { /* loop */ Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; PyObject *item; const char *item_str; for (i = 0; i < size; i++) { item = PyList_GET_ITEM(ls, i); item_str = _PyUnicode_AsString(item); // printf(" %s\n", item_str); if (item_str) { ID *id = BLO_library_link_named_part(mainl, &(self->blo_handle), idcode, item_str); if (id) { #ifdef USE_RNA_DATABLOCKS /* swap name for pointer to the id */ Py_DECREF(item); item = PyCapsule_New((void *)id, NULL, NULL); #endif } else { bpy_lib_exit_warn_idname(self, name_plural, item_str); /* just warn for now */ /* err = -1; */ #ifdef USE_RNA_DATABLOCKS item = Py_INCREF_RET(Py_None); #endif } /* ID or None */ } else { /* XXX, could complain about this */ bpy_lib_exit_warn_type(self, item); PyErr_Clear(); #ifdef USE_RNA_DATABLOCKS item = Py_INCREF_RET(Py_None); #endif } #ifdef USE_RNA_DATABLOCKS PyList_SET_ITEM(ls, i, item); #endif } } } } } if (err == -1) { /* exception raised above, XXX, this leaks some memory */ BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false); return NULL; } else { Library *lib = mainl->curlib; /* newly added lib, assign before append end */ BLO_library_link_end(mainl, &(self->blo_handle), self->flag, NULL, NULL); BLO_blendhandle_close(self->blo_handle); self->blo_handle = NULL; /* copied from wm_operator.c */ { /* mark all library linked objects to be updated */ BKE_main_lib_objects_recalc_all(G.main); /* append, rather than linking */ if ((self->flag & FILE_LINK) == 0) { BKE_library_make_local(bmain, lib, true); } } BKE_main_id_flag_all(bmain, LIB_TAG_PRE_EXISTING, false); /* finally swap the capsules for real bpy objects * important since BLO_library_append_end initializes NodeTree types used by srna->refine */ { int idcode_step = 0, idcode; while ((idcode = BKE_idcode_iter_step(&idcode_step))) { if (BKE_idcode_is_linkable(idcode)) { const char *name_plural = BKE_idcode_to_name_plural(idcode); PyObject *ls = PyDict_GetItemString(self->dict, name_plural); if (ls && PyList_Check(ls)) { Py_ssize_t size = PyList_GET_SIZE(ls); Py_ssize_t i; PyObject *item; for (i = 0; i < size; i++) { item = PyList_GET_ITEM(ls, i); if (PyCapsule_CheckExact(item)) { PointerRNA id_ptr; ID *id; id = PyCapsule_GetPointer(item, NULL); Py_DECREF(item); RNA_id_pointer_create(id, &id_ptr); item = pyrna_struct_CreatePyObject(&id_ptr); PyList_SET_ITEM(ls, i, item); } } } } } } Py_RETURN_NONE; } }
PyObject *BaseMathObject_owner_get(BaseMathObject *self, void *UNUSED(closure)) { PyObject *ret = self->cb_user ? self->cb_user : Py_None; return Py_INCREF_RET(ret); }