Ejemplo n.º 1
0
/**
 * \return success
 */
bool BPY_execute_string_as_number(bContext *C, const char *expr, double *value, const bool verbose)
{
	PyGILState_STATE gilstate;
	bool ok = true;

	if (!value || !expr) return -1;

	if (expr[0] == '\0') {
		*value = 0.0;
		return ok;
	}

	bpy_context_set(C, &gilstate);

	ok = PyC_RunString_AsNumber(expr, value, "<blender button>");

	if (ok == false) {
		if (verbose) {
			BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
		}
		else {
			PyErr_Clear();
		}
	}

	bpy_context_clear(C, &gilstate);

	return ok;
}
Ejemplo n.º 2
0
/* callbacks */
static void bpy_prop_update_cb(struct bContext *C, struct PointerRNA *ptr, struct PropertyRNA *prop)
{
	PyGILState_STATE gilstate;
	PyObject **py_data= (PyObject **)RNA_property_py_data_get(prop);
	PyObject *py_func;
	PyObject *args;
	PyObject *self;
	PyObject *ret;
	const int is_write_ok= pyrna_write_check();

	BLI_assert(py_data != NULL);

	if(!is_write_ok) {
		pyrna_write_set(TRUE);
	}

	bpy_context_set(C, &gilstate);

	py_func= py_data[BPY_DATA_CB_SLOT_UPDATE];

	args= PyTuple_New(2);
	self= pyrna_struct_as_instance(ptr);
	PyTuple_SET_ITEM(args, 0, self);

	PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
	Py_INCREF(bpy_context_module);

	ret= PyObject_CallObject(py_func, args);

	Py_DECREF(args);

	if(ret == NULL) {
		printf_func_error(py_func);
	}
	else {
		if(ret != Py_None) {
			PyErr_SetString(PyExc_ValueError, "the return value must be None");
			printf_func_error(py_func);
		}

		Py_DECREF(ret);
	}

	bpy_context_clear(C, &gilstate);

	if(!is_write_ok) {
		pyrna_write_set(FALSE);
	}
}
Ejemplo n.º 3
0
void BPY_modules_load_user(bContext *C)
{
	PyGILState_STATE gilstate;
	Main *bmain = CTX_data_main(C);
	Text *text;

	/* can happen on file load */
	if (bmain == NULL)
		return;

	/* update pointers since this can run from a nested script
	 * on file load */
	if (py_call_level) {
		BPY_context_update(C);
	}

	bpy_context_set(C, &gilstate);

	for (text = bmain->text.first; text; text = text->id.next) {
		if (text->flags & TXT_ISSCRIPT && BLI_testextensie(text->id.name + 2, ".py")) {
			if (!(G.f & G_SCRIPT_AUTOEXEC)) {
				if (!(G.f & G_SCRIPT_AUTOEXEC_FAIL_QUIET)) {
					G.f |= G_SCRIPT_AUTOEXEC_FAIL;
					BLI_snprintf(G.autoexec_fail, sizeof(G.autoexec_fail), "Text '%s'", text->id.name + 2);

					printf("scripts disabled for \"%s\", skipping '%s'\n", bmain->name, text->id.name + 2);
				}
			}
			else {
				PyObject *module = bpy_text_import(text);

				if (module == NULL) {
					PyErr_Print();
					PyErr_Clear();
				}
				else {
					Py_DECREF(module);
				}

				/* check if the script loaded a new file */
				if (bmain != CTX_data_main(C)) {
					break;
				}
			}
		}
	}
	bpy_context_clear(C, &gilstate);
}
Ejemplo n.º 4
0
int BPY_string_exec(bContext *C, const char *expr)
{
	PyGILState_STATE gilstate;
	PyObject *main_mod = NULL;
	PyObject *py_dict, *retval;
	int error_ret = 0;
	Main *bmain_back; /* XXX, quick fix for release (Copy Settings crash), needs further investigation */

	if (!expr) return -1;

	if (expr[0] == '\0') {
		return error_ret;
	}

	bpy_context_set(C, &gilstate);

	PyC_MainModule_Backup(&main_mod);

	py_dict = PyC_DefaultNameSpace("<blender string>");

	bmain_back = bpy_import_main_get();
	bpy_import_main_set(CTX_data_main(C));

	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);

	bpy_import_main_set(bmain_back);

	if (retval == NULL) {
		error_ret = -1;

		BPy_errors_to_report(CTX_wm_reports(C));
	}
	else {
		Py_DECREF(retval);
	}

	PyC_MainModule_Restore(main_mod);

	bpy_context_clear(C, &gilstate);
	
	return error_ret;
}
Ejemplo n.º 5
0
static void cb_region_draw(const bContext *C, ARegion *UNUSED(ar), void *customdata)
{
	PyObject *cb_func, *cb_args, *result;
	PyGILState_STATE gilstate;

	bpy_context_set((bContext *)C, &gilstate);

	cb_func = PyTuple_GET_ITEM((PyObject *)customdata, 1);
	cb_args = PyTuple_GET_ITEM((PyObject *)customdata, 2);
	result = PyObject_CallObject(cb_func, cb_args);

	if (result) {
		Py_DECREF(result);
	}
	else {
		PyErr_Print();
		PyErr_Clear();
	}

	bpy_context_clear((bContext *)C, &gilstate);
}
Ejemplo n.º 6
0
/* return -1 on error, else 0 */
int BPY_button_exec(bContext *C, const char *expr, double *value, const short verbose)
{
	PyGILState_STATE gilstate;
	PyObject *py_dict, *mod, *retval;
	int error_ret = 0;
	PyObject *main_mod = NULL;
	
	if (!value || !expr) return -1;

	if (expr[0] == '\0') {
		*value = 0.0;
		return error_ret;
	}

	bpy_context_set(C, &gilstate);

	PyC_MainModule_Backup(&main_mod);

	py_dict = PyC_DefaultNameSpace("<blender button>");

	mod = PyImport_ImportModule("math");
	if (mod) {
		PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
		Py_DECREF(mod);
	}
	else { /* highly unlikely but possibly */
		PyErr_Print();
		PyErr_Clear();
	}
	
	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
	
	if (retval == NULL) {
		error_ret = -1;
	}
	else {
		double val;

		if (PyTuple_Check(retval)) {
			/* Users my have typed in 10km, 2m
			 * add up all values */
			int i;
			val = 0.0;

			for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
				val += PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
			}
		}
		else {
			val = PyFloat_AsDouble(retval);
		}
		Py_DECREF(retval);
		
		if (val == -1 && PyErr_Occurred()) {
			error_ret = -1;
		}
		else if (!finite(val)) {
			*value = 0.0;
		}
		else {
			*value = val;
		}
	}
	
	if (error_ret) {
		if (verbose) {
			BPy_errors_to_report(CTX_wm_reports(C));
		}
		else {
			PyErr_Clear();
		}
	}

	PyC_MainModule_Backup(&main_mod);
	
	bpy_context_clear(C, &gilstate);
	
	return error_ret;
}
Ejemplo n.º 7
0
static int python_script_exec(bContext *C, const char *fn, struct Text *text,
                              struct ReportList *reports, const short do_jump)
{
	PyObject *main_mod = NULL;
	PyObject *py_dict = NULL, *py_result = NULL;
	PyGILState_STATE gilstate;

