added Geometry as a BGE module, removed its dependency on gen_utils.c
authorCampbell Barton <ideasman42@gmail.com>
Thu, 30 Apr 2009 12:45:13 +0000 (12:45 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 30 Apr 2009 12:45:13 +0000 (12:45 +0000)
source/blender/python/api2_2x/Blender.c
source/blender/python/api2_2x/Geometry.c
source/blender/python/api2_2x/Geometry.h
source/gameengine/BlenderRoutines/BL_KetsjiEmbedStart.cpp
source/gameengine/GamePlayer/ghost/GPG_Application.cpp
source/gameengine/Ketsji/CMakeLists.txt
source/gameengine/Ketsji/KX_PythonInit.cpp
source/gameengine/Ketsji/KX_PythonInit.h
source/gameengine/Ketsji/SConscript

index 2e44f0635e5a231979cb59749b1d78d1085f9892..fd316eb484f357bd47eb8d9388892fd70f4cc10e 100644 (file)
@@ -1092,7 +1092,7 @@ void M_Blender_Init(void)
        PyDict_SetItemString(dict, "Mesh", Mesh_Init()); 
        PyDict_SetItemString(dict, "Metaball", Metaball_Init());
        PyDict_SetItemString(dict, "Mathutils", Mathutils_Init("Blender.Mathutils"));
-       PyDict_SetItemString(dict, "Geometry", Geometry_Init());
+       PyDict_SetItemString(dict, "Geometry", Geometry_Init("Blender.Geometry"));
        PyDict_SetItemString(dict, "Modifier", Modifier_Init());
        PyDict_SetItemString(dict, "NMesh", NMesh_Init());
        PyDict_SetItemString(dict, "Node", Node_Init());
index 89c63870d5987a1b2be6f4520c7a412b5c0cfe38..d568472c838e204d335fdf2480b6231263e7a691 100644 (file)
@@ -38,9 +38,6 @@
 #include "BKE_displist.h"
 #include "MEM_guardedalloc.h"
 #include "BLI_blenlib.h"
-
-/* needed for EXPP_ReturnPyObjError and EXPP_check_sequence_consistency */
-#include "gen_utils.h"
  
 #include "BKE_utildefines.h"
 #include "BLI_boxpack2d.h"
@@ -76,13 +73,32 @@ struct PyMethodDef M_Geometry_methods[] = {
        {"BoxPack2D", ( PyCFunction ) M_Geometry_BoxPack2D, METH_O, M_Geometry_BoxPack2D_doc},
        {NULL, NULL, 0, NULL}
 };
+
+#if (PY_VERSION_HEX >= 0x03000000)
+static struct PyModuleDef M_Geometry_module_def = {
+       {}, /* m_base */
+       "Geometry",  /* m_name */
+       M_Geometry_doc,  /* m_doc */
+       0,  /* m_size */
+       M_Geometry_methods,  /* m_methods */
+       0,  /* m_reload */
+       0,  /* m_traverse */
+       0,  /* m_clear */
+       0,  /* m_free */
+};
+#endif
+
 /*----------------------------MODULE INIT-------------------------*/
-PyObject *Geometry_Init(void)
+PyObject *Geometry_Init(const char *from)
 {
        PyObject *submodule;
-
-       submodule = Py_InitModule3("Blender.Geometry",
-                                   M_Geometry_methods, M_Geometry_doc);
+       
+#if (PY_VERSION_HEX >= 0x03000000)
+       submodule = PyModule_Create(&M_Geometry_module_def);
+#else
+       submodule = Py_InitModule3(from, M_Geometry_methods, M_Geometry_doc);
+#endif
+       
        return (submodule);
 }
 
@@ -92,7 +108,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
 {
        PyObject *tri_list; /*return this list of tri's */
        PyObject *polyLine, *polyVec;
-       int i, len_polylines, len_polypoints;
+       int i, len_polylines, len_polypoints, ls_error = 0;
        
        /* display listbase */
        ListBase dispbase={NULL, NULL};
@@ -105,8 +121,8 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
        
        
        if(!PySequence_Check(polyLineSeq)) {
-               return EXPP_ReturnPyObjError( PyExc_TypeError,
-                                             "expected a sequence of poly lines" );
+               PyErr_SetString( PyExc_TypeError, "expected a sequence of poly lines" );
+               return NULL;
        }
        
        len_polylines = PySequence_Size( polyLineSeq );
@@ -116,19 +132,20 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
                if (!PySequence_Check(polyLine)) {
                        freedisplist(&dispbase);
                        Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/
-                       return EXPP_ReturnPyObjError( PyExc_TypeError,
-                                 "One or more of the polylines is not a sequence of Mathutils.Vector's" );
+                       PyErr_SetString( PyExc_TypeError, "One or more of the polylines is not a sequence of Mathutils.Vector's" );
+                       return NULL;
                }
                
                len_polypoints= PySequence_Size( polyLine );
                if (len_polypoints>0) { /* dont bother adding edges as polylines */
+#if 0
                        if (EXPP_check_sequence_consistency( polyLine, &vector_Type ) != 1) {
                                freedisplist(&dispbase);
                                Py_DECREF(polyLine);
-                               return EXPP_ReturnPyObjError( PyExc_TypeError,
-                                         "A point in one of the polylines is not a Mathutils.Vector type" );
+                               PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" );
+                               return NULL;
                        }
-                       
+#endif
                        dl= MEM_callocN(sizeof(DispList), "poly disp");
                        BLI_addtail(&dispbase, dl);
                        dl->type= DL_INDEX3;
@@ -141,13 +158,17 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
                        
                        for( index = 0; index<len_polypoints; ++index, fp+=3) {
                                polyVec= PySequence_GetItem( polyLine, index );
-                               
-                               fp[0] = ((VectorObject *)polyVec)->vec[0];
-                               fp[1] = ((VectorObject *)polyVec)->vec[1];
-                               if( ((VectorObject *)polyVec)->size > 2 )
-                                       fp[2] = ((VectorObject *)polyVec)->vec[2];
-                               else
-                                       fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */
+                               if(VectorObject_Check(polyVec)) {
+                                       fp[0] = ((VectorObject *)polyVec)->vec[0];
+                                       fp[1] = ((VectorObject *)polyVec)->vec[1];
+                                       if( ((VectorObject *)polyVec)->size > 2 )
+                                               fp[2] = ((VectorObject *)polyVec)->vec[2];
+                                       else
+                                               fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */
+                               }
+                               else {
+                                       ls_error= 1;
+                               }
                                
                                totpoints++;
                                Py_DECREF(polyVec);
@@ -156,7 +177,12 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
                Py_DECREF(polyLine);
        }
        
-       if (totpoints) {
+       if(ls_error) {
+               freedisplist(&dispbase); /* possible some dl was allocated */
+               PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" );
+               return NULL;
+       }
+       else if (totpoints) {
                /* now make the list to return */
                filldisplist(&dispbase, &dispbase);
                
@@ -167,8 +193,8 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
                tri_list= PyList_New(dl->parts);
                if( !tri_list ) {
                        freedisplist(&dispbase);
-                       return EXPP_ReturnPyObjError( PyExc_RuntimeError,
-                                       "Geometry.PolyFill failed to make a new list" );
+                       PyErr_SetString( PyExc_RuntimeError, "Geometry.PolyFill failed to make a new list" );
+                       return NULL;
                }
                
                index= 0;
@@ -181,6 +207,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
                freedisplist(&dispbase);
        } else {
                /* no points, do this so scripts dont barf */
+               freedisplist(&dispbase); /* possible some dl was allocated */
                tri_list= PyList_New(0);
        }
        
@@ -197,9 +224,10 @@ static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args )
          &vector_Type, &line_a2,
          &vector_Type, &line_b1,
          &vector_Type, &line_b2)
-       )
-               return ( EXPP_ReturnPyObjError
-                        ( PyExc_TypeError, "expected 4 vector types\n" ) );
+       ) {
+               PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
+               return NULL;
+       }
        
        a1x= line_a1->vec[0];
        a1y= line_a1->vec[1];
