[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17982] branches/blender2.5/blender/source : wip operator py-api

Campbell Barton ideasman42 at gmail.com
Sun Dec 21 09:53:38 CET 2008


Revision: 17982
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17982
Author:   campbellbarton
Date:     2008-12-21 09:53:36 +0100 (Sun, 21 Dec 2008)

Log Message:
-----------
wip operator py-api
"operator.ED_VIEW3D_OT_viewhome(center=1)" calls the operator, converting keyword args to properties.
Need a way to run scripts in the UI for useful testing.

Still need to deal with operator exceptions and verifying args against operator options. 

Added temporary WM_operatortype_first() to allow python to return a list if available operators, can replace this with something better later (operator iterator?)

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/python/BPY_extern.h
    branches/blender2.5/blender/source/blender/python/SConscript
    branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
    branches/blender2.5/blender/source/blender/windowmanager/WM_api.h
    branches/blender2.5/blender/source/blender/windowmanager/intern/wm_operators.c
    branches/blender2.5/blender/source/creator/creator.c

Added Paths:
-----------
    branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.h
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.c
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator.h

Modified: branches/blender2.5/blender/source/blender/python/BPY_extern.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/BPY_extern.h	2008-12-21 08:45:13 UTC (rev 17981)
+++ branches/blender2.5/blender/source/blender/python/BPY_extern.h	2008-12-21 08:53:36 UTC (rev 17982)
@@ -94,7 +94,7 @@
 	int BPY_menu_do_python( short menutype, int event );
 	int BPY_menu_do_shortcut( short menutype, unsigned short key, unsigned short modifiers );
 	int BPY_menu_invoke( struct BPyMenu *pym, short menutype );
-	void BPY_run_python_script( const char *filename );
+	void BPY_run_python_script( struct bContext *C, const char *filename );
 	int BPY_run_script(struct Script *script);
 	void BPY_free_compiled_text( struct Text *text );
 

Modified: branches/blender2.5/blender/source/blender/python/SConscript
===================================================================
--- branches/blender2.5/blender/source/blender/python/SConscript	2008-12-21 08:45:13 UTC (rev 17981)
+++ branches/blender2.5/blender/source/blender/python/SConscript	2008-12-21 08:53:36 UTC (rev 17982)
@@ -4,7 +4,7 @@
 sources = env.Glob('intern/*.c')
 
 incs = '. ../editors/include ../makesdna ../makesrna ../blenlib ../blenkernel ../nodes'
-incs += ' ../imbuf ../blenloader ../render/extern/include'
+incs += ' ../imbuf ../blenloader ../render/extern/include ../windowmanager'
 incs += ' #intern/guardedalloc #intern/memutil'
 incs += ' ' + env['BF_PYTHON_INC']
 

