* temporary PKey in the script and 3D view runs "./test.py" (for testing PyOperators...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 25 Dec 2008 14:17:54 +0000 (14:17 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 25 Dec 2008 14:17:54 +0000 (14:17 +0000)
* added ED_SCRIPT_OT_run_pyfile that takes a filename argument.
* RNA_property_string_set didn't add a value to ID props if the prop wasnt there (like ints, floats and bools do)
* bpy_operator.c - raise an error when unknown keyword args are passed to any operator .

Examples of bpy operator api...

bpyoperator.ED_VIEW3D_OT_viewhome(center=1)
bpyoperator.ED_SCR_OT_frame_offset(delta=10)
bpyoperator.ED_VIEW3D_OT_make_parent(type='OBJECT')

At the moment there is no way to stop the operators .invoke() function from running so ED_VIEW3D_OT_make_parent still opens the menu even though it doesn't need to.

source/blender/editors/space_script/SConscript
source/blender/editors/space_script/script_edit.c [new file with mode: 0644]
source/blender/editors/space_script/script_intern.h
source/blender/editors/space_script/script_ops.c [new file with mode: 0644]
source/blender/editors/space_script/space_script.c
source/blender/editors/space_view3d/view3d_ops.c
source/blender/makesrna/intern/rna_access.c
source/blender/python/intern/bpy_interface.c
source/blender/python/intern/bpy_operator.c

index 62af4de4caf4b9c9468eb496ceee776094a94f8a..8bf0447a6a0232c87fc87f0965b9fc8a1f06e44e 100644 (file)
@@ -5,5 +5,8 @@ sources = env.Glob('*.c')
 
 incs = '../include ../../blenlib ../../blenkernel ../../makesdna ../../imbuf'
 incs += ' ../../windowmanager #/intern/guardedalloc #/extern/glew/include'
+incs += ' ../../makesrna'
+incs += ' ../../python'
+
 
 env.BlenderLib ( 'bf_editors_space_script', sources, Split(incs), [], libtype=['core'], priority=[90] )
diff --git a/source/blender/editors/space_script/script_edit.c b/source/blender/editors/space_script/script_edit.c
new file mode 100644 (file)
index 0000000..6422061
--- /dev/null
@@ -0,0 +1,89 @@
+/**
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2008 Blender Foundation.
+ * All rights reserved.
+ *
+ *
+ * Contributor(s): Blender Foundation
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#include "DNA_space_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_userdef_types.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_blenlib.h"
+
+#include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_screen.h"
+#include "BKE_utildefines.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "ED_screen.h"
+#include "ED_types.h"
+
+#include "UI_interface.h"
+#include "UI_resources.h"
+
+#include "script_intern.h"     // own include
+
+
+#include "BPY_extern.h" /* BPY_run_python_script */
+
+static int run_pyfile_exec(bContext *C, wmOperator *op)
+{
+       ScrArea *sa= CTX_wm_area(C);
+       ARegion *ar= CTX_wm_region(C);
+
+       char filename[512];
+       RNA_string_get(op->ptr, "filename", filename);
+
+       BPY_run_python_script(C, filename);
+
+       ED_region_tag_redraw(ar);
+
+       return OPERATOR_FINISHED;
+}
+
+void ED_SCRIPT_OT_run_pyfile(wmOperatorType *ot)
+{
+
+       /* identifiers */
+       ot->name= "Run python file";
+       ot->idname= "ED_SCRIPT_OT_run_pyfile";
+
+       /* api callbacks */
+       ot->exec= run_pyfile_exec;
+       ot->poll= ED_operator_areaactive;
+
+       RNA_def_property(ot->srna, "filename", PROP_STRING, PROP_FILEPATH);
+}
index ede9d8468cb3991e6ccca82b3f01f24e4de7ca28..599964956668f98bd7ec823607c264c513bbd2e5 100644 (file)
 /* script_header.c */
 void script_header_buttons(const bContext *C, ARegion *ar);
 
+/* script_ops.c */
+void script_operatortypes(void);
+void script_keymap(struct wmWindowManager *wm);
+
+/* script_edit.c */
+void ED_SCRIPT_OT_run_pyfile(struct wmOperatorType *ot);
 
 #endif /* ED_SCRIPT_INTERN_H */
 
diff --git a/source/blender/editors/space_script/script_ops.c b/source/blender/editors/space_script/script_ops.c
new file mode 100644 (file)
index 0000000..f6e4fa7
--- /dev/null
@@ -0,0 +1,72 @@
+/**
+ * $Id:
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2008 Blender Foundation.
+ * All rights reserved.
+ *
+ *
+ * Contributor(s): Blender Foundation
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <stdlib.h>
+#include <math.h>
+
+#include "MEM_guardedalloc.h"
+
+#include "DNA_scene_types.h"
+#include "DNA_screen_types.h"
+#include "DNA_space_types.h"
+#include "DNA_userdef_types.h"
+#include "DNA_windowmanager_types.h"
+
+#include "BLI_arithb.h"
+#include "BLI_blenlib.h"
+
+#include "BKE_context.h"
+#include "BKE_global.h"
+#include "BKE_utildefines.h"
+
+#include "RNA_access.h"
+#include "RNA_define.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "ED_screen.h"
+
+#include "script_intern.h"
+
+
+/* ************************** registration **********************************/
+
+void script_operatortypes(void)
+{
+       WM_operatortype_append(ED_SCRIPT_OT_run_pyfile);
+}
+
+void script_keymap(wmWindowManager *wm)
+{
+       ListBase *keymap= WM_keymap_listbase(wm, "Script", SPACE_SCRIPT, 0);
+
+       /* TODO - this is just while we have no way to load a text datablock */
+       RNA_string_set(WM_keymap_add_item(keymap, "ED_SCRIPT_OT_run_pyfile", PKEY, KM_PRESS, 0, 0)->ptr, "filename", "test.py");
+}
+
index d9fd4159fb1068b1422455b45641c90a95cd0a4e..52ef016497dc3955016f8df2caad67e6813d38c3 100644 (file)
@@ -159,16 +159,6 @@ static void script_main_area_draw(const bContext *C, ARegion *ar)
        /* scrollers? */
 }
 
-void script_operatortypes(void)
-{
-       
-}
-
-void script_keymap(struct wmWindowManager *wm)
-{
-       
-}
-
 /* add handlers, stuff you only do once or on area/region changes */
 static void script_header_area_init(wmWindowManager *wm, ARegion *ar)
 {
index 158bde600a0dcfbab119e13c581d651621875ab7..d65d1186efb060a2bb58dda715d663bfd08479cf 100644 (file)
@@ -113,5 +113,8 @@ void view3d_keymap(wmWindowManager *wm)
        WM_keymap_add_item(keymap, "ED_VIEW3D_OT_clipping", BKEY, KM_PRESS, KM_ALT, 0);
        WM_keymap_add_item(keymap, "ED_VIEW3D_OT_circle_select", CKEY, KM_PRESS, 0, 0);
 
+       /* TODO - this is just while we have no way to load a text datablock */
+       RNA_string_set(WM_keymap_add_item(keymap, "ED_SCRIPT_OT_run_pyfile", PKEY, KM_PRESS, 0, 0)->ptr, "filename", "test.py");
+
 }
 
index 2c207539c2fe438fda91fb95d648296a5bde84fe..7c2788e8f9bde8dc028432ab7a38a48a0b1ac2c4 100644 (file)
@@ -796,6 +796,16 @@ void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *val
                IDP_AssignString(idprop, (char*)value);
        else if(sprop->set)
                sprop->set(ptr, value);
+       else if(!(prop->flag & PROP_NOT_EDITABLE)) {
+               IDPropertyTemplate val;
+               IDProperty *group;
+
+               val.str= value;
+
+               group= rna_idproperties_get(ptr->type, ptr->data, 1);
+               if(group)
+                       IDP_AddToGroup(group, IDP_New(IDP_STRING, val, (char*)prop->identifier));
+       }
 }
 
 int RNA_property_enum_get(PointerRNA *ptr, PropertyRNA *prop)
