added Geometry as a BGE module, removed its dependency on gen_utils.c

This commit is contained in:
Campbell Barton 2009-04-30 12:45:13 +00:00
parent b14dc8f3d9
commit fdf6ea916d
9 changed files with 96 additions and 51 deletions

@ -1092,7 +1092,7 @@ void M_Blender_Init(void)
PyDict_SetItemString(dict, "Mesh", Mesh_Init()); PyDict_SetItemString(dict, "Mesh", Mesh_Init());
PyDict_SetItemString(dict, "Metaball", Metaball_Init()); PyDict_SetItemString(dict, "Metaball", Metaball_Init());
PyDict_SetItemString(dict, "Mathutils", Mathutils_Init("Blender.Mathutils")); 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, "Modifier", Modifier_Init());
PyDict_SetItemString(dict, "NMesh", NMesh_Init()); PyDict_SetItemString(dict, "NMesh", NMesh_Init());
PyDict_SetItemString(dict, "Node", Node_Init()); PyDict_SetItemString(dict, "Node", Node_Init());

@ -39,9 +39,6 @@
#include "MEM_guardedalloc.h" #include "MEM_guardedalloc.h"
#include "BLI_blenlib.h" #include "BLI_blenlib.h"
/* needed for EXPP_ReturnPyObjError and EXPP_check_sequence_consistency */
#include "gen_utils.h"
#include "BKE_utildefines.h" #include "BKE_utildefines.h"
#include "BLI_boxpack2d.h" #include "BLI_boxpack2d.h"
#include "BLI_arithb.h" #include "BLI_arithb.h"
@ -76,13 +73,32 @@ struct PyMethodDef M_Geometry_methods[] = {
{"BoxPack2D", ( PyCFunction ) M_Geometry_BoxPack2D, METH_O, M_Geometry_BoxPack2D_doc}, {"BoxPack2D", ( PyCFunction ) M_Geometry_BoxPack2D, METH_O, M_Geometry_BoxPack2D_doc},
{NULL, NULL, 0, NULL} {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-------------------------*/ /*----------------------------MODULE INIT-------------------------*/
PyObject *Geometry_Init(void) PyObject *Geometry_Init(const char *from)
{ {
PyObject *submodule; PyObject *submodule;
submodule = Py_InitModule3("Blender.Geometry", #if (PY_VERSION_HEX >= 0x03000000)
M_Geometry_methods, M_Geometry_doc); submodule = PyModule_Create(&M_Geometry_module_def);
#else
submodule = Py_InitModule3(from, M_Geometry_methods, M_Geometry_doc);
#endif
return (submodule); 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 *tri_list; /*return this list of tri's */
PyObject *polyLine, *polyVec; PyObject *polyLine, *polyVec;
int i, len_polylines, len_polypoints; int i, len_polylines, len_polypoints, ls_error = 0;
/* display listbase */ /* display listbase */
ListBase dispbase={NULL, NULL}; ListBase dispbase={NULL, NULL};
@ -105,8 +121,8 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
if(!PySequence_Check(polyLineSeq)) { if(!PySequence_Check(polyLineSeq)) {
return EXPP_ReturnPyObjError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "expected a sequence of poly lines" );
"expected a sequence of poly lines" ); return NULL;
} }
len_polylines = PySequence_Size( polyLineSeq ); len_polylines = PySequence_Size( polyLineSeq );
@ -116,19 +132,20 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
if (!PySequence_Check(polyLine)) { if (!PySequence_Check(polyLine)) {
freedisplist(&dispbase); freedisplist(&dispbase);
Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/ Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/
return EXPP_ReturnPyObjError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "One or more of the polylines is not a sequence of Mathutils.Vector's" );
"One or more of the polylines is not a sequence of Mathutils.Vector's" ); return NULL;
} }
len_polypoints= PySequence_Size( polyLine ); len_polypoints= PySequence_Size( polyLine );
if (len_polypoints>0) { /* dont bother adding edges as polylines */ if (len_polypoints>0) { /* dont bother adding edges as polylines */
#if 0
if (EXPP_check_sequence_consistency( polyLine, &vector_Type ) != 1) { if (EXPP_check_sequence_consistency( polyLine, &vector_Type ) != 1) {
freedisplist(&dispbase); freedisplist(&dispbase);
Py_DECREF(polyLine); Py_DECREF(polyLine);
return EXPP_ReturnPyObjError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" );
"A point in one of the polylines is not a Mathutils.Vector type" ); return NULL;
} }
#endif
dl= MEM_callocN(sizeof(DispList), "poly disp"); dl= MEM_callocN(sizeof(DispList), "poly disp");
BLI_addtail(&dispbase, dl); BLI_addtail(&dispbase, dl);
dl->type= DL_INDEX3; 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) { for( index = 0; index<len_polypoints; ++index, fp+=3) {
polyVec= PySequence_GetItem( polyLine, index ); polyVec= PySequence_GetItem( polyLine, index );
if(VectorObject_Check(polyVec)) {
fp[0] = ((VectorObject *)polyVec)->vec[0]; fp[0] = ((VectorObject *)polyVec)->vec[0];
fp[1] = ((VectorObject *)polyVec)->vec[1]; fp[1] = ((VectorObject *)polyVec)->vec[1];
if( ((VectorObject *)polyVec)->size > 2 ) if( ((VectorObject *)polyVec)->size > 2 )
fp[2] = ((VectorObject *)polyVec)->vec[2]; fp[2] = ((VectorObject *)polyVec)->vec[2];
else else
fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */ fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */
}
else {
ls_error= 1;
}
totpoints++; totpoints++;
Py_DECREF(polyVec); Py_DECREF(polyVec);
@ -156,7 +177,12 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
Py_DECREF(polyLine); 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 */ /* now make the list to return */
filldisplist(&dispbase, &dispbase); filldisplist(&dispbase, &dispbase);
@ -167,8 +193,8 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
tri_list= PyList_New(dl->parts); tri_list= PyList_New(dl->parts);
if( !tri_list ) { if( !tri_list ) {
freedisplist(&dispbase); freedisplist(&dispbase);
return EXPP_ReturnPyObjError( PyExc_RuntimeError, PyErr_SetString( PyExc_RuntimeError, "Geometry.PolyFill failed to make a new list" );
"Geometry.PolyFill failed to make a new list" ); return NULL;
} }
index= 0; index= 0;
@ -181,6 +207,7 @@ static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
freedisplist(&dispbase); freedisplist(&dispbase);
} else { } else {
/* no points, do this so scripts dont barf */ /* no points, do this so scripts dont barf */
freedisplist(&dispbase); /* possible some dl was allocated */
tri_list= PyList_New(0); 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_a2,
&vector_Type, &line_b1, &vector_Type, &line_b1,
&vector_Type, &line_b2) &vector_Type, &line_b2)
) ) {
return ( EXPP_ReturnPyObjError PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
( PyExc_TypeError, "expected 4 vector types\n" ) ); return NULL;
}
a1x= line_a1->vec[0]; a1x= line_a1->vec[0];
a1y= line_a1->vec[1]; a1y= line_a1->vec[1];
@ -293,10 +321,10 @@ static PyObject *M_Geometry_ClosestPointOnLine( PyObject * self, PyObject * args
&vector_Type, &pt, &vector_Type, &pt,
&vector_Type, &line_1, &vector_Type, &line_1,
&vector_Type, &line_2) &vector_Type, &line_2)
) ) {
return ( EXPP_ReturnPyObjError PyErr_SetString( PyExc_TypeError, "expected 3 vector types\n" );
( PyExc_TypeError, "expected 3 vector types\n" ) ); return NULL;
}
/* accept 2d verts */ /* accept 2d verts */
if (pt->size==3) { VECCOPY(pt_in, pt->vec);} if (pt->size==3) { VECCOPY(pt_in, pt->vec);}
else { pt_in[2]=0.0; VECCOPY2D(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_p1,
&vector_Type, &tri_p2, &vector_Type, &tri_p2,
&vector_Type, &tri_p3) &vector_Type, &tri_p3)
) ) {
return ( EXPP_ReturnPyObjError PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
( 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)); 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_p2,
&vector_Type, &quad_p3, &vector_Type, &quad_p3,
&vector_Type, &quad_p4) &vector_Type, &quad_p4)
) ) {
return ( EXPP_ReturnPyObjError PyErr_SetString( PyExc_TypeError, "expected 5 vector types\n" );
( 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)); 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 */ /* Error checking must alredy be done */
if( !PyList_Check( value ) ) if( !PyList_Check( value ) ) {
return EXPP_ReturnIntError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
"can only back a list of [x,y,x,w]" ); return -1;
}
len = PyList_Size( value ); len = PyList_Size( value );
@ -370,8 +401,8 @@ static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
list_item = PyList_GET_ITEM( value, i ); list_item = PyList_GET_ITEM( value, i );
if( !PyList_Check( list_item ) || PyList_Size( list_item ) < 4 ) { if( !PyList_Check( list_item ) || PyList_Size( list_item ) < 4 ) {
MEM_freeN(*boxarray); MEM_freeN(*boxarray);
return EXPP_ReturnIntError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
"can only back a list of [x,y,x,w]" ); return -1;
} }
box = (*boxarray)+i; box = (*boxarray)+i;
@ -381,8 +412,8 @@ static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
if (!PyNumber_Check(item_1) || !PyNumber_Check(item_2)) { if (!PyNumber_Check(item_1) || !PyNumber_Check(item_2)) {
MEM_freeN(*boxarray); MEM_freeN(*boxarray);
return EXPP_ReturnIntError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "can only back a list of 2d boxes [x,y,x,w]" );
"can only back a list of 2d boxes [x,y,x,w]" ); return -1;
} }
box->w = (float)PyFloat_AsDouble( item_1 ); box->w = (float)PyFloat_AsDouble( item_1 );
@ -418,9 +449,10 @@ static PyObject *M_Geometry_BoxPack2D( PyObject * self, PyObject * boxlist )
int len; int len;
int error; int error;
if(!PyList_Check(boxlist)) if(!PyList_Check(boxlist)) {
return EXPP_ReturnPyObjError( PyExc_TypeError, PyErr_SetString( PyExc_TypeError, "expected a sequence of boxes [[x,y,w,h], ... ]" );
"expected a sequence of boxes [[x,y,w,h], ... ]" ); return NULL;
}
len = PyList_Size( boxlist ); len = PyList_Size( boxlist );