@@ -293,10 +321,10 @@ static PyObject *M_Geometry_ClosestPointOnLine( PyObject * self, PyObject * args
        &vector_Type, &pt,
        &vector_Type, &line_1,
        &vector_Type, &line_2)
-         )
-               return ( EXPP_ReturnPyObjError
-                               ( PyExc_TypeError, "expected 3 vector types\n" ) );
-       
+         ) {
+               PyErr_SetString( PyExc_TypeError, "expected 3 vector types\n" );
+               return NULL;
+       }
        /* accept 2d verts */
        if (pt->size==3) { VECCOPY(pt_in, pt->vec);}
        else { pt_in[2]=0.0;    VECCOPY2D(pt_in, pt->vec) }
@@ -325,9 +353,10 @@ static PyObject *M_Geometry_PointInTriangle2D( PyObject * self, PyObject * args
          &vector_Type, &tri_p1,
          &vector_Type, &tri_p2,
          &vector_Type, &tri_p3)
-       )
-               return ( EXPP_ReturnPyObjError
-                        ( PyExc_TypeError, "expected 4 vector types\n" ) );
+       ) {
+               PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
+               return NULL;
+       }
        
        return PyInt_FromLong(IsectPT2Df(pt_vec->vec, tri_p1->vec, tri_p2->vec, tri_p3->vec));
 }