Added: branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.c	                        (rev 0)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.c	2008-12-21 08:53:36 UTC (rev 17982)
@@ -0,0 +1,182 @@
+/**
+ * $Id: IDProp.c
+ *
+ * ***** 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) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * Contributor(s): Joseph Eagar, Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+ 
+ #include "DNA_ID.h"
+
+#include "BKE_idprop.h"
+
+#include "bpy_idprop.h"
+
+#include "MEM_guardedalloc.h"
+
+#define BSTR_EQ(a, b)	(*(a) == *(b) && !strcmp(a, b))
+
+static PyObject *EXPP_ReturnPyObjError( PyObject * type, char *error_msg )
+{				/* same as above, just to change its name smoothly */
+	PyErr_SetString( type, error_msg );
+	return NULL;
+}
+
+static int EXPP_ReturnIntError( PyObject * type, char *error_msg )
+{
+	PyErr_SetString( type, error_msg );
+	return -1;
+}
+
+ 
+/*returns NULL on success, error string on failure*/
+static char *BPy_IDProperty_Map_ValidateAndCreate(char *name, IDProperty *group, PyObject *ob)
+{
+	IDProperty *prop = NULL;
+	IDPropertyTemplate val = {0};
+	
+	if (PyFloat_Check(ob)) {
+		val.d = PyFloat_AsDouble(ob);
+		prop = IDP_New(IDP_DOUBLE, val, name);
+	} else if (PyLong_Check(ob)) {
+		val.i = (int) PyLong_AsLong(ob);
+		prop = IDP_New(IDP_INT, val, name);
+	} else if (PyUnicode_Check(ob)) {
+		val.str = _PyUnicode_AsString(ob);
+		prop = IDP_New(IDP_STRING, val, name);
+	} else if (PySequence_Check(ob)) {
+		PyObject *item;
+		int i;
+		
+		/*validate sequence and derive type.
+		we assume IDP_INT unless we hit a float
+		number; then we assume it's */
+		val.array.type = IDP_INT;
+		val.array.len = PySequence_Length(ob);
+		for (i=0; i<val.array.len; i++) {
+			item = PySequence_GetItem(ob, i);
+			if (PyFloat_Check(item)) val.array.type = IDP_DOUBLE;
+			else if (!PyLong_Check(item)) return "only floats and ints are allowed in ID property arrays";
+			Py_XDECREF(item);
+		}
+		
+		prop = IDP_New(IDP_ARRAY, val, name);
+		for (i=0; i<val.array.len; i++) {
+			item = PySequence_GetItem(ob, i);
+			if (val.array.type == IDP_INT) {
+				item = PyNumber_Int(item);
+				((int*)prop->data.pointer)[i] = (int)PyLong_AsLong(item);
+			} else {
+				item = PyNumber_Float(item);
+				((double*)prop->data.pointer)[i] = (float)PyFloat_AsDouble(item);
+			}
+			Py_XDECREF(item);
+		}
+	} else if (PyMapping_Check(ob)) {
+		PyObject *keys, *vals, *key, *pval;
+		int i, len;
+		/*yay! we get into recursive stuff now!*/
+		keys = PyMapping_Keys(ob);
+		vals = PyMapping_Values(ob);
+		
+		/*we allocate the group first; if we hit any invalid data,
+		  we can delete it easily enough.*/
+		prop = IDP_New(IDP_GROUP, val, name);
+		len = PyMapping_Length(ob);
+		for (i=0; i<len; i++) {
+			key = PySequence_GetItem(keys, i);
+			pval = PySequence_GetItem(vals, i);
+			if (!PyUnicode_Check(key)) {
+				IDP_FreeProperty(prop);
+				MEM_freeN(prop);
+				Py_XDECREF(keys);
+				Py_XDECREF(vals);
+				Py_XDECREF(key);
+				Py_XDECREF(pval);
+				return "invalid element in subgroup dict template!";
+			}
+			if (BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, pval)) {
+				IDP_FreeProperty(prop);
+				MEM_freeN(prop);
+				Py_XDECREF(keys);
+				Py_XDECREF(vals);
+				Py_XDECREF(key);
+				Py_XDECREF(pval);
+				return "invalid element in subgroup dict template!";
+			}
+			Py_XDECREF(key);
+			Py_XDECREF(pval);
+		}
+		Py_XDECREF(keys);
+		Py_XDECREF(vals);
+	} else return "invalid property value";
+	
+	IDP_ReplaceInGroup(group, prop);
+	return NULL;
+}
+
+
+static int BPy_IDGroup_Map_SetItem(IDProperty *prop, PyObject *key, PyObject *val)
+{
+	char *err;
+	
+	if (prop->type  != IDP_GROUP)
+		return EXPP_ReturnIntError( PyExc_TypeError,
+			"unsubscriptable object");
+			
+	if (!PyUnicode_Check(key))
+		return EXPP_ReturnIntError( PyExc_TypeError,
+		   "only strings are allowed as subgroup keys" );
+
+	if (val == NULL) {
+		IDProperty *pkey = IDP_GetPropertyFromGroup(prop, _PyUnicode_AsString(key));
+		if (pkey) {
+			IDP_RemFromGroup(prop, pkey);
+			IDP_FreeProperty(pkey);
+			MEM_freeN(pkey);
+			return 0;
+		} else return EXPP_ReturnIntError( PyExc_RuntimeError, "property not found in group" );
+	}
+	
+	err = BPy_IDProperty_Map_ValidateAndCreate(_PyUnicode_AsString(key), prop, val);
+	if (err) return EXPP_ReturnIntError( PyExc_RuntimeError, err );
+	
+	return 0;
+}
+
+
+PyObject *BPy_IDGroup_Update(IDProperty *prop, PyObject *value)
+{
+	PyObject *pkey, *pval;
+	Py_ssize_t i=0;
+	
+	if (!PyDict_Check(value))
+		return EXPP_ReturnPyObjError( PyExc_TypeError,
+		   "expected an object derived from dict.");
+		   
+	while (PyDict_Next(value, &i, &pkey, &pval)) {
+		BPy_IDGroup_Map_SetItem(prop, pkey, pval);
+		if (PyErr_Occurred()) return NULL;
+	}
+	
+	Py_RETURN_NONE;
+}
\ No newline at end of file

Added: branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.h
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.h	                        (rev 0)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_idprop.h	2008-12-21 08:53:36 UTC (rev 17982)
@@ -0,0 +1,33 @@
+/**
+ * $Id: IDProp.h
+ *
+ * ***** 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) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * Contributor(s): Joseph Eagar
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include <Python.h>
+
+struct ID;
+struct IDProperty;
+
+PyObject *BPy_IDGroup_Update(IDProperty *prop, PyObject *value);
\ No newline at end of file

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2008-12-21 08:45:13 UTC (rev 17981)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_interface.c	2008-12-21 08:53:36 UTC (rev 17982)
@@ -3,16 +3,19 @@
 #include "compile.h"		/* for the PyCodeObject */
 #include "eval.h"		/* for PyEval_EvalCode */
 
+#include "BKE_context.h"
+
 #include "bpy_compat.h"
 
 #include "bpy_rna.h"
+#include "bpy_operator.h"
 
 
 /*****************************************************************************
 * Description: This function creates a new Python dictionary object.
 *****************************************************************************/
 
-static PyObject *CreateGlobalDictionary( void )
+static PyObject *CreateGlobalDictionary( bContext *C )
 {
 	PyObject *dict = PyDict_New(  );
 	PyObject *item = PyUnicode_FromString( "__main__" );
@@ -28,6 +31,10 @@
 	item = BPY_rna_doc();
 	PyDict_SetItemString( dict, "bpydoc", item );
 	Py_DECREF(item);
+
+	item = BPY_operator_module(C);
+	PyDict_SetItemString( dict, "bpyoperator", item );
+	Py_DECREF(item);
 	
 	return dict;
 }
@@ -60,7 +67,7 @@
 	return;
 }
 
-void BPY_run_python_script( const char *fn )
+void BPY_run_python_script( bContext *C, const char *fn )
 {
 	PyObject *py_dict, *py_result;
 	char pystring[512];
@@ -73,7 +80,7 @@
 	
 	gilstate = PyGILState_Ensure();
 	
-	py_dict = CreateGlobalDictionary();
+	py_dict = CreateGlobalDictionary(C);
 	
 	py_result = PyRun_String( pystring, Py_file_input, py_dict, py_dict );
 	


@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list