[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [11924] branches/pyapi_devel/source/ blender/python: update this cranch from pyapi changes in trunk

Campbell Barton cbarton at metavr.com
Sun Sep 2 23:03:59 CEST 2007


Revision: 11924
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11924
Author:   campbellbarton
Date:     2007-09-02 23:03:59 +0200 (Sun, 02 Sep 2007)

Log Message:
-----------
update this cranch from pyapi changes in trunk

Modified Paths:
--------------
    branches/pyapi_devel/source/blender/python/BPY_interface.c
    branches/pyapi_devel/source/blender/python/api2_2x/Armature.c
    branches/pyapi_devel/source/blender/python/api2_2x/Bone.c
    branches/pyapi_devel/source/blender/python/api2_2x/Draw.c
    branches/pyapi_devel/source/blender/python/api2_2x/Effect.c
    branches/pyapi_devel/source/blender/python/api2_2x/Mesh.c
    branches/pyapi_devel/source/blender/python/api2_2x/NLA.c
    branches/pyapi_devel/source/blender/python/api2_2x/Object.c
    branches/pyapi_devel/source/blender/python/api2_2x/bpy.c
    branches/pyapi_devel/source/blender/python/api2_2x/bpy.h

Modified: branches/pyapi_devel/source/blender/python/BPY_interface.c
===================================================================
--- branches/pyapi_devel/source/blender/python/BPY_interface.c	2007-09-02 19:32:22 UTC (rev 11923)
+++ branches/pyapi_devel/source/blender/python/BPY_interface.c	2007-09-02 21:03:59 UTC (rev 11924)
@@ -31,6 +31,8 @@
  * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */
 
+/* TODO - replace g_blenderdict - its null now! */
+
 #include <Python.h>
 
 #include "compile.h"		/* for the PyCodeObject */
@@ -45,6 +47,7 @@
 #include "BKE_library.h"
 #include "BKE_object.h"		/* during_scriptlink() */
 #include "BKE_text.h"
+#include "BKE_constraint.h" /* for bConstraintOb */
 
 #include "DNA_curve_types.h" /* for struct IpoDriver */
 #include "DNA_ID.h" /* ipo driver */
@@ -64,6 +67,7 @@
 #include "api2_2x/gen_library.h" /* GetPyObjectFromID */
 #include "api2_2x/BGL.h" 
 #include "api2_2x/Blender.h"
+#include "api2_2x/bpy.h"
 #include "api2_2x/Camera.h"
 #include "api2_2x/Draw.h"
 #include "api2_2x/Object.h"
@@ -307,7 +311,12 @@
 
 	//init a global dictionary
 	g_blenderdict = NULL;
-
+	g_bpydict = NULL;
+	g_bpystatedict = NULL;
+	
+	/* Init the module here! */
+	m_bpy_init();
+	
 	//Look for a python installation
 	init_syspath( first_time ); /* not first_time: some msgs are suppressed */
 	
@@ -395,7 +404,7 @@
 
 	path = Py_BuildValue( "s", bprogname );
 
-	mod = PyImport_ImportModule( "Blender.sys" );
+	mod = PyImport_ImportModule( "bpy.app" );
 
 	if( mod ) {
 		d = PyModule_GetDict( mod );
@@ -1145,9 +1154,9 @@
 
 	PyDict_SetItemString(d, "__builtins__", PyEval_GetBuiltins());
 
-	mod = PyImport_ImportModule("Blender");
+	mod = PyImport_ImportModule("bpy");
 	if (mod) {
-		PyDict_SetItemString(d, "Blender", mod);
+		PyDict_SetItemString(d, "bpy", mod);
 		PyDict_SetItemString(d, "b", mod);
 		Py_DECREF(mod);
 	}
@@ -1159,7 +1168,7 @@
 		Py_DECREF(mod);
 	}
 
-	mod = PyImport_ImportModule("Blender.Noise");
+	mod = PyImport_ImportModule("bpy.math.noise");
 	if (mod) {
 		PyDict_SetItemString(d, "noise", mod);
 		PyDict_SetItemString(d, "n", mod);
@@ -1189,7 +1198,8 @@
 			Py_DECREF(fcn);
 		}
 	}
-
+	
+	/* TODO - change these */
 	/* me(meshname) == Blender.Mesh.Get(meshname) */
 	mod = PyImport_ImportModule("Blender.Mesh");
 	if (mod) {
@@ -1256,7 +1266,9 @@
 	return 0;
 }
 
-/* This evals py constraints. It is passed all the arguments the normal constraints recieve */
+/* PyConstraints Evaluation Function (only called from evaluate_constraint)
+ * This function is responsible for modifying the ownermat that it is passed. 
+ */
 void BPY_pyconstraint_eval(bPythonConstraint *con, float ownermat[][4], float targetmat[][4])
 {
 	PyObject *srcmat, *tarmat, *idprop;
@@ -1383,6 +1395,110 @@
 	Py_XDECREF( retval );
 }
 