@ -34,6 +34,6 @@
#include <Python.h> #include <Python.h>
#include "Mathutils.h" #include "Mathutils.h"
PyObject *Geometry_Init( void ); PyObject *Geometry_Init( const char *from );
#endif /* EXPP_Geometry_H */ #endif /* EXPP_Geometry_H */

@ -368,6 +368,7 @@ extern "C" void StartKetsjiShell(struct ScrArea *area,
initGameKeys(); initGameKeys();
initPythonConstraintBinding(); initPythonConstraintBinding();
initMathutils(); initMathutils();
initGeometry();
initBGL(); initBGL();
#ifdef WITH_FFMPEG #ifdef WITH_FFMPEG
initVideoTexture(); initVideoTexture();
@ -671,6 +672,7 @@ extern "C" void StartKetsjiShellSimulation(struct ScrArea *area,
initGameKeys(); initGameKeys();
initPythonConstraintBinding(); initPythonConstraintBinding();
initMathutils(); initMathutils();
initGeometry();
initBGL(); initBGL();
#ifdef WITH_FFMPEG #ifdef WITH_FFMPEG
initVideoTexture(); initVideoTexture();

@ -693,6 +693,7 @@ bool GPG_Application::startEngine(void)
initGameKeys(); initGameKeys();
initPythonConstraintBinding(); initPythonConstraintBinding();
initMathutils(); initMathutils();
initGeometry();
initBGL(); initBGL();
#ifdef WITH_FFMPEG #ifdef WITH_FFMPEG
initVideoTexture(); initVideoTexture();

@ -28,6 +28,7 @@ FILE(GLOB SRC *.cpp)
SET(SRC SET(SRC
${SRC} ${SRC}
../../../source/blender/python/api2_2x/Mathutils.c ../../../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/constant.c
../../../source/blender/python/api2_2x/euler.c ../../../source/blender/python/api2_2x/euler.c
../../../source/blender/python/api2_2x/matrix.c ../../../source/blender/python/api2_2x/matrix.c

@ -86,6 +86,7 @@
extern "C" { extern "C" {
#include "Mathutils.h" // Blender.Mathutils module copied here so the blenderlayer can use. #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 "bpy_internal_import.h" /* from the blender python api, but we want to import text too! */
#include "BGL.h" #include "BGL.h"
} }
@ -1407,7 +1408,7 @@ PyObject *KXpy_import(PyObject *self, PyObject *args)
/* quick hack for GamePython modules /* quick hack for GamePython modules
TODO: register builtin modules properly by ExtendInittab */ TODO: register builtin modules properly by ExtendInittab */
if (!strcmp(name, "GameLogic") || !strcmp(name, "GameKeys") || !strcmp(name, "PhysicsConstraints") || 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); return PyImport_ImportModuleEx(name, globals, locals, fromlist);
} }
@ -1725,6 +1726,7 @@ static void clearGameModules()
clearModule(modules, "GameKeys"); clearModule(modules, "GameKeys");
clearModule(modules, "VideoTexture"); clearModule(modules, "VideoTexture");
clearModule(modules, "Mathutils"); clearModule(modules, "Mathutils");
clearModule(modules, "Geometry");
clearModule(modules, "BGL"); clearModule(modules, "BGL");
PyErr_Clear(); // incase some of these were alredy removed. PyErr_Clear(); // incase some of these were alredy removed.
#endif #endif
@ -2051,6 +2053,11 @@ PyObject* initMathutils()
return Mathutils_Init("Mathutils"); // Use as a top level module in BGE 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() PyObject* initBGL()
{ {
return BGL_Init("BGL"); // Use as a top level module in BGE return BGL_Init("BGL"); // Use as a top level module in BGE

@ -45,6 +45,7 @@ PyObject* initGameKeys();
PyObject* initRasterizer(class RAS_IRasterizer* rasty,class RAS_ICanvas* canvas); 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* initGamePlayerPythonScripting(const STR_String& progname, TPythonSecurityLevel level, struct Main *maggie, int argc, char** argv);
PyObject* initMathutils(); PyObject* initMathutils();
PyObject* initGeometry();
PyObject* initBGL(); PyObject* initBGL();
PyObject* initVideoTexture(void); PyObject* initVideoTexture(void);
void exitGamePlayerPythonScripting(); void exitGamePlayerPythonScripting();

@ -9,6 +9,7 @@ defs = ''
# Mathutils C files. # Mathutils C files.
sources.extend([\ sources.extend([\
'#source/blender/python/api2_2x/Mathutils.c',\ '#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/constant.c',\
'#source/blender/python/api2_2x/euler.c',\ '#source/blender/python/api2_2x/euler.c',\
'#source/blender/python/api2_2x/matrix.c',\ '#source/blender/python/api2_2x/matrix.c',\