@@ -342,9 +371,10 @@ static PyObject *M_Geometry_PointInQuad2D( PyObject * self, PyObject * args )
          &vector_Type, &quad_p2,
          &vector_Type, &quad_p3,
          &vector_Type, &quad_p4)
-       )
-               return ( EXPP_ReturnPyObjError
-                        ( PyExc_TypeError, "expected 5 vector types\n" ) );
+       ) {
+               PyErr_SetString( PyExc_TypeError, "expected 5 vector types\n" );
+               return NULL;
+       }
        
        return PyInt_FromLong(IsectPQ2Df(pt_vec->vec, quad_p1->vec, quad_p2->vec, quad_p3->vec, quad_p4->vec));
 }
@@ -357,9 +387,10 @@ static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
        
        
        /* Error checking must alredy be done */
-       if( !PyList_Check( value ) )
-               return EXPP_ReturnIntError( PyExc_TypeError,
-                               "can only back a list of [x,y,x,w]" );
+       if( !PyList_Check( value ) ) {
+               PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
+               return -1;
+       }
        
        len = PyList_Size( value );
        
@@ -370,8 +401,8 @@ static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
                list_item = PyList_GET_ITEM( value, i );
                if( !PyList_Check( list_item ) || PyList_Size( list_item ) < 4 ) {
                        MEM_freeN(*boxarray);
-                       return EXPP_ReturnIntError( PyExc_TypeError,
-                                       "can only back a list of [x,y,x,w]" );
+                       PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
+                       return -1;
                }
                
                box = (*boxarray)+i;
@@ -381,8 +412,8 @@ static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
                
                if (!PyNumber_Check(item_1) || !PyNumber_Check(item_2)) {
                        MEM_freeN(*boxarray);
-                       return EXPP_ReturnIntError( PyExc_TypeError,
-                                       "can only back a list of 2d boxes [x,y,x,w]" );
+                       PyErr_SetString( PyExc_TypeError, "can only back a list of 2d boxes [x,y,x,w]" );
+                       return -1;
                }
                
                box->w =  (float)PyFloat_AsDouble( item_1 );
@@ -418,9 +449,10 @@ static PyObject *M_Geometry_BoxPack2D( PyObject * self, PyObject * boxlist )
        int len;
        int error;
        
-       if(!PyList_Check(boxlist))
-               return EXPP_ReturnPyObjError( PyExc_TypeError,
-                                             "expected a sequence of boxes [[x,y,w,h], ... ]" );
+       if(!PyList_Check(boxlist)) {
+               PyErr_SetString( PyExc_TypeError, "expected a sequence of boxes [[x,y,w,h], ... ]" );
+               return NULL;
+       }
        
        len = PyList_Size( boxlist );
        
index e9e365cc9ae34c369a13ca997aae788347b6ce43..ebfb054c54aa367e574c555d314b46e7b8bcf1d7 100644 (file)
@@ -34,6 +34,6 @@
 #include <Python.h>
 #include "Mathutils.h"
 
-PyObject *Geometry_Init( void );
+PyObject *Geometry_Init( const char *from );
 
 #endif                         /* EXPP_Geometry_H */
index 2337ab49ee90e33e43b2a3e0054bd22e48e4cead..641938253b77cebd529364bd89279b1cfabc4d20 100644 (file)
@@ -368,6 +368,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
                        initGameKeys();
                        initPythonConstraintBinding();
                        initMathutils();
+                       initGeometry();
                        initBGL();
 #ifdef WITH_FFMPEG
                        initVideoTexture();