	BLI_assert(fn || text);

	if (fn == NULL && text == NULL) {
		return 0;
	}

	bpy_context_set(C, &gilstate);

	PyC_MainModule_Backup(&main_mod);

	if (text) {
		char fn_dummy[FILE_MAXDIR];
		bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);

		if (text->compiled == NULL) {   /* if it wasn't already compiled, do it now */
			char *buf = txt_to_buf(text);

			text->compiled = Py_CompileString(buf, fn_dummy, Py_file_input);

			MEM_freeN(buf);

			if (PyErr_Occurred()) {
				if (do_jump) {
					python_script_error_jump_text(text);
				}
				BPY_text_free_code(text);
			}
		}

		if (text->compiled) {
			py_dict = PyC_DefaultNameSpace(fn_dummy);
			py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
		}

	}
	else {
		FILE *fp = BLI_fopen(fn, "r");

		if (fp) {
			py_dict = PyC_DefaultNameSpace(fn);

#ifdef _WIN32
			/* Previously we used PyRun_File to run directly the code on a FILE
			 * object, but as written in the Python/C API Ref Manual, chapter 2,
			 * 'FILE structs for different C libraries can be different and
			 * incompatible'.
			 * So now we load the script file data to a buffer */
			{
				char *pystring;

				fclose(fp);

				pystring = MEM_mallocN(strlen(fn) + 32, "pystring");
				pystring[0] = '\0';
				sprintf(pystring, "exec(open(r'%s').read())", fn);
				py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
				MEM_freeN(pystring);
			}
#else
			py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
			fclose(fp);
#endif
		}
		else {
			PyErr_Format(PyExc_IOError,
			             "Python file \"%s\" could not be opened: %s",
			             fn, strerror(errno));
			py_result = NULL;
		}
	}

	if (!py_result) {
		if (text) {
			if (do_jump) {
				python_script_error_jump_text(text);
			}
		}
		BPy_errors_to_report(reports);
	}
	else {
		Py_DECREF(py_result);
	}

	if (py_dict) {
#ifdef PYMODULE_CLEAR_WORKAROUND
		PyModuleObject *mmod = (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__");
		PyObject *dict_back = mmod->md_dict;
		/* freeing the module will clear the namespace,
		 * gives problems running classes defined in this namespace being used later. */
		mmod->md_dict = NULL;
		Py_DECREF(dict_back);
#endif

#undef PYMODULE_CLEAR_WORKAROUND
	}

	PyC_MainModule_Restore(main_mod);

	bpy_context_clear(C, &gilstate);

	return (py_result != NULL);
}
Ejemplo n.º 8
0
static EnumPropertyItem *bpy_props_enum_itemf(struct bContext *C, PointerRNA *ptr, PropertyRNA *prop, int *free)
{
	PyGILState_STATE gilstate;

	PyObject *py_func= RNA_property_enum_py_data_get(prop);
	PyObject *self= NULL;
	PyObject *args;
	PyObject *items; /* returned from the function call */

	EnumPropertyItem *eitems= NULL;
	int err= 0;

	bpy_context_set(C, &gilstate);

	args= PyTuple_New(2);
	self= pyrna_struct_as_instance(ptr);
	PyTuple_SET_ITEM(args, 0, self);

	/* now get the context */
	PyTuple_SET_ITEM(args, 1, (PyObject *)bpy_context_module);
	Py_INCREF(bpy_context_module);

	items= PyObject_CallObject(py_func, args);

	Py_DECREF(args);

	if(items==NULL) {
		err= -1;
	}
	else {
		PyObject *items_fast;
		int defvalue_dummy=0;

		if(!(items_fast= PySequence_Fast(items, "EnumProperty(...): return value from the callback was not a sequence"))) {
			err= -1;
		}
		else {
			eitems= enum_items_from_py(items_fast, NULL, &defvalue_dummy, (RNA_property_flag(prop) & PROP_ENUM_FLAG)!=0);

			Py_DECREF(items_fast);

			if(!eitems) {
				err= -1;
			}
		}

		Py_DECREF(items);
	}

	if(err != -1) { /* worked */
		*free= 1;
	}
	else {
		printf_func_error(py_func);

		eitems= DummyRNA_NULL_items;
	}


	bpy_context_clear(C, &gilstate);
	return eitems;
}
Ejemplo n.º 9
0
static bool python_script_exec(
        bContext *C, const char *fn, struct Text *text,
        struct ReportList *reports, const bool do_jump)
{
	Main *bmain_old = CTX_data_main(C);
	PyObject *main_mod = NULL;
	PyObject *py_dict = NULL, *py_result = NULL;
	PyGILState_STATE gilstate;