+/* PyConstraints 'Driver' Function
+ * This function is responsible for running any code that requires full access to the owner and the target
+ * It should be used sparringly, and only for doing 'hacks' which are not possible any other way.
+ */
+void BPY_pyconstraint_driver(bPythonConstraint *con, bConstraintOb *cob, Object *target, char subtarget[])
+{
+	PyObject *owner, *subowner, *tar, *subtar; 
+	PyObject *idprop;
+	PyObject *globals, *gval;
+	PyObject *pyargs, *retval;
+	
+	if ( !con->text ) return;
+	if ( con->flag & PYCON_SCRIPTERROR) return;
+	
+	globals = CreateGlobalDictionary();
+	
+	owner = Object_CreatePyObject( cob->ob );
+	subowner = PyPoseBone_FromPosechannel( cob->pchan );
+	
+	tar = Object_CreatePyObject( target );
+	if ( (target) && (target->type==OB_ARMATURE) ) {
+		bPoseChannel *pchan;
+		pchan = get_pose_channel( target->pose, subtarget );
+		subtar = PyPoseBone_FromPosechannel( pchan );
+	}
+	else
+		subtar = PyString_FromString(subtarget);
+	
+	idprop = IDProperty_CreatePyObject( NULL, con->prop, NULL); 
+	
+/*  since I can't remember what the armature weakrefs do, I'll just leave this here
+    commented out.  This function was based on pydrivers, and it might still be relevent.
+	if( !setup_armature_weakrefs()){
+		fprintf( stderr, "Oops - weakref dict setup\n");
+		return result;
+	}
+*/
+	retval = RunPython( con->text, globals );
+
+	if ( retval == NULL ) {
+		BPY_Err_Handle(con->text->id.name);
+		ReleaseGlobalDictionary( globals );
+		con->flag |= PYCON_SCRIPTERROR;
+	
+		/* free temp objects */
+		Py_XDECREF( idprop );
+		Py_XDECREF( owner );
+		Py_XDECREF( subowner );
+		Py_XDECREF( tar );
+		Py_XDECREF( subtar );
+		return;
+	}
+
+	if (retval) {Py_XDECREF( retval );}
+	retval = NULL;
+	
+	gval = PyDict_GetItemString(globals, "doDriver");
+	if (!gval) {
+		ReleaseGlobalDictionary( globals );
+	
+		/* free temp objects */
+		Py_XDECREF( idprop );
+		Py_XDECREF( owner );
+		Py_XDECREF( subowner );
+		Py_XDECREF( tar );
+		Py_XDECREF( subtar );
+		return;
+	}
+	
+	/* Now for the fun part! Try and find the functions we need. */
+	if (PyFunction_Check(gval) ) {
+		pyargs = Py_BuildValue("OOOOO", owner, subowner, tar, subtar, idprop);
+		retval = PyObject_CallObject(gval, pyargs);
+		Py_XDECREF( pyargs );
+	} else {
+		printf("ERROR: doDriver is supposed to be a function!\n");
+		con->flag |= PYCON_SCRIPTERROR;
+		ReleaseGlobalDictionary( globals );
+		
+		Py_XDECREF( idprop );
+		Py_XDECREF( owner );
+		Py_XDECREF( subowner );
+		Py_XDECREF( tar );
+		Py_XDECREF( subtar );
+		return;
+	}
+	
+	/* an error occurred while running the function? */
+	if (!retval) {
+		BPY_Err_Handle(con->text->id.name);
+		con->flag |= PYCON_SCRIPTERROR;
+	}
+	
+	/* clear globals */
+	ReleaseGlobalDictionary( globals );
+	
+	/* free temp objects */
+	Py_XDECREF( idprop );
+	Py_XDECREF( owner );
+	Py_XDECREF( subowner );
+	Py_XDECREF( tar );
+	Py_XDECREF( subtar );
+}
+
 /* This evaluates whether constraint uses targets, and also the target matrix 
  * Return code of 0 = doesn't use targets, 1 = uses targets + matrix set, -1 = uses targets + matrix not set
  */
@@ -1394,7 +1510,6 @@
 	PyObject *gval, *gval2;
 	PyObject *pyargs, *retval;
 	BPyMatrixObject *retmat;
-	bPoseChannel *pchan;
 	int row, col;
 	
 	if ( !con->text ) return 0;
@@ -1403,11 +1518,13 @@
 	globals = CreateGlobalDictionary();
 	
 	tar = Object_CreatePyObject( con->tar );
-	if ( con->tar )
+	if ( (con->tar) && (con->tar->type==OB_ARMATURE) ) {
+		bPoseChannel *pchan;
 		pchan = get_pose_channel( con->tar->pose, con->subtarget );
+		subtar = PyPoseBone_FromPosechannel( pchan );
+	}
 	else
-		pchan = NULL;
-	subtar = PyPoseBone_FromPosechannel( pchan );
+		subtar = PyString_FromString(con->subtarget);
 	
 	tarmat = Matrix_CreatePyObject( (float*)targetmat, 4, 4, (PyObject *)NULL );
 	idprop = IDProperty_CreatePyObject( NULL, con->prop, NULL);
