change unit evaluation only to do try the units replacements if evaluating with python fails, in rare cases its possible a valid python expression could get units applied to it.

This commit is contained in:
2011-04-17 12:47:20 +00:00
parent d00f664ee0
commit 0862abf68b
3 changed files with 30 additions and 15 deletions

View File

@@ -1601,25 +1601,34 @@ int ui_set_but_string(bContext *C, uiBut *but, const char *str)
double value;
#ifdef WITH_PYTHON
{
char str_unit_convert[256];
int unit_type= uiButGetUnitType(but);
Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
int ok= FALSE;
BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
if(str[0] != '\0') {
int is_unit_but= ui_is_but_unit(but);
/* only enable verbose if we won't run again with units */
if(BPY_button_exec(C, str, &value, is_unit_but==FALSE) != -1) {
ok= TRUE; /* parse normal string via py (no unit conversion needed) */
}
else if(is_unit_but) {
/* parse failed, this is a unit but so run replacements and parse again */
char str_unit_convert[256];
const int unit_type= uiButGetUnitType(but);
Scene *scene= CTX_data_scene((bContext *)but->block->evil_C);
BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
if(ui_is_but_unit(but)) {
/* ugly, use the draw string to get the value, this could cause problems if it includes some text which resolves to a unit */
bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr, ui_get_but_scale_unit(but, 1.0), scene->unit.system, unit_type>>16);
}
if(BPY_button_exec(C, str_unit_convert, &value)) {
value = ui_get_but_val(but); /* use its original value */
if(str[0])
return 0;
if(BPY_button_exec(C, str_unit_convert, &value, TRUE) != -1) {
ok= TRUE;
}
}
}
if(ok == FALSE) {
return 0;
}
#else
value= atof(str);
#endif // WITH_PYTHON

View File

@@ -86,7 +86,7 @@ void BPY_modules_load_user(struct bContext *C);
void BPY_driver_reset(void);
float BPY_driver_exec(struct ChannelDriver *driver);
int BPY_button_exec(struct bContext *C, const char *expr, double *value);
int BPY_button_exec(struct bContext *C, const char *expr, double *value, const short verbose);
int BPY_string_exec(struct bContext *C, const char *expr);
void BPY_DECREF(void *pyob_ptr); /* Py_DECREF() */

View File

@@ -471,7 +471,8 @@ void BPY_DECREF(void *pyob_ptr)
PyGILState_Release(gilstate);
}
int BPY_button_exec(bContext *C, const char *expr, double *value)
/* 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;
@@ -536,7 +537,12 @@ int BPY_button_exec(bContext *C, const char *expr, double *value)
}
if(error_ret) {
BPy_errors_to_report(CTX_wm_reports(C));
if(verbose) {
BPy_errors_to_report(CTX_wm_reports(C));
}
else {
PyErr_Clear();
}
}
PyC_MainModule_Backup(&main_mod);