	BLI_assert(fn || text);

	if (fn == NULL && text == NULL) {
		return 0;
	}

	bpy_context_set(C, &gilstate);

	PyC_MainModule_Backup(&main_mod);

	if (text) {
		char fn_dummy[FILE_MAXDIR];
		bpy_text_filename_get(fn_dummy, sizeof(fn_dummy), text);

		if (text->compiled == NULL) {   /* if it wasn't already compiled, do it now */
			char *buf;
			PyObject *fn_dummy_py;

			fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);

			buf = txt_to_buf(text);
			text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
			MEM_freeN(buf);

			Py_DECREF(fn_dummy_py);

			if (PyErr_Occurred()) {
				if (do_jump) {
					python_script_error_jump_text(text);
				}
				BPY_text_free_code(text);
			}
		}

		if (text->compiled) {
			py_dict = PyC_DefaultNameSpace(fn_dummy);
			py_result =  PyEval_EvalCode(text->compiled, py_dict, py_dict);
		}

	}
	else {
		FILE *fp = BLI_fopen(fn, "r");

		if (fp) {
			py_dict = PyC_DefaultNameSpace(fn);

#ifdef _WIN32
			/* Previously we used PyRun_File to run directly the code on a FILE
			 * object, but as written in the Python/C API Ref Manual, chapter 2,
			 * 'FILE structs for different C libraries can be different and
			 * incompatible'.
			 * So now we load the script file data to a buffer */
			{
				const char *pystring =
				        "ns = globals().copy()\n"
				        "with open(__file__, 'rb') as f: exec(compile(f.read(), __file__, 'exec'), ns)";

				fclose(fp);

				py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
			}
#else
			py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
			fclose(fp);
#endif
		}
		else {
			PyErr_Format(PyExc_IOError,
			             "Python file \"%s\" could not be opened: %s",
			             fn, strerror(errno));
			py_result = NULL;
		}
	}

	if (!py_result) {
		if (text) {
			if (do_jump) {
				/* ensure text is valid before use, the script may have freed its self */
				Main *bmain_new = CTX_data_main(C);
				if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->text, text) != -1)) {
					python_script_error_jump_text(text);
				}
			}
		}
		BPy_errors_to_report(reports);
	}
	else {
		Py_DECREF(py_result);
	}

	if (py_dict) {
#ifdef PYMODULE_CLEAR_WORKAROUND
		PyModuleObject *mmod = (PyModuleObject *)PyDict_GetItemString(PyThreadState_GET()->interp->modules, "__main__");
		PyObject *dict_back = mmod->md_dict;
		/* freeing the module will clear the namespace,
		 * gives problems running classes defined in this namespace being used later. */
		mmod->md_dict = NULL;
		Py_DECREF(dict_back);
#endif

#undef PYMODULE_CLEAR_WORKAROUND
	}

	PyC_MainModule_Restore(main_mod);

	bpy_context_clear(C, &gilstate);

	return (py_result != NULL);
}