@@ -1897,13 +2014,11 @@
 		disable_where_scriptlink( (short)during_slink );
 
 		/* set globals in Blender module to identify scriptlink */
-		EXPP_dict_set_item_str( g_blenderdict, "bylink", Py_True ); Py_INCREF(Py_True);
-		EXPP_dict_set_item_str( g_blenderdict, "link",
-				      GetPyObjectFromID( id ) );
-		EXPP_dict_set_item_str( g_blenderdict, "event",
+		PyDict_SetItemString(	g_bpystatedict, "bylink", Py_True);
+		EXPP_dict_set_item_str( g_bpystatedict, "link", GetPyObjectFromID( id ) );
+		EXPP_dict_set_item_str( g_bpystatedict, "event",
 				      PyString_FromString( event_to_name
 							   ( event ) ) );
-
 		if (event == SCRIPT_POSTRENDER) event = SCRIPT_RENDER;
 
 		for( index = 0; index < scriptlink->totscript; index++ ) {
@@ -1939,10 +2054,9 @@
 		/* cleanup bylink flag and clear link so PyObject
 		 * can be released 
 		 */
-		PyDict_SetItemString(g_blenderdict, "bylink", Py_False);
-		PyDict_SetItemString( g_blenderdict, "link", Py_None );
-		EXPP_dict_set_item_str( g_blenderdict, "event",
-				      PyString_FromString( "" ) );
+		PyDict_SetItemString(g_bpystatedict, "bylink", Py_False);
+		PyDict_SetItemString( g_bpystatedict, "link", Py_None );
+		EXPP_dict_set_item_str( g_bpystatedict, "event", PyString_FromString( "" ) );
 	}
 }
 
@@ -2118,12 +2232,11 @@
 		}
 
 		/* set globals in Blender module to identify space handler scriptlink */
-		EXPP_dict_set_item_str(g_blenderdict, "bylink", Py_True); Py_INCREF(Py_True);
+		PyDict_SetItemString(g_bpystatedict, "bylink", Py_True);
 		/* unlike normal scriptlinks, here Blender.link is int (space event type) */
-		EXPP_dict_set_item_str(g_blenderdict, "link", PyInt_FromLong(space_event));
+		EXPP_dict_set_item_str(g_bpystatedict, "link", PyInt_FromLong(space_event));
 		/* note: DRAW space_events set event to 0 */
-		EXPP_dict_set_item_str(g_blenderdict, "event", PyInt_FromLong(event));
-
+		EXPP_dict_set_item_str(g_bpystatedict, "event", PyInt_FromLong(event));
 		/* now run all assigned space handlers for this space and space_event */
 		for( index = 0; index < scriptlink->totscript; index++ ) {
 
@@ -2152,7 +2265,7 @@
 					 * if the script sets Blender.event to None it accepted it;
 					 * otherwise the space's event handling callback that called us
 					 * can go on processing the event */
-					if (event && (PyDict_GetItemString(g_blenderdict,"event") == Py_None))
+					if (event && (PyDict_GetItemString(g_bpystatedict,"event") == Py_None))
 						retval = 1; /* event was swallowed */
 				}
 
@@ -2177,10 +2290,9 @@
 			}
 
 		}
-
-		PyDict_SetItemString(g_blenderdict, "bylink", Py_False);
-		PyDict_SetItemString(g_blenderdict, "link", Py_None );
-		EXPP_dict_set_item_str(g_blenderdict, "event", PyString_FromString(""));
+		PyDict_SetItemString(g_bpystatedict, "bylink", Py_False);
+		PyDict_SetItemString(g_bpystatedict, "link", Py_None );
+		EXPP_dict_set_item_str(g_bpystatedict, "event", PyString_FromString(""));
 	}
 	
 	/* retval:

Modified: branches/pyapi_devel/source/blender/python/api2_2x/Armature.c
===================================================================
--- branches/pyapi_devel/source/blender/python/api2_2x/Armature.c	2007-09-02 19:32:22 UTC (rev 11923)
+++ branches/pyapi_devel/source/blender/python/api2_2x/Armature.c	2007-09-02 21:03:59 UTC (rev 11924)
@@ -1089,6 +1089,9 @@
 		Py_RETURN_NONE;
 	}
 	PySequence_DelItem(armlist, i);
+	
+	/*PyObject_Print(	armlist, stderr, 0);*/
+	
 	Py_RETURN_NONE;
 }
 /*-----------------(internal)
@@ -1100,7 +1103,28 @@
 	PyObject *maindict = NULL, *weakref = NULL;
 	PyObject *armlist = NULL;  /* list of armature weak refs */
 	char *list_name = ARM_WEAKREF_LIST_NAME;
+	int i;
 
+	//put a weakreference in __main__
+	maindict= PyModule_GetDict(PyImport_AddModule(	"__main__"));
+
+	armlist = PyDict_GetItemString(maindict, list_name);
+	if(!armlist) {

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list