@@ -671,6 +672,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
                        initGameKeys();
                        initPythonConstraintBinding();
                        initMathutils();
+                       initGeometry();
                        initBGL();
 #ifdef WITH_FFMPEG
                        initVideoTexture();
index 907ba99e63ba5c4f1cd9e8c69d289c71b71e1c1e..af8b94857b9d13f717e34b0a83cf26178c696f57 100644 (file)
@@ -693,6 +693,7 @@ bool GPG_Application::startEngine(void)
                initGameKeys();
                initPythonConstraintBinding();
                initMathutils();
+               initGeometry();
                initBGL();
 #ifdef WITH_FFMPEG
         initVideoTexture();
index c4623b5b6fec23edb42a3a587b0075ed92ded717..abc2b44825e073e5dee7c037c43218901df0679c 100644 (file)
@@ -28,6 +28,7 @@ FILE(GLOB SRC *.cpp)
 SET(SRC 
   ${SRC}
   ../../../source/blender/python/api2_2x/Mathutils.c
+  ../../../source/blender/python/api2_2x/Geometry.c
   ../../../source/blender/python/api2_2x/constant.c
   ../../../source/blender/python/api2_2x/euler.c
   ../../../source/blender/python/api2_2x/matrix.c
index b17d4042bb6ffb695c3b72348f6bcb9d1f3e1c5b..f5d32e36fc5e4aef07300d713f03bffff19437fb 100644 (file)
@@ -86,6 +86,7 @@
 
 extern "C" {
        #include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use.
+       #include "Geometry.h" // Blender.Geometry module copied here so the blenderlayer can use.
        #include "bpy_internal_import.h"  /* from the blender python api, but we want to import text too! */
        #include "BGL.h"
 }
@@ -1407,7 +1408,7 @@ PyObject *KXpy_import(PyObject *self, PyObject *args)
        /* quick hack for GamePython modules 
                TODO: register builtin modules properly by ExtendInittab */
        if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") || !strcmp(name, "PhysicsConstraints") ||
-               !strcmp(name, "Rasterizer") || !strcmp(name, "Mathutils") || !strcmp(name, "BGL")) {
+               !strcmp(name, "Rasterizer") || !strcmp(name, "Mathutils") || !strcmp(name, "BGL") || !strcmp(name, "Geometry")) {
                return PyImport_ImportModuleEx(name, globals, locals, fromlist);
        }
        
@@ -1725,6 +1726,7 @@ static void clearGameModules()
        clearModule(modules, "GameKeys");       
        clearModule(modules, "VideoTexture");   
        clearModule(modules, "Mathutils");      
+       clearModule(modules, "Geometry");       
        clearModule(modules, "BGL");    
        PyErr_Clear(); // incase some of these were alredy removed.
 #endif
@@ -2051,6 +2053,11 @@ PyObject* initMathutils()
        return Mathutils_Init("Mathutils"); // Use as a top level module in BGE
 }
 
+PyObject* initGeometry()
+{
+       return Geometry_Init("Geometry"); // Use as a top level module in BGE
+}
+
 PyObject* initBGL()
 {
        return BGL_Init("BGL"); // Use as a top level module in BGE
index 11360197b951627eb3c103c6b4f8167e7754b854..3253ac8f711888960c3341fce8ec575fe05e30f5 100644 (file)
@@ -45,6 +45,7 @@ PyObject*     initGameKeys();
 PyObject*      initRasterizer(class RAS_IRasterizer* rasty,class RAS_ICanvas* canvas);
 PyObject*      initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie, int argc, char** argv);
 PyObject*      initMathutils();
+PyObject*      initGeometry();
 PyObject*      initBGL();
 PyObject*      initVideoTexture(void); 
 void           exitGamePlayerPythonScripting();
index d97f13b07586e67d73ab26e365d57e2711d57802..dd7297080e566e536bf179b55abdf68052e6af39 100644 (file)
@@ -9,6 +9,7 @@ defs = ''
 # Mathutils C files.
 sources.extend([\
        '#source/blender/python/api2_2x/Mathutils.c',\
+       '#source/blender/python/api2_2x/Geometry.c',\
        '#source/blender/python/api2_2x/constant.c',\
        '#source/blender/python/api2_2x/euler.c',\
        '#source/blender/python/api2_2x/matrix.c',\