Example #1
0
static BlendFileData *load_game_data(const char *progname, char *filename = NULL, char *relativename = NULL)
{
    ReportList reports;
    BlendFileData *bfd = NULL;

    BKE_reports_init(&reports, RPT_STORE);

    /* try to load ourself, will only work if we are a runtime */
    if (BLO_is_a_runtime(progname)) {
        bfd= BLO_read_runtime(progname, &reports);
        if (bfd) {
            bfd->type= BLENFILETYPE_RUNTIME;
            BLI_strncpy(bfd->main->name, progname, sizeof(bfd->main->name));
        }
    } else {
        bfd= BLO_read_from_file(progname, &reports);
    }

    if (!bfd && filename) {
        bfd = load_game_data(filename);
        if (!bfd) {
            printf("Loading %s failed: ", filename);
            BKE_reports_print(&reports, RPT_ERROR);
        }
    }

    BKE_reports_clear(&reports);

    return bfd;
}
Example #2
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;
}
Example #3
0
static BlendFileData *load_game_data(char *filename)
{
	ReportList reports;
	BlendFileData *bfd;
	
	BKE_reports_init(&reports, RPT_STORE);
	bfd= BLO_read_from_file(filename, &reports);

	if (!bfd) {
		printf("Loading %s failed: ", filename);
		BKE_reports_print(&reports, RPT_ERROR);
	}

	BKE_reports_clear(&reports);

	return bfd;
}
Example #4
0
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
{
	char *report_str;

	report_str = BKE_reports_string(reports, RPT_ERROR);

	if (clear == true) {
		BKE_reports_clear(reports);
	}

	if (report_str) {
		PyErr_SetString(exception, report_str);
		MEM_freeN(report_str);
	}

	return (report_str == NULL) ? 0 : -1;
}
Example #5
0
bool GPC_Engine::Start(char *filename)
{
	ReportList reports;
	BlendFileData *bfd;
	
	BKE_reports_init(&reports, RPT_STORE);
	bfd= BLO_read_from_file(filename, &reports);
	BKE_reports_clear(&reports);

	if (!bfd) {
			// XXX, deal with error here
		cout << "Unable to load: " << filename << endl;
		return false;
	}

	StartKetsji();

	if(bfd->type == BLENFILETYPE_PUB)
		m_canvas->SetBannerDisplayEnabled(false);

	return true;
}
Example #6
0
bool GPC_Engine::Start(unsigned char *blenderDataBuffer,
		unsigned int blenderDataBufferSize)
{
	ReportList reports;
	BlendFileData *bfd;
	
	BKE_reports_init(&reports, RPT_STORE);
	bfd= BLO_read_from_memory(blenderDataBuffer, blenderDataBufferSize, &reports);
	BKE_reports_clear(&reports);

	if (!bfd) {
			// XXX, deal with error here
		cout << "Unable to load. " << endl;
		return false;
	}
	
	StartKetsji();

	if(bfd->type == BLENFILETYPE_PUB)
		m_canvas->SetBannerDisplayEnabled(false);

	return true;
}
Example #7
0
File: wm.c Project: mik0001/Blender
void WM_operator_free(wmOperator *op)
{

#ifdef WITH_PYTHON
	if(op->py_instance) {
		/* do this first incase there are any __del__ functions or
		 * similar that use properties */
		BPY_DECREF(op->py_instance);
	}
#endif

	if(op->ptr) {
		op->properties= op->ptr->data;
		MEM_freeN(op->ptr);
	}

	if(op->properties) {
		IDP_FreeProperty(op->properties);
		MEM_freeN(op->properties);
	}

	if(op->reports && (op->reports->flag & RPT_FREE)) {
		BKE_reports_clear(op->reports);
		MEM_freeN(op->reports);
	}

	if(op->macro.first) {
		wmOperator *opm, *opmnext;
		for(opm= op->macro.first; opm; opm= opmnext) {
			opmnext = opm->next;
			WM_operator_free(opm);
		}
	}
	
	MEM_freeN(op);
}
Example #8
0
static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
{
	static const char *kwlist[] = {
		"filepath", "datablocks",
		/* optional */
		"relative_remap", "fake_user", "compress",
		NULL,
	};

	/* args */
	const char *filepath;
	char filepath_abs[FILE_MAX];
	PyObject *datablocks = NULL;
	bool use_relative_remap = false, use_fake_user = false, use_compress = false;

	if (!PyArg_ParseTupleAndKeywords(
	        args, kwds,
	        "sO!|$O&O&O&:write", (char **)kwlist,
	        &filepath,
	        &PySet_Type, &datablocks,
	        PyC_ParseBool, &use_relative_remap,
	        PyC_ParseBool, &use_fake_user,
	        PyC_ParseBool, &use_compress))
	{
		return NULL;
	}

	Main *bmain_src = G.main;
	int write_flags = 0;

	if (use_relative_remap) {
		write_flags |= G_FILE_RELATIVE_REMAP;
	}

	if (use_compress) {
		write_flags |= G_FILE_COMPRESS;
	}

	BLI_strncpy(filepath_abs, filepath, FILE_MAX);
	BLI_path_abs(filepath_abs, G.main->name);

	BKE_blendfile_write_partial_begin(bmain_src);

	/* array of ID's and backup any data we modify */
	struct {
		ID *id;
		/* original values */
		short id_flag;
		short id_us;
	} *id_store_array, *id_store;
	int id_store_len = 0;

	PyObject *ret;

	/* collect all id data from the set and store in 'id_store_array' */
	{
		Py_ssize_t pos, hash;
		PyObject *key;

		id_store_array = MEM_mallocN(sizeof(*id_store_array) * PySet_Size(datablocks), __func__);
		id_store = id_store_array;

		pos = hash = 0;
		while (_PySet_NextEntry(datablocks, &pos, &key, &hash)) {

			if (!pyrna_id_FromPyObject(key, &id_store->id)) {
				PyErr_Format(PyExc_TypeError,
				             "Expected and ID type, not %.200s",
				             Py_TYPE(key)->tp_name);
				ret = NULL;
				goto finally;
			}
			else {
				id_store->id_flag = id_store->id->flag;
				id_store->id_us = id_store->id->us;

				if (use_fake_user) {
					id_store->id->flag |= LIB_FAKEUSER;
				}
				id_store->id->us = 1;

				BKE_blendfile_write_partial_tag_ID(id_store->id, true);

				id_store_len += 1;
				id_store++;
			}
		}
	}

	/* write blend */
	int retval = 0;
	ReportList reports;

	BKE_reports_init(&reports, RPT_STORE);

	retval = BKE_blendfile_write_partial(bmain_src, filepath_abs, write_flags, &reports);

	/* cleanup state */
	BKE_blendfile_write_partial_end(bmain_src);

	if (retval) {
		BKE_reports_print(&reports, RPT_ERROR_ALL);
		BKE_reports_clear(&reports);
		ret = Py_None;
		Py_INCREF(ret);
	}
	else {
		if (BPy_reports_to_error(&reports, PyExc_IOError, true) == 0) {
			PyErr_SetString(PyExc_IOError, "Unknown error writing library data");
		}
		ret = NULL;
	}


finally:

	/* clear all flags for ID's added to the store (may run on error too) */
	id_store = id_store_array;

	for (int i = 0; i < id_store_len; id_store++, i++) {


		if (use_fake_user) {
			if ((id_store->id_flag & LIB_FAKEUSER) == 0) {
				id_store->id->flag &= ~LIB_FAKEUSER;
			}
		}

		id_store->id->us = id_store->id_us;

		BKE_blendfile_write_partial_tag_ID(id_store->id, false);
	}

	MEM_freeN(id_store_array);

	return ret;
}
Example #9
0
File: wm.c Project: mik0001/Blender
static void wm_reports_free(wmWindowManager *wm)
{
	BKE_reports_clear(&wm->reports);
	WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
}
Example #10
0
static int load_file(int UNUSED(argc), const char **argv, void *data)
{
	bContext *C = data;

	/* Make the path absolute because its needed for relative linked blends to be found */
	char filename[FILE_MAX];

	/* note, we could skip these, but so far we always tried to load these files */
	if (argv[0][0] == '-') {
		fprintf(stderr, "unknown argument, loading as file: %s\n", argv[0]);
	}

	BLI_strncpy(filename, argv[0], sizeof(filename));
	BLI_path_cwd(filename);

	if (G.background) {
		int retval = BKE_read_file(C, filename, NULL);

		/* we successfully loaded a blend file, get sure that
		 * pointcache works */
		if (retval != BKE_READ_FILE_FAIL) {
			wmWindowManager *wm = CTX_wm_manager(C);

			/* special case, 2.4x files */
			if (wm == NULL && CTX_data_main(C)->wm.first == NULL) {
				extern void wm_add_default(bContext *C);

				/* wm_add_default() needs the screen to be set. */
				CTX_wm_screen_set(C, CTX_data_main(C)->screen.first);
				wm_add_default(C);
			}

			CTX_wm_manager_set(C, NULL); /* remove wm to force check */
			WM_check(C);
			G.relbase_valid = 1;
			if (CTX_wm_manager(C) == NULL) CTX_wm_manager_set(C, wm);  /* reset wm */

			DAG_on_visible_update(CTX_data_main(C), TRUE);
		}
		else {
			/* failed to load file, stop processing arguments */
			return -1;
		}

		/* WM_file_read() runs normally but since we're in background mode do here */
#ifdef WITH_PYTHON
		/* run any texts that were loaded in and flagged as modules */
		BPY_python_reset(C);
#endif

		/* happens for the UI on file reading too (huh? (ton))*/
		// XXX		BKE_reset_undo();
		//			BKE_write_undo("original");	/* save current state */
	}
	else {
		/* we are not running in background mode here, but start blender in UI mode with
		 * a file - this should do everything a 'load file' does */
		ReportList reports;
		BKE_reports_init(&reports, RPT_PRINT);
		WM_file_autoexec_init(filename);
		WM_file_read(C, filename, &reports);
		BKE_reports_clear(&reports);
	}

	G.file_loaded = 1;

	return 0;
}
Example #11
0
static PyObject *pyop_call(PyObject *UNUSED(self), PyObject *args)
{
	wmOperatorType *ot;
	int error_val = 0;
	PointerRNA ptr;
	int operator_ret = OPERATOR_CANCELLED;

	char     *opname;
	char     *context_str = NULL;
	PyObject *kw = NULL; /* optional args */
	PyObject *context_dict = NULL; /* optional args */
	PyObject *context_dict_back;

	/* note that context is an int, python does the conversion in this case */
	int context = WM_OP_EXEC_DEFAULT;
	int is_undo = false;

	/* 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, "sO|O!si:_bpy.ops.call",
	                      &opname, &context_dict, &PyDict_Type, &kw, &context_str, &is_undo))
	{
		return NULL;
	}

	ot = WM_operatortype_find(opname, true);

	if (ot == NULL) {
		PyErr_Format(PyExc_AttributeError,
		             "Calling operator \"bpy.ops.%s\" error, "
		             "could not be found", opname);
		return NULL;
	}
	
	if (!pyrna_write_check()) {
		PyErr_Format(PyExc_RuntimeError,
		             "Calling operator \"bpy.ops.%s\" error, "
		             "can't modify blend data in this state (drawing/rendering)",
		             opname);
		return NULL;
	}

	if (context_str) {
		if (RNA_enum_value_from_id(operator_context_items, context_str, &context) == 0) {
			char *enum_str = BPy_enum_as_string(operator_context_items);
			PyErr_Format(PyExc_TypeError,
			             "Calling operator \"bpy.ops.%s\" error, "
			             "expected a string enum in (%.200s)",
			             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\" 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 */

	if (WM_operator_poll_context((bContext *)C, ot, context) == false) {
		const char *msg = CTX_wm_operator_poll_msg_get(C);
		PyErr_Format(PyExc_RuntimeError,
		             "Operator bpy.ops.%.200s.poll() %.200s",
		             opname, msg ? msg : "failed, context is incorrect");
		CTX_wm_operator_poll_msg_set(C, NULL); /* better set to NULL else it could be used again */
		error_val = -1;
	}
	else {
		WM_operator_properties_create_ptr(&ptr, ot);
		WM_operator_properties_sanitize(&ptr, 0);

		if (kw && PyDict_Size(kw))
			error_val = pyrna_pydict_to_props(&ptr, kw, 0, "Converting py args to operator properties: ");


		if (error_val == 0) {
			ReportList *reports;

			reports = MEM_mallocN(sizeof(ReportList), "wmOperatorReportList");
			BKE_reports_init(reports, RPT_STORE | RPT_OP_HOLD); /* own so these don't move into global reports */

#ifdef BPY_RELEASE_GIL
			/* release GIL, since a thread could be started from an operator
			 * that updates a driver */
			/* note: I have not seen any examples of code that does this
			 * so it may not be officially supported but seems to work ok. */
			{
				PyThreadState *ts = PyEval_SaveThread();
#endif

				operator_ret = WM_operator_call_py(C, ot, context, &ptr, reports, is_undo);

#ifdef BPY_RELEASE_GIL
				/* regain GIL */
				PyEval_RestoreThread(ts);
			}
#endif

			error_val = BPy_reports_to_error(reports, PyExc_RuntimeError, false);

			/* operator output is nice to have in the terminal/console too */
			if (reports->list.first) {
				char *report_str = BKE_reports_string(reports, 0); /* all reports */
	
				if (report_str) {
					PySys_WriteStdout("%s\n", report_str);
					MEM_freeN(report_str);
				}
			}
	
			BKE_reports_clear(reports);
			if ((reports->flag & RPT_FREE) == 0) {
				MEM_freeN(reports);
			}
		}

		WM_operator_properties_free(&ptr);

#if 0
		/* if there is some way to know an operator takes args we should use this */
		{
			/* no props */
			if (kw != NULL) {
				PyErr_Format(PyExc_AttributeError,
				             "Operator \"%s\" does not take any args",
				             opname);
				return NULL;
			}

			WM_operator_name_call(C, opname, WM_OP_EXEC_DEFAULT, NULL);
		}
#endif
	}

	/* 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);

	if (error_val == -1) {
		return NULL;
	}

	/* when calling  bpy.ops.wm.read_factory_settings() bpy.data's main pointer is freed by clear_globals(),
	 * further access will crash blender. setting context is not needed in this case, only calling because this
	 * function corrects bpy.data (internal Main pointer) */
	BPY_modules_update(C);

	/* needed for when WM_OT_read_factory_settings us called from within a script */
	bpy_import_main_set(CTX_data_main(C));

	/* return operator_ret as a bpy enum */
	return pyrna_enum_bitfield_to_py(operator_return_items, operator_ret);

}
Example #12
0
static void wm_free_reports(bContext *C)
{
	ReportList *reports = CTX_wm_reports(C);

	BKE_reports_clear(reports);
}
Example #13
0
bool KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
{
	Main *main_newlib; /* stored as a dynamic 'main' until we free it */
	Main *main_tmp= NULL; /* created only for linking, then freed */
	LinkNode *names = NULL;
	int idcode= BKE_idcode_from_name(group);
	short flag= 0; /* don't need any special options */
	ReportList reports;
	static char err_local[255];
	
	/* only scene and mesh supported right now */
	if (idcode!=ID_SCE && idcode!=ID_ME &&idcode!=ID_AC) {
		snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group);
		*err_str= err_local;
		BLO_blendhandle_close(bpy_openlib);
		return false;
	}
	
	if (GetMainDynamicPath(path)) {
		snprintf(err_local, sizeof(err_local), "blend file already open \"%s\"\n", path);
		*err_str= err_local;
		BLO_blendhandle_close(bpy_openlib);
		return false;
	}

	if (bpy_openlib==NULL) {
		snprintf(err_local, sizeof(err_local), "could not open blendfile \"%s\"\n", path);
		*err_str= err_local;
		return false;
	}
	
	main_newlib= (Main *)MEM_callocN( sizeof(Main), "BgeMain");
	BKE_reports_init(&reports, RPT_STORE);	

	/* here appending/linking starts */
	main_tmp = BLO_library_append_begin(main_newlib, &bpy_openlib, (char *)path);

	int totnames_dummy;
	names = BLO_blendhandle_get_datablock_names( bpy_openlib, idcode, &totnames_dummy);
	
	int i=0;
	LinkNode *n= names;
	while(n) {
		BLO_library_append_named_part(main_tmp, &bpy_openlib, (char *)n->link, idcode);
		n= (LinkNode *)n->next;
		i++;
	}
	BLI_linklist_free(names, free);	/* free linklist *and* each node's data */
	
	BLO_library_append_end(NULL, main_tmp, &bpy_openlib, idcode, flag);

	/* now do another round of linking for Scenes so all actions are properly loaded */
	if (idcode==ID_SCE && options & LIB_LOAD_LOAD_ACTIONS) {
		main_tmp = BLO_library_append_begin(main_newlib, &bpy_openlib, (char *)path);

		int totnames_dummy;
		names = BLO_blendhandle_get_datablock_names( bpy_openlib, ID_AC, &totnames_dummy);
	
		int i=0;
		LinkNode *n= names;
		while(n) {
			BLO_library_append_named_part(main_tmp, &bpy_openlib, (char *)n->link, ID_AC);
			n= (LinkNode *)n->next;
			i++;
		}
		BLI_linklist_free(names, free);	/* free linklist *and* each node's data */
	
		BLO_library_append_end(NULL, main_tmp, &bpy_openlib, ID_AC, flag);
	}
	
	BLO_blendhandle_close(bpy_openlib);

	BKE_reports_clear(&reports);
	/* done linking */	
	
	/* needed for lookups*/
	GetMainDynamic().push_back(main_newlib);
	strncpy(main_newlib->name, path, sizeof(main_newlib->name));	
	
	
	if (idcode==ID_ME) {
		/* Convert all new meshes into BGE meshes */
		ID* mesh;
	
		for (mesh= (ID *)main_newlib->mesh.first; mesh; mesh= (ID *)mesh->next ) {
			if (options & LIB_LOAD_VERBOSE)
				printf("MeshName: %s\n", mesh->name+2);
			RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)mesh, NULL, scene_merge, this);
			scene_merge->GetLogicManager()->RegisterMeshName(meshobj->GetName(),meshobj);
		}
	}
	else if (idcode==ID_AC) {
		/* Convert all actions */
		ID *action;

		for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) {
			if (options & LIB_LOAD_VERBOSE)
				printf("ActionName: %s\n", action->name+2);
			scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action);
		}
	}
	else if (idcode==ID_SCE) {		
		/* Merge all new linked in scene into the existing one */
		ID *scene;
		for (scene= (ID *)main_newlib->scene.first; scene; scene= (ID *)scene->next ) {
			if (options & LIB_LOAD_VERBOSE)
				printf("SceneName: %s\n", scene->name+2);
			
			/* merge into the base  scene */
			KX_Scene* other= m_ketsjiEngine->CreateScene((Scene *)scene);
			scene_merge->MergeScene(other);
			
			// RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene
			delete other;
		}

		/* Now handle all the actions */
		if (options & LIB_LOAD_LOAD_ACTIONS) {
			ID *action;

			for (action= (ID *)main_newlib->action.first; action; action= (ID *)action->next) {
				if (options & LIB_LOAD_VERBOSE)
					printf("ActionName: %s\n", action->name+2);
				scene_merge->GetLogicManager()->RegisterActionName(action->name+2, action);
			}
		}
	}
	
	return true;
}
Example #14
0
static void wm_free_reports(bContext *C)
{
	BKE_reports_clear(CTX_wm_reports(C));
}
KX_LibLoadStatus *KX_BlenderSceneConverter::LinkBlendFile(BlendHandle *bpy_openlib, const char *path, char *group, KX_Scene *scene_merge, char **err_str, short options)
{
	Main *main_newlib; /* stored as a dynamic 'main' until we free it */
	const int idcode = BKE_idcode_from_name(group);
	ReportList reports;
	static char err_local[255];

//	TIMEIT_START(bge_link_blend_file);

	KX_LibLoadStatus *status;

	/* only scene and mesh supported right now */
	if (idcode != ID_SCE && idcode != ID_ME && idcode != ID_AC) {
		snprintf(err_local, sizeof(err_local), "invalid ID type given \"%s\"\n", group);
		*err_str = err_local;
		BLO_blendhandle_close(bpy_openlib);
		return NULL;
	}
	
	if (GetMainDynamicPath(path)) {
		snprintf(err_local, sizeof(err_local), "blend file already open \"%s\"\n", path);
		*err_str = err_local;
		BLO_blendhandle_close(bpy_openlib);
		return NULL;
	}

	if (bpy_openlib == NULL) {
		snprintf(err_local, sizeof(err_local), "could not open blendfile \"%s\"\n", path);
		*err_str = err_local;
		return NULL;
	}

	main_newlib = BKE_main_new();
	BKE_reports_init(&reports, RPT_STORE);

	short flag = 0; /* don't need any special options */
	/* created only for linking, then freed */
	Main *main_tmp = BLO_library_link_begin(main_newlib, &bpy_openlib, (char *)path);

	load_datablocks(main_tmp, bpy_openlib, path, idcode);

	if (idcode == ID_SCE && options & LIB_LOAD_LOAD_SCRIPTS) {
		load_datablocks(main_tmp, bpy_openlib, path, ID_TXT);
	}

	/* now do another round of linking for Scenes so all actions are properly loaded */
	if (idcode == ID_SCE && options & LIB_LOAD_LOAD_ACTIONS) {
		load_datablocks(main_tmp, bpy_openlib, path, ID_AC);
	}

	BLO_library_link_end(main_tmp, &bpy_openlib, flag, NULL, NULL);

	BLO_blendhandle_close(bpy_openlib);

	BKE_reports_clear(&reports);
	/* done linking */
	
	/* needed for lookups*/
	GetMainDynamic().push_back(main_newlib);
	BLI_strncpy(main_newlib->name, path, sizeof(main_newlib->name));
	
	
	status = new KX_LibLoadStatus(this, m_ketsjiEngine, scene_merge, path);

	if (idcode == ID_ME) {
		/* Convert all new meshes into BGE meshes */
		ID *mesh;
	
		for (mesh = (ID *)main_newlib->mesh.first; mesh; mesh = (ID *)mesh->next ) {
			if (options & LIB_LOAD_VERBOSE)
				printf("MeshName: %s\n", mesh->name + 2);
			RAS_MeshObject *meshobj = BL_ConvertMesh((Mesh *)mesh, NULL, scene_merge, this, false); // For now only use the libloading option for scenes, which need to handle materials/shaders
			scene_merge->GetLogicManager()->RegisterMeshName(meshobj->GetName(), meshobj);
		}
	}
	else if (idcode == ID_AC) {
		/* Convert all actions */
		ID *action;

		for (action= (ID *)main_newlib->action.first; action; action = (ID *)action->next) {
			if (options & LIB_LOAD_VERBOSE)
				printf("ActionName: %s\n", action->name + 2);
			scene_merge->GetLogicManager()->RegisterActionName(action->name + 2, action);
		}
	}
	else if (idcode == ID_SCE) {
		/* Merge all new linked in scene into the existing one */
		ID *scene;
		// scenes gets deleted by the thread when it's done using it (look in async_convert())
		vector<Scene *> *scenes = (options & LIB_LOAD_ASYNC) ? new vector<Scene *>() : NULL;

		for (scene = (ID *)main_newlib->scene.first; scene; scene = (ID *)scene->next ) {
			if (options & LIB_LOAD_VERBOSE)
				printf("SceneName: %s\n", scene->name + 2);
			
			if (options & LIB_LOAD_ASYNC) {
				scenes->push_back((Scene *)scene);
			} 
			else {
				/* merge into the base  scene */
				KX_Scene* other = m_ketsjiEngine->CreateScene((Scene *)scene, true);
				scene_merge->MergeScene(other);
			
				// RemoveScene(other); // Don't run this, it frees the entire scene converter data, just delete the scene
				delete other;
			}
		}

		if (options & LIB_LOAD_ASYNC) {
			status->SetData(scenes);
			BLI_task_pool_push(m_threadinfo->m_pool, async_convert, (void *)status, false, TASK_PRIORITY_LOW);
		}

#ifdef WITH_PYTHON
		/* Handle any text datablocks */
		if (options & LIB_LOAD_LOAD_SCRIPTS)
			addImportMain(main_newlib);
#endif

		/* Now handle all the actions */
		if (options & LIB_LOAD_LOAD_ACTIONS) {
			ID *action;

			for (action = (ID *)main_newlib->action.first; action; action = (ID *)action->next) {
				if (options & LIB_LOAD_VERBOSE)
					printf("ActionName: %s\n", action->name + 2);
				scene_merge->GetLogicManager()->RegisterActionName(action->name + 2, action);
			}
		}
	}

	if (!(options & LIB_LOAD_ASYNC))
		status->Finish();

//	TIMEIT_END(bge_link_blend_file);

	m_status_map[main_newlib->name] = status;
	return status;
}