fix for python not being able to call operators with a executuon context.

This commit is contained in:
Campbell Barton 2010-02-10 11:10:38 +00:00
parent fbc201d2dd
commit c2b2ccde45
6 changed files with 34 additions and 39 deletions

@ -27,19 +27,6 @@ op_call = ops_module.call
op_as_string = ops_module.as_string
op_get_rna = ops_module.get_rna
# Keep in sync with WM_types.h
context_dict = {
'INVOKE_DEFAULT': 0,
'INVOKE_REGION_WIN': 1,
'INVOKE_AREA': 2,
'INVOKE_SCREEN': 3,
'EXEC_DEFAULT': 4,
'EXEC_REGION_WIN': 5,
'EXEC_AREA': 6,
'EXEC_SCREEN': 7,
}
class bpy_ops(object):
'''
Fake module like class.
@ -161,16 +148,10 @@ class bpy_ops_submodule_op(object):
else:
C_exec = args[0]
try:
context = context_dict[C_exec]
except:
raise ValueError("Expected a single context argument in: " + \
str(list(context_dict.keys())))
if len(args) == 2:
C_dict = args[1]
ret = op_call(self.idname_py(), C_dict, kw, context)
ret = op_call(self.idname_py(), C_dict, kw, C_exec)
else:
ret = op_call(self.idname_py(), C_dict, kw)

@ -75,6 +75,8 @@ extern EnumPropertyItem space_type_items[];
extern EnumPropertyItem keymap_propvalue_items[];
extern EnumPropertyItem operator_context_items[];
extern EnumPropertyItem wm_report_items[];
extern EnumPropertyItem property_unit_items[];

@ -36,6 +36,22 @@
#include "WM_types.h"
/* see WM_types.h */
EnumPropertyItem operator_context_items[] = {
{WM_OP_INVOKE_DEFAULT, "INVOKE_DEFAULT", 0, "Invoke Default", ""},
{WM_OP_INVOKE_REGION_WIN, "INVOKE_REGION_WIN", 0, "Invoke Region Window", ""},
{WM_OP_INVOKE_REGION_CHANNELS, "INVOKE_REGION_CHANNELS", 0, "Invoke Region Channels", ""},
{WM_OP_INVOKE_REGION_PREVIEW, "INVOKE_REGION_PREVIEW", 0, "Invoke Region Preview", ""},
{WM_OP_INVOKE_AREA, "INVOKE_AREA", 0, "Invoke Area", ""},
{WM_OP_INVOKE_SCREEN, "INVOKE_SCREEN", 0, "Invoke Screen", ""},
{WM_OP_EXEC_DEFAULT, "EXEC_DEFAULT", 0, "Exec Default", ""},
{WM_OP_EXEC_REGION_WIN, "EXEC_REGION_WIN", 0, "Exec Region Window", ""},
{WM_OP_EXEC_REGION_CHANNELS, "EXEC_REGION_CHANNELS", 0, "Exec Region Channels", ""},
{WM_OP_EXEC_REGION_PREVIEW, "EXEC_REGION_PREVIEW", 0, "Exec Region Preview", ""},
{WM_OP_EXEC_AREA, "EXEC_AREA", 0, "Exec Area", ""},
{WM_OP_EXEC_SCREEN, "EXEC_SCREEN", 0, "Exec Screen", ""},
{0, NULL, 0, NULL, NULL}};
#ifdef RNA_RUNTIME
#include "MEM_guardedalloc.h"
@ -517,22 +533,6 @@ static void rna_def_ui_layout(BlenderRNA *brna)
{UI_LAYOUT_ALIGN_CENTER, "CENTER", 0, "Center", ""},
{UI_LAYOUT_ALIGN_RIGHT, "RIGHT", 0, "Right", ""},
{0, NULL, 0, NULL, NULL}};
/* see WM_types.h */
static EnumPropertyItem operator_context_items[] = {
{WM_OP_INVOKE_DEFAULT, "INVOKE_DEFAULT", 0, "Invoke Default", ""},
{WM_OP_INVOKE_REGION_WIN, "INVOKE_REGION_WIN", 0, "Invoke Region Window", ""},
{WM_OP_INVOKE_REGION_CHANNELS, "INVOKE_REGION_CHANNELS", 0, "Invoke Region Channels", ""},
{WM_OP_INVOKE_REGION_PREVIEW, "INVOKE_REGION_PREVIEW", 0, "Invoke Region Preview", ""},
{WM_OP_INVOKE_AREA, "INVOKE_AREA", 0, "Invoke Area", ""},
{WM_OP_INVOKE_SCREEN, "INVOKE_SCREEN", 0, "Invoke Screen", ""},
{WM_OP_EXEC_DEFAULT, "EXEC_DEFAULT", 0, "Exec Default", ""},
{WM_OP_EXEC_REGION_WIN, "EXEC_REGION_WIN", 0, "Exec Region Window", ""},
{WM_OP_EXEC_REGION_CHANNELS, "EXEC_REGION_CHANNELS", 0, "Exec Region Channels", ""},
{WM_OP_EXEC_REGION_PREVIEW, "EXEC_REGION_PREVIEW", 0, "Exec Region Preview", ""},
{WM_OP_EXEC_AREA, "EXEC_AREA", 0, "Exec Area", ""},
{WM_OP_EXEC_SCREEN, "EXEC_SCREEN", 0, "Exec Screen", ""},
{0, NULL, 0, NULL, NULL}};
/* layout */

@ -48,8 +48,9 @@ static PyObject *pyop_call( PyObject * self, PyObject * args)
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;
@ -60,7 +61,7 @@ static PyObject *pyop_call( PyObject * self, PyObject * args)
// 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 = BPy_GetContext();
if (!PyArg_ParseTuple(args, "sO|O!i:_bpy.ops.call", &opname, &context_dict, &PyDict_Type, &kw, &context))
if (!PyArg_ParseTuple(args, "sO|O!s:_bpy.ops.call", &opname, &context_dict, &PyDict_Type, &kw, &context_str))
return NULL;
ot= WM_operatortype_exists(opname);
@ -70,6 +71,15 @@ static PyObject *pyop_call( PyObject * self, PyObject * args)
return NULL;
}
if(context_str) {
if(RNA_enum_value_from_id(operator_context_items, context_str, &operator_ret)==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(!PyDict_Check(context_dict))
context_dict= NULL;

@ -389,7 +389,7 @@ static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
RNA_property_enum_items(BPy_GetContext(), ptr, prop, &item, NULL, &free);
if(item) {
result= (char*)BPy_enum_as_string(item);
result= BPy_enum_as_string(item);
}
else {
result= "";
@ -401,6 +401,7 @@ static char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
return result;
}
static int pyrna_string_to_enum(PyObject *item, PointerRNA *ptr, PropertyRNA *prop, int *val, const char *error_prefix)
{
char *param= _PyUnicode_AsString(item);

@ -2369,6 +2369,7 @@ static void do_merge_fullsample(Render *re, bNodeTree *ntree)
float *col= rres.rectf + 4*y*re->rectx;
for(x=0; x<re->rectx; x++, rf+=4, col+=4) {
/* clamping to 1.0 is needed for correct AA */
if(col[0]<0.0f) col[0]=0.0f; else if(col[0] > 1.0f) col[0]= 1.0f;
if(col[1]<0.0f) col[1]=0.0f; else if(col[1] > 1.0f) col[1]= 1.0f;
if(col[2]<0.0f) col[2]=0.0f; else if(col[2] > 1.0f) col[2]= 1.0f;