* Updated the naming convention for some functions. I forgot those the last

time.
* Created the M_ObjectCheckPyObject and M_ObjectFromPyObject functions. These
  are needed for interfacing to other modules.
* The Object methods have been created. Not all of them are implemented yet.
This commit is contained in:
2003-05-17 13:46:15 +00:00
parent d03323b1c3
commit 612123e8fd
5 changed files with 495 additions and 57 deletions

View File

@@ -201,12 +201,12 @@ PyObject *Blender_ReleaseGlobalDict(PyObject *self, PyObject *args)
/*****************************************************************************/
/* Function: initBlender */
/*****************************************************************************/
void initBlender (void)
void M_Blender_Init (void)
{
PyObject * module;
PyObject * dict;
printf ("In initBlender()\n");
printf ("In M_Blender_Init()\n");
g_blenderdict = NULL;
/* TODO: create a docstring for the Blender module */
@@ -214,7 +214,7 @@ void initBlender (void)
dict = PyModule_GetDict (module);
g_blenderdict = dict;
PyDict_SetItemString (dict, "Object", initObject());
PyDict_SetItemString (dict, "Object", M_Object_Init());
PyDict_SetItemString (dict, "NMesh", M_NMesh_Init());
PyDict_SetItemString (dict, "Camera", M_Camera_Init());
PyDict_SetItemString (dict, "Lamp", M_Lamp_Init());

View File

@@ -51,7 +51,7 @@ void initBlenderApi2_2x (void)
{
printf ("initBlenderApi2_2x\n");
g_blenderdict = NULL;
initBlender ();
M_Blender_Init ();
}
ScriptLink * setScriptLinks(ID *id, short event)
@@ -72,7 +72,7 @@ ScriptLink * setScriptLinks(ID *id, short event)
{
return NULL;
}
link = ObjectCreatePyObject (object);
link = M_ObjectCreatePyObject (object);
scriptlink = &(object->scriptlink);
break;
case ID_LA:

View File

@@ -38,7 +38,7 @@
PyObject *M_Object_New(PyObject *self, PyObject *args)
{
struct Object * object;
C_BlenObject * blen_object;
C_Object * blen_object;
int type;
char name[32];
@@ -170,7 +170,7 @@ PyObject *M_Object_New(PyObject *self, PyObject *args)
G.totobj++;
/* Create a Python object from it. */
blen_object = (C_BlenObject*)PyObject_NEW (C_BlenObject, &object_type);
blen_object = (C_Object*)PyObject_NEW (C_Object, &object_type);
blen_object->object = object;
return ((PyObject*)blen_object);
@@ -191,7 +191,7 @@ PyObject *M_Object_Get(PyObject *self, PyObject *args)
if (name != NULL)
{
C_BlenObject * blen_object;
C_Object * blen_object;
object = GetObjectByName (name);
@@ -201,7 +201,7 @@ PyObject *M_Object_Get(PyObject *self, PyObject *args)
return (PythonReturnErrorObject (PyExc_AttributeError,
"Unknown object specified."));
}
blen_object = (C_BlenObject*)PyObject_NEW (C_BlenObject, &object_type);
blen_object = (C_Object*)PyObject_NEW (C_Object, &object_type);
blen_object->object = object;
return ((PyObject*)blen_object);
@@ -247,7 +247,7 @@ PyObject *M_Object_Get(PyObject *self, PyObject *args)
/*****************************************************************************/
PyObject *M_Object_GetSelected (PyObject *self, PyObject *args)
{
C_BlenObject * blen_object;
C_Object * blen_object;
PyObject * list;
Base * base_iter;
@@ -259,7 +259,7 @@ PyObject *M_Object_GetSelected (PyObject *self, PyObject *args)
(G.scene->basact->lay & G.vd->lay)))
{
/* Active object is first in the list. */
blen_object = (C_BlenObject*)PyObject_NEW (C_BlenObject, &object_type);
blen_object = (C_Object*)PyObject_NEW (C_Object, &object_type);
if (blen_object == NULL)
{
Py_DECREF (list);
@@ -277,8 +277,7 @@ PyObject *M_Object_GetSelected (PyObject *self, PyObject *args)
(G.scene->basact->lay & G.vd->lay)) &&
(base_iter != G.scene->basact))
{
blen_object = (C_BlenObject*)PyObject_NEW (C_BlenObject,
&object_type);
blen_object = (C_Object*)PyObject_NEW (C_Object, &object_type);
if (blen_object == NULL)
{
Py_DECREF (list);
@@ -296,7 +295,7 @@ PyObject *M_Object_GetSelected (PyObject *self, PyObject *args)
/*****************************************************************************/
/* Function: initObject */
/*****************************************************************************/
PyObject *initObject (void)
PyObject *M_Object_Init (void)
{
PyObject * module;
@@ -308,17 +307,322 @@ PyObject *initObject (void)
}
/*****************************************************************************/
/* Function: ObjectCreatePyObject */
/* Python C_Camera methods: */
/*****************************************************************************/
static PyObject *Object_clrParent (C_Object *self, PyObject *args)
{
int mode=0;
int fast=0;
Object * parent;
if (!PyArg_ParseTuple (args, "|ii", &mode, &fast))
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"expected one or two integers as arguments"));
}
parent = self->object->parent;
self->object->parent = 0;
if (mode == 2)
{
/* Keep transform */
apply_obmat (self->object);
}
if (!fast)
{
sort_baselist (G.scene);
}
return (Py_None);
}
static PyObject *Object_getData (C_Object *self)
{
PyObject * data_object;
int obj_id;
ID * id;
/* If there's no data associated to the Object, then there's nothing to */
/* return. */
if (self->object->data == NULL)
{
Py_INCREF (Py_None);
return (Py_None);
}
id = (ID*)self->object;
obj_id = MAKE_ID2 (id->name[0], id->name[1]);
switch (obj_id)
{
case ID_CA:
case ID_CU:
case ID_IM:
case ID_IP:
case ID_LA:
case ID_MA:
case ID_ME:
case ID_OB:
data_object = M_ObjectCreatePyObject (self->object->data);
Py_INCREF (data_object);
return (data_object);
case ID_SCE:
case ID_TXT:
case ID_WO:
default:
Py_INCREF (Py_None);
return (Py_None);
}
return (Py_None);
}
static PyObject *Object_getDeformData (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getDeltaLocation (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getDrawMode (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getDrawType (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getEuler (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getInverseMatrix (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getLocation (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_getMaterials (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getMatrix (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getParent (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getTracked (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_getType (C_Object *self)
{
return (Py_None);
}
static PyObject *Object_link (C_Object *self, PyObject *args)
{
PyObject * py_data;
ID * id;
ID * oldid;
int obj_id;
void * data = NULL;
if (!PyArg_ParseTuple (args, "O", &py_data))
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"expected an object as argument"));
}
/* TODO: remove the Object type here, add the correct functions when */
/* ready. */
if (M_ObjectCheckPyObject (py_data))
data = (void*) M_ObjectFromPyObject (py_data);
oldid = (ID*) self->object->data;
id = (ID*) data;
obj_id = MAKE_ID2 (id->name[0], id->name[1]);
switch (obj_id)
{
case ID_ME:
if (self->object->type != OB_MESH)
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"The 'link' object is incompatible with the base object"));
}
break;
case ID_CA:
if (self->object->type != OB_CAMERA)
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"The 'link' object is incompatible with the base object"));
}
break;
case ID_LA:
if (self->object->type != OB_LAMP)
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"The 'link' object is incompatible with the base object"));
}
break;
default:
return (PythonReturnErrorObject (PyExc_AttributeError,
"Linking this object type is not supported"));
}
self->object->data = data;
id_us_plus (id);
if (oldid)
{
if (id->us > 0)
{
id->us--;
}
else
{
return (PythonReturnErrorObject (PyExc_RuntimeError,
"old object reference count below 0"));
}
}
return (Py_None);
}
static PyObject *Object_makeParent (C_Object *self, PyObject *args)
{
PyObject * list;
PyObject * py_child;
Object * child;
Object * parent;
int noninverse;
int fast;
int i;
/* Check if the arguments passed to makeParent are valid. */
if (!PyArg_ParseTuple (args, "O|ii", &list, &noninverse, &fast))
{
return (PythonReturnErrorObject (PyExc_AttributeError,
"expected a list of objects and one or two integers as arguments"));
}
if (!PySequence_Check (list))
{
return (PythonReturnErrorObject (PyExc_TypeError,
"expected a list of objects"));
}
/* Check if the PyObject passed in list is a Blender object. */
for (i=0 ; i<PySequence_Length (list) ; i++)
{
child = NULL;
py_child = PySequence_GetItem (list, i);
if (M_ObjectCheckPyObject (py_child))
child = (Object*) M_ObjectFromPyObject (py_child);
if (child == NULL)
{
return (PythonReturnErrorObject (PyExc_TypeError,
"Object Type expected"));
}
parent = (Object*)self->object;
if (test_parent_loop (parent, child))
{
return (PythonReturnErrorObject (PyExc_RuntimeError,
"parenting loop detected - parenting failed"));
}
child->partype = PAROBJECT;
child->parent = parent;
if (noninverse == 1)
{
/* Parent inverse = unity */
child->loc[0] = 0.0;
child->loc[1] = 0.0;
child->loc[2] = 0.0;
}
else
{
what_does_parent (child);
Mat4Invert (child->parentinv, parent->obmat);
}
if (!fast)
{
sort_baselist (G.scene);
}
/* We don't need the child object anymore. */
Py_DECREF ((PyObject *) child);
}
return (Py_None);
}
static PyObject *Object_materialUsage (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setDeltaLocation (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setDrawMode (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setDrawType (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setEuler (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setLocation (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_setMaterials (C_Object *self, PyObject *args)
{
return (Py_None);
}
static PyObject *Object_shareFrom (C_Object *self, PyObject *args)
{
return (Py_None);
}
/*****************************************************************************/
/* Function: M_ObjectCreatePyObject */
/* Description: This function will create a new BlenObject from an existing */
/* Object structure. */
/*****************************************************************************/
PyObject* ObjectCreatePyObject (struct Object *obj)
PyObject* M_ObjectCreatePyObject (struct Object *obj)
{
C_BlenObject * blen_object;
C_Object * blen_object;
printf ("In ObjectCreatePyObject\n");
printf ("In M_ObjectCreatePyObject\n");
blen_object = (C_BlenObject*)PyObject_NEW (C_BlenObject, &object_type);
blen_object = (C_Object*)PyObject_NEW (C_Object, &object_type);
if (blen_object == NULL)
{
@@ -328,12 +632,35 @@ PyObject* ObjectCreatePyObject (struct Object *obj)
return ((PyObject*)blen_object);
}
/*****************************************************************************/
/* Function: M_ObjectCheckPyObject */
/* Description: This function returns true when the given PyObject is of the */
/* type Object. Otherwise it will return false. */
/*****************************************************************************/
int M_ObjectCheckPyObject (PyObject *py_obj)
{
return (py_obj->ob_type == &object_type);
}
/*****************************************************************************/
/* Function: M_ObjectFromPyObject */
/* Description: This function returns the Blender object from the given */
/* PyObject. */
/*****************************************************************************/
struct Object* M_ObjectFromPyObject (PyObject *py_obj)
{
C_Object * blen_obj;
blen_obj = (C_Object*)py_obj;
return (blen_obj->object);
}
/*****************************************************************************/
/* Function: ObjectDeAlloc */
/* Description: This is a callback function for the BlenObject type. It is */
/* the destructor function. */
/*****************************************************************************/
void ObjectDeAlloc (C_BlenObject *obj)
static void ObjectDeAlloc (C_Object *obj)
{
PyObject_DEL (obj);
}
@@ -344,7 +671,7 @@ void ObjectDeAlloc (C_BlenObject *obj)
/* the function that retrieves any value from Blender and */
/* passes it to Python. */
/*****************************************************************************/
PyObject* ObjectGetAttr (C_BlenObject *obj, char *name)
static PyObject* ObjectGetAttr (C_Object *obj, char *name)
{
struct Object * object;
struct Ika * ika;
@@ -427,14 +754,11 @@ PyObject* ObjectGetAttr (C_BlenObject *obj, char *name)
if (StringEqual (name, "Layer"))
return (PyInt_FromLong(object->lay));
if (StringEqual (name, "parent"))
return (ObjectCreatePyObject (object->parent));
return (M_ObjectCreatePyObject (object->parent));
if (StringEqual (name, "track"))
return (ObjectCreatePyObject (object->track));
return (M_ObjectCreatePyObject (object->track));
if (StringEqual (name, "data"))
{
printf ("This is not implemented yet.\n");
return (Py_None);
}
return (Object_getData (obj));
if (StringEqual (name, "ipo"))
{
printf ("This is not implemented yet.\n");
@@ -467,7 +791,7 @@ PyObject* ObjectGetAttr (C_BlenObject *obj, char *name)
/* the function that retrieves any value from Python and sets */
/* it accordingly in Blender. */
/*****************************************************************************/
int ObjectSetAttr (C_BlenObject *obj, char *name, PyObject *value)
static int ObjectSetAttr (C_Object *obj, char *name, PyObject *value)
{
struct Object * object;
struct Ika * ika;
@@ -598,3 +922,23 @@ int ObjectSetAttr (C_BlenObject *obj, char *name, PyObject *value)
return (0);
}
/*****************************************************************************/
/* Function: ObjectPrint */
/* Description: This is a callback function for the C_Object type. It */
/* builds a meaninful string to 'print' object objects. */
/*****************************************************************************/
static int ObjectPrint(C_Object *self, FILE *fp, int flags)
{
fprintf(fp, "[Object \"%s\"]", self->object->id.name+2);
return 0;
}
/*****************************************************************************/
/* Function: ObjectRepr */
/* Description: This is a callback function for the C_Object type. It */
/* builds a meaninful string to represent object objects. */
/*****************************************************************************/
static PyObject *ObjectRepr (C_Object *self)
{
return PyString_FromString(self->object->id.name+2);
}

View File

@@ -35,11 +35,13 @@
#include <Python.h>
#include <stdio.h>
#include <BDR_editobject.h>
#include <BKE_global.h>
#include <BKE_library.h>
#include <BKE_main.h>
#include <BKE_mesh.h>
#include <BKE_object.h>
#include <BKE_scene.h>
#include <BLI_arithb.h>
#include <BLI_blenlib.h>
#include <DNA_ID.h>
@@ -51,6 +53,7 @@
#include <DNA_view3d_types.h>
#include "gen_utils.h"
#include "modules.h"
/*****************************************************************************/
/* Python API function prototypes for the Blender module. */
@@ -82,23 +85,15 @@ char M_Object_GetSelected_doc[] =
The active object is the first in the list, if visible";
/*****************************************************************************/
/* Python BlenderObject structure definition. */
/* Python C_Object structure definition. */
/*****************************************************************************/
typedef struct {
PyObject_HEAD
PyObject *dict;
struct Object *object;
} C_BlenObject;
} C_Object;
/*****************************************************************************/
/* PythonTypeObject callback function prototypes */
/*****************************************************************************/
void ObjectDeAlloc (C_BlenObject *obj);
PyObject* ObjectGetAttr (C_BlenObject *obj, char *name);
int ObjectSetAttr (C_BlenObject *obj, char *name, PyObject *v);
/*****************************************************************************/
/* Python method structure definition. */
/* Python method structure definition for Blender.Object module: */
/*****************************************************************************/
struct PyMethodDef M_Object_methods[] = {
{"New", (PyCFunction)M_Object_New, METH_VARARGS,
@@ -112,6 +107,98 @@ struct PyMethodDef M_Object_methods[] = {
{NULL, NULL, 0, NULL}
};
/*****************************************************************************/
/* Python C_Object methods declarations: */
/*****************************************************************************/
static PyObject *Object_clrParent (C_Object *self, PyObject *args);
static PyObject *Object_getData (C_Object *self);
static PyObject *Object_getDeformData (C_Object *self);
static PyObject *Object_getDeltaLocation (C_Object *self);
static PyObject *Object_getDrawMode (C_Object *self);
static PyObject *Object_getDrawType (C_Object *self);
static PyObject *Object_getEuler (C_Object *self);
static PyObject *Object_getInverseMatrix (C_Object *self);
static PyObject *Object_getLocation (C_Object *self, PyObject *args);
static PyObject *Object_getMaterials (C_Object *self);
static PyObject *Object_getMatrix (C_Object *self);
static PyObject *Object_getParent (C_Object *self);
static PyObject *Object_getTracked (C_Object *self);
static PyObject *Object_getType (C_Object *self);
static PyObject *Object_link (C_Object *self, PyObject *args);
static PyObject *Object_makeParent (C_Object *self, PyObject *args);
static PyObject *Object_materialUsage (C_Object *self, PyObject *args);
static PyObject *Object_setDeltaLocation (C_Object *self, PyObject *args);
static PyObject *Object_setDrawMode (C_Object *self, PyObject *args);
static PyObject *Object_setDrawType (C_Object *self, PyObject *args);
static PyObject *Object_setEuler (C_Object *self, PyObject *args);
static PyObject *Object_setLocation (C_Object *self, PyObject *args);
static PyObject *Object_setMaterials (C_Object *self, PyObject *args);
static PyObject *Object_shareFrom (C_Object *self, PyObject *args);
/*****************************************************************************/
/* Python C_Object methods table: */
/*****************************************************************************/
static PyMethodDef C_Object_methods[] = {
/* name, method, flags, doc */
{"clrParent", (PyCFunction)Object_clrParent, METH_VARARGS,
"(x) - "},
{"getData", (PyCFunction)Object_getData, METH_NOARGS,
"(x) - "},
{"getDeformData", (PyCFunction)Object_getDeformData, METH_NOARGS,
"(x) - "},
{"getDeltaLocation", (PyCFunction)Object_getDeltaLocation, METH_NOARGS,
"(x) - "},
{"getDrawMode", (PyCFunction)Object_getDrawMode, METH_NOARGS,
"(x) - "},
{"getDrawType", (PyCFunction)Object_getDrawType, METH_NOARGS,
"(x) - "},
{"getEuler", (PyCFunction)Object_getEuler, METH_NOARGS,
"(x) - "},
{"getInverseMatrix", (PyCFunction)Object_getInverseMatrix, METH_NOARGS,
"(x) - "},
{"getLocation", (PyCFunction)Object_getLocation, METH_VARARGS,
"(x) - "},
{"getMaterials", (PyCFunction)Object_getMaterials, METH_NOARGS,
"(x) - "},
{"getMatrix", (PyCFunction)Object_getMatrix, METH_NOARGS,
"(x) - "},
{"getParent", (PyCFunction)Object_getParent, METH_NOARGS,
"(x) - "},
{"getTracked", (PyCFunction)Object_getTracked, METH_NOARGS,
"(x) - "},
{"getType", (PyCFunction)Object_getType, METH_NOARGS,
"(x) - "},
{"link", (PyCFunction)Object_link, METH_VARARGS,
"(x) - "},
{"makeParent", (PyCFunction)Object_makeParent, METH_VARARGS,
"(x) - "},
{"materialUsage", (PyCFunction)Object_materialUsage, METH_VARARGS,
"(x) - "},
{"setDeltaLocation", (PyCFunction)Object_setDeltaLocation, METH_VARARGS,
"(x) - "},
{"setDrawMode", (PyCFunction)Object_setDrawMode, METH_VARARGS,
"(x) - "},
{"setDrawType", (PyCFunction)Object_setDrawType, METH_VARARGS,
"(x) - "},
{"setEuler", (PyCFunction)Object_setEuler, METH_VARARGS,
"(x) - "},
{"setLocation", (PyCFunction)Object_setLocation, METH_VARARGS,
"(x) - "},
{"setMaterials", (PyCFunction)Object_setMaterials, METH_VARARGS,
"(x) - "},
{"shareFrom", (PyCFunction)Object_shareFrom, METH_VARARGS,
"(x) - "},
};
/*****************************************************************************/
/* PythonTypeObject callback function prototypes */
/*****************************************************************************/
static void ObjectDeAlloc (C_Object *obj);
static int ObjectPrint (C_Object *obj, FILE *fp, int flags);
static PyObject* ObjectGetAttr (C_Object *obj, char *name);
static int ObjectSetAttr (C_Object *obj, char *name, PyObject *v);
static PyObject* ObjectRepr (C_Object *obj);
/*****************************************************************************/
/* Python TypeObject structure definition. */
/*****************************************************************************/
@@ -119,20 +206,25 @@ static PyTypeObject object_type =
{
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"Object", /* tp_name */
sizeof (C_BlenObject), /* tp_basicsize */
"Object", /* tp_name */
sizeof (C_Object), /* tp_basicsize */
0, /* tp_itemsize */
/* methods */
(destructor)ObjectDeAlloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc)ObjectGetAttr, /* tp_getattr */
(setattrfunc)ObjectSetAttr, /* tp_setattr */
(printfunc)ObjectPrint, /* tp_print */
(getattrfunc)ObjectGetAttr, /* tp_getattr */
(setattrfunc)ObjectSetAttr, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
(reprfunc)ObjectRepr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_as_hash */
0,0,0,0,0,0,
0, /* tp_doc */
0,0,0,0,0,0,
C_Object_methods, /* tp_methods */
0, /* tp_members */
};
#endif /* EXPP_OBJECT_H */

View File

@@ -40,15 +40,17 @@
/*****************************************************************************/
extern PyObject *g_blenderdict;
void initBlender (void);
PyObject *initObject (void);
PyObject *ObjectCreatePyObject (struct Object *obj);
PyObject *M_NMesh_Init (void);
PyObject *M_Camera_Init (void);
PyObject *M_Lamp_Init (void);
PyObject *M_Curve_Init (void);
PyObject *M_Image_Init (void);
PyObject *M_Window_Init (void);
PyObject *M_Draw_Init (void);
PyObject *M_BGL_Init (void);
PyObject *M_Text_Init (void);
void M_Blender_Init (void);
PyObject * M_Object_Init (void);
PyObject * M_ObjectCreatePyObject (struct Object *obj);
int M_ObjectCheckPyObject (PyObject *py_obj);
struct Object * M_ObjectFromPyObject (PyObject *py_obj);
PyObject * M_NMesh_Init (void);
PyObject * M_Camera_Init (void);
PyObject * M_Lamp_Init (void);
PyObject * M_Curve_Init (void);
PyObject * M_Image_Init (void);
PyObject * M_Window_Init (void);
PyObject * M_Draw_Init (void);
PyObject * M_BGL_Init (void);
PyObject * M_Text_Init (void);