index 7c54c728a05f66d33a87485e3f303ead08497a24..2662890f3c8ef172a8a2e19ddd67e05fc9198d3b 100644 (file)
@@ -72,9 +72,9 @@ void BPY_run_python_script( bContext *C, const char *fn )
        PyObject *py_dict, *py_result;
        char pystring[512];
        PyGILState_STATE gilstate;
-       
+
        /* TODO - look into a better way to run a file */
-       sprintf(pystring, "exec(open(r'%s').read())", fn);
+       sprintf(pystring, "exec(open(r'%s').read())", fn);      
        
        BPY_start_python();
        
index d6bae190dd2165287330b627acabb6a459094ce2..b2cda26975145c6ae8143970757b04366009fded 100644 (file)
@@ -93,6 +93,7 @@ static PyObject * pyop_func_call(BPy_OperatorFunc * self, PyObject *args, PyObje
        wmOperatorType *ot;
 
        int error_val = 0;
+       int totkw;
        const char *arg_name= NULL;
        PyObject *item;
        
@@ -118,6 +119,7 @@ static PyObject * pyop_func_call(BPy_OperatorFunc * self, PyObject *args, PyObje
        iterprop= RNA_struct_iterator_property(&ptr);
        RNA_property_collection_begin(&ptr, iterprop, &iter);
 
+       totkw = kw ? PyDict_Size(kw):0;
 
        for(; iter.valid; RNA_property_collection_next(&iter)) {
                prop= iter.ptr.data;
@@ -144,26 +146,39 @@ static PyObject * pyop_func_call(BPy_OperatorFunc * self, PyObject *args, PyObje
                        error_val= 1;
                        break;
                }
+
+               totkw--;
        }
 
        RNA_property_collection_end(&iter);
 
-       if (error_val) {
-               if (properties) {
-                       IDP_FreeProperty(properties);
-                       MEM_freeN(properties);
+       if (error_val==0 && totkw > 0) { /* some keywords were given that were not used :/ */
+               PyObject *key, *value;
+               Py_ssize_t pos = 0;
+
+               while (PyDict_Next(kw, &pos, &key, &value)) {
+                       arg_name= _PyUnicode_AsString(key);
+                       if (RNA_struct_find_property(&ptr, arg_name) == NULL) break;
+                       arg_name= NULL;
                }
 
-               return NULL; 
+               PyErr_Format( PyExc_AttributeError, "argument \"%s\" unrecognized", arg_name ? arg_name : "<UNKNOWN>");
+               error_val = 1;
+       }
+
+       if (error_val==0) {
+               WM_operator_name_call(self->C, self->name, WM_OP_DEFAULT, properties);
        }
-       
-       
-       WM_operator_name_call(self->C, self->name, WM_OP_DEFAULT, properties);
 
        if (properties) {
                IDP_FreeProperty(properties);
                MEM_freeN(properties);
        }
+
+       if (error_val) {
+               return NULL;
+       }
+
        Py_RETURN_NONE;
 }