Exemplo n.º 1
0
static PyObject *bpy_lib_enter(BPy_Library *self)
{
	PyObject *ret;
	BPy_Library *self_from;
	PyObject *from_dict = _PyDict_NewPresized(MAX_LIBARRAY);
	ReportList reports;

	BKE_reports_init(&reports, RPT_STORE);

	self->blo_handle = BLO_blendhandle_from_file(self->abspath, &reports);

	if (self->blo_handle == NULL) {
		if (BPy_reports_to_error(&reports, PyExc_IOError, true) != -1) {
			PyErr_Format(PyExc_IOError,
			             "load: %s failed to open blend file",
			             self->abspath);
		}
		return NULL;
	}
	else {
		int i = 0, code;
		while ((code = BKE_idcode_iter_step(&i))) {
			if (BKE_idcode_is_linkable(code)) {
				const char *name_plural = BKE_idcode_to_name_plural(code);
				PyObject *str = PyUnicode_FromString(name_plural);
				PyObject *item;

				PyDict_SetItem(self->dict, str, item = PyList_New(0));
				Py_DECREF(item);
				PyDict_SetItem(from_dict, str, item = _bpy_names(self, code));
				Py_DECREF(item);

				Py_DECREF(str);
			}
		}
	}

	/* create a dummy */
	self_from = PyObject_New(BPy_Library, &bpy_lib_Type);
	BLI_strncpy(self_from->relpath, self->relpath, sizeof(self_from->relpath));
	BLI_strncpy(self_from->abspath, self->abspath, sizeof(self_from->abspath));

	self_from->blo_handle = NULL;
	self_from->flag = 0;
	self_from->dict = from_dict; /* owns the dict */

	/* return pair */
	ret = PyTuple_New(2);
	PyTuple_SET_ITEMS(ret,
	        (PyObject *)self_from,
	        (PyObject *)self);
	Py_INCREF(self);

	BKE_reports_clear(&reports);

	return ret;
}
Exemplo n.º 2
0
static PyObject *bpy_lib_exit(BPy_Library *self, PyObject *UNUSED(args))
{
	Main *bmain = CTX_data_main(BPy_GetContext());
	Main *mainl = NULL;
	int err = 0;

	flag_all_listbases_ids(LIB_PRE_EXISTING, 1);

	/* here appending/linking starts */
	mainl = BLO_library_append_begin(bmain, &(self->blo_handle), self->relpath);

	{
		int i = 0, code;
		while ((code = BKE_idcode_iter_step(&i))) {
			if (BKE_idcode_is_linkable(code)) {
				const char *name_plural = BKE_idcode_to_name_plural(code);
				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_append_named_part(mainl, &(self->blo_handle), item_str, code);
							if (id) {
#ifdef USE_RNA_DATABLOCKS
								PointerRNA id_ptr;
								RNA_id_pointer_create(id, &id_ptr);
								Py_DECREF(item);
								item = pyrna_struct_CreatePyObject(&id_ptr);
#endif
							}
							else {
								bpy_lib_exit_warn_idname(self, name_plural, item_str);
								/* just warn for now */
								/* err = -1; */
#ifdef USE_RNA_DATABLOCKS
								item = Py_None;
								Py_INCREF(item);
#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_None;
							Py_INCREF(item);
#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;
		flag_all_listbases_ids(LIB_PRE_EXISTING, 0);
		return NULL;
	}
	else {
		Library *lib = mainl->curlib; /* newly added lib, assign before append end */
		BLO_library_append_end(NULL, mainl, &(self->blo_handle), 0, self->flag);
		BLO_blendhandle_close(self->blo_handle);
		self->blo_handle = NULL;

		{	/* copied from wm_operator.c */
			/* mark all library linked objects to be updated */
			recalc_all_library_objects(G.main);

			/* append, rather than linking */
			if ((self->flag & FILE_LINK) == 0) {
				BKE_library_make_local(bmain, lib, 1);
			}
		}

		flag_all_listbases_ids(LIB_PRE_EXISTING, 0);

		Py_RETURN_NONE;
	}
}
Exemplo n.º 3
0
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;
	}
}