diff options
-rw-r--r-- | source/blender/python/api2_2x/Armature.c | 394 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Armature.h | 168 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Blender.c | 3 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Bone.c | 731 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Bone.h | 54 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Curve.c | 237 | ||||
-rw-r--r-- | source/blender/python/api2_2x/Curve.h | 128 | ||||
-rw-r--r-- | source/blender/python/api2_2x/NMesh.c | 58 | ||||
-rw-r--r-- | source/blender/python/api2_2x/NMesh.h | 7 | ||||
-rw-r--r-- | source/blender/python/api2_2x/modules.h | 12 |
10 files changed, 1647 insertions, 145 deletions
diff --git a/source/blender/python/api2_2x/Armature.c b/source/blender/python/api2_2x/Armature.c new file mode 100644 index 00000000000..800124ddc7e --- /dev/null +++ b/source/blender/python/api2_2x/Armature.c @@ -0,0 +1,394 @@ +/* + * + * ***** BEGIN GPL/BL DUAL 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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. + * + * This is a new part of Blender. + * + * Contributor(s): Jordi Rovira i Bonet + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** + */ + +#include "Armature.h" +#include "Bone.h" + +/*****************************************************************************/ +/* Function: M_Armature_New */ +/* Python equivalent: Blender.Armature.New */ +/*****************************************************************************/ +static PyObject *M_Armature_New(PyObject *self, PyObject *args, + PyObject *keywords) +{ + char *type_str = "Armature"; + char *name_str = "ArmatureData"; + static char *kwlist[] = {"type_str", "name_str", NULL}; + C_Armature *py_armature; /* for Armature Data object wrapper in Python */ + bArmature *bl_armature; /* for actual Armature Data we create in Blender */ + char buf[21]; + + printf ("In Armature_New()\n"); + + if (!PyArg_ParseTupleAndKeywords(args, keywords, "|ss", kwlist, + &type_str, &name_str)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string(s) or empty argument")); + + bl_armature = add_armature(); /* first create in Blender */ + if (bl_armature) /* now create the wrapper obj in Python */ + py_armature = (C_Armature *)PyObject_NEW(C_Armature, &Armature_Type); + else + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't create Armature Data in Blender")); + + if (py_armature == NULL) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create Armature Data object")); + + /* link Python armature wrapper with Blender Armature: */ + py_armature->armature = bl_armature; + + if (strcmp(name_str, "ArmatureData") == 0) + return (PyObject *)py_armature; + else { /* user gave us a name for the armature, use it */ + PyOS_snprintf(buf, sizeof(buf), "%s", name_str); + rename_id(&bl_armature->id, buf); + } + + return (PyObject *)py_armature; +} + +/*****************************************************************************/ +/* Function: M_Armature_Get */ +/* Python equivalent: Blender.Armature.Get */ +/*****************************************************************************/ +static PyObject *M_Armature_Get(PyObject *self, PyObject *args) +{ + char *name = NULL; + bArmature *armature_iter; + C_Armature *wanted_armature; + + printf ("In Armature_Get()\n"); + if (!PyArg_ParseTuple(args, "|s", &name)) + return (EXPP_ReturnPyObjError (PyExc_TypeError, + "expected string argument (or nothing)")); + + armature_iter = G.main->armature.first; + + /* Use the name to search for the armature requested. */ + + if (name) { /* (name) - Search armature by name */ + wanted_armature = NULL; + + while ((armature_iter) && (wanted_armature == NULL)) { + + if (strcmp (name, armature_iter->id.name+2) == 0) { + wanted_armature = (C_Armature *)PyObject_NEW(C_Armature, &Armature_Type); + if (wanted_armature) wanted_armature->armature = armature_iter; + } + + armature_iter = armature_iter->id.next; + } + + if (wanted_armature == NULL) {/* Requested Armature doesn't exist */ + char error_msg[64]; + PyOS_snprintf(error_msg, sizeof(error_msg), + "Armature \"%s\" not found", name); + return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg)); + } + + return (PyObject*)wanted_armature; + } + + else + { + /* Return a list of all armatures in the scene */ + int index = 0; + PyObject *armlist, *pystr; + + armlist = PyList_New (BLI_countlist (&(G.main->armature))); + + if (armlist == NULL) + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyList")); + + while (armature_iter) { + pystr = PyString_FromString (armature_iter->id.name+2); + + if (!pystr) + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString")); + + PyList_SET_ITEM (armlist, index, pystr); + + armature_iter = armature_iter->id.next; + index++; + } + + return (armlist); + } + +} + +/*****************************************************************************/ +/* Function: M_Armature_Init */ +/*****************************************************************************/ +PyObject *M_Armature_Init (void) +{ + PyObject *submodule; + PyObject *dict; + + printf ("In M_Armature_Init()\n"); + + submodule = Py_InitModule3("Blender.Armature", + M_Armature_methods, M_Armature_doc); + + /* Add the Bone submodule to this module */ + dict = PyModule_GetDict (submodule); + PyDict_SetItemString (dict, "Bone", M_Bone_Init()); + + return (submodule); +} + +/*****************************************************************************/ +/* Python C_Armature methods: */ +/*****************************************************************************/ +static PyObject *Armature_getName(C_Armature *self) +{ + PyObject *attr = PyString_FromString(self->armature->id.name+2); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Armature.name attribute")); +} + + +/** Create and return a list of the root bones for this armature. */ +static PyObject *Armature_getBones(C_Armature *self) +{ + int totbones = 0; + PyObject *listbones = NULL; + Bone* current = NULL; + int i; + + /* Count the number of bones to create the list */ + current = self->armature->bonebase.first; + for (;current; current=current->next) totbones++; + + /* Create a list with a bone wrapper for each bone */ + current = self->armature->bonebase.first; + listbones = PyList_New(totbones); + for (i=0; i<totbones; i++) { + /* Wrap and set to corresponding element of the list. */ + PyList_SetItem(listbones, i, M_BoneCreatePyObject(current) ); + current = current->next; + } + + return listbones; +} + + +static PyObject *Armature_setName(C_Armature *self, PyObject *args) +{ + char *name; + char buf[21]; + + if (!PyArg_ParseTuple(args, "s", &name)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string argument")); + + PyOS_snprintf(buf, sizeof(buf), "%s", name); + + rename_id(&self->armature->id, buf); + + Py_INCREF(Py_None); + return Py_None; +} + +/* + static PyObject *Armature_setBones(C_Armature *self, PyObject *args) + { + // TODO: Implement me! + printf("ERROR: Armature_setBones NOT implemented yet!\n"); + Py_INCREF(Py_None); + return Py_None; + + } +*/ + +/*****************************************************************************/ +/* Function: ArmatureDeAlloc */ +/* Description: This is a callback function for the C_Armature type. It is */ +/* the destructor function. */ +/*****************************************************************************/ +static void ArmatureDeAlloc (C_Armature *self) +{ + PyObject_DEL (self); +} + +/*****************************************************************************/ +/* Function: ArmatureGetAttr */ +/* Description: This is a callback function for the C_Armature type. It is */ +/* the function that accesses C_Armature member variables and */ +/* methods. */ +/*****************************************************************************/ +static PyObject* ArmatureGetAttr (C_Armature *self, char *name) +{ + PyObject *attr = Py_None; + + if (strcmp(name, "name") == 0) + attr = Armature_getName(self); + if (strcmp(name, "bones") == 0) + attr = Armature_getBones(self); + else if (strcmp(name, "__members__") == 0) { + /* 2 entries */ + attr = Py_BuildValue("[s,s]", + "name", "bones"); + } + + if (!attr) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyObject")); + + if (attr != Py_None) return attr; /* member attribute found, return it */ + + /* not an attribute, search the methods table */ + return Py_FindMethod(C_Armature_methods, (PyObject *)self, name); +} + +/*****************************************************************************/ +/* Function: ArmatureSetAttr */ +/* Description: This is a callback function for the C_Armature type. It is */ +/* the function that changes Armature Data members values. If */ +/* this data is linked to a Blender Armature, it also gets */ +/* updated. */ +/*****************************************************************************/ +static int ArmatureSetAttr (C_Armature *self, char *name, PyObject *value) +{ + PyObject *valtuple; + PyObject *error = NULL; + + valtuple = Py_BuildValue("(N)", value); /*the set* functions expect a tuple*/ + + if (!valtuple) + return EXPP_ReturnIntError(PyExc_MemoryError, + "ArmatureSetAttr: couldn't create tuple"); + + if (strcmp (name, "name") == 0) + error = Armature_setName (self, valtuple); + /* if (strcmp (name, "bones") == 0) + error = Armature_setBones (self, valtuple);*/ + else { /* Error */ + Py_DECREF(valtuple); + + /* ... member with the given name was found */ + return (EXPP_ReturnIntError (PyExc_KeyError, + "attribute not found")); + } + + Py_DECREF(valtuple); + + if (error != Py_None) return -1; + + Py_DECREF(Py_None); /* was incref'ed by the called Armature_set* function */ + return 0; /* normal exit */ +} + +/*****************************************************************************/ +/* Function: ArmaturePrint */ +/* Description: This is a callback function for the C_Armature type. It */ +/* builds a meaninful string to 'print' armature objects. */ +/*****************************************************************************/ +static int ArmaturePrint(C_Armature *self, FILE *fp, int flags) +{ + fprintf(fp, "[Armature \"%s\"]", self->armature->id.name+2); + return 0; +} + +/*****************************************************************************/ +/* Function: ArmatureRepr */ +/* Description: This is a callback function for the C_Armature type. It */ +/* builds a meaninful string to represent armature objects. */ +/*****************************************************************************/ +static PyObject *ArmatureRepr (C_Armature *self) +{ + return PyString_FromString(self->armature->id.name+2); +} + +/*****************************************************************************/ +/* Function: ArmatureCmp */ +/* Description: This is a callback function for the C_Armature type. It */ +/* compares the two armatures: translate comparison to the */ +/* C pointers. */ +/*****************************************************************************/ +static int ArmatureCmp (C_Armature *a, C_Armature *b) +{ + if (a<b) return -1; + else if (a==b) return 0; + else return 1; +} + + +/*****************************************************************************/ +/* Function: M_ArmatureCreatePyObject */ +/* Description: This function will create a new BlenArmature from an */ +/* existing Armature structure. */ +/*****************************************************************************/ +PyObject* M_ArmatureCreatePyObject (struct bArmature *obj) +{ + C_Armature * blen_armature; + + printf ("In M_ArmatureCreatePyObject\n"); + + blen_armature = (C_Armature*)PyObject_NEW (C_Armature, &Armature_Type); + + if (blen_armature == NULL) + { + return (NULL); + } + blen_armature->armature = obj; + return ((PyObject*)blen_armature); +} + +/*****************************************************************************/ +/* Function: M_ArmatureCheckPyObject */ +/* Description: This function returns true when the given PyObject is of the */ +/* type Armature. Otherwise it will return false. */ +/*****************************************************************************/ +int M_ArmatureCheckPyObject (PyObject *py_obj) +{ + return (py_obj->ob_type == &Armature_Type); +} + +/*****************************************************************************/ +/* Function: M_ArmatureFromPyObject */ +/* Description: This function returns the Blender armature from the given */ +/* PyObject. */ +/*****************************************************************************/ +struct bArmature* M_ArmatureFromPyObject (PyObject *py_obj) +{ + C_Armature * blen_obj; + + blen_obj = (C_Armature*)py_obj; + return (blen_obj->armature); +} diff --git a/source/blender/python/api2_2x/Armature.h b/source/blender/python/api2_2x/Armature.h new file mode 100644 index 00000000000..17a3a4d36b2 --- /dev/null +++ b/source/blender/python/api2_2x/Armature.h @@ -0,0 +1,168 @@ +/* + * + * ***** BEGIN GPL/BL DUAL 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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. + * + * This is a new part of Blender. + * + * Contributor(s): Jordi Rovira i Bonet + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#ifndef EXPP_ARMATURE_H +#define EXPP_ARMATURE_H + +#include <Python.h> +#include <stdio.h> + +#include <BKE_main.h> +#include <BKE_global.h> +#include <BKE_object.h> +#include <BKE_armature.h> +#include <BKE_library.h> +#include <BLI_blenlib.h> +#include <DNA_armature_types.h> + +#include "constant.h" +#include "gen_utils.h" +#include "modules.h" + + +/*****************************************************************************/ +/* Python API function prototypes for the Armature module. */ +/*****************************************************************************/ +static PyObject *M_Armature_New (PyObject *self, PyObject *args, + PyObject *keywords); +static PyObject *M_Armature_Get (PyObject *self, PyObject *args); +PyObject *M_Armature_Init (void); + +/*****************************************************************************/ +/* The following string definitions are used for documentation strings. */ +/* In Python these will be written to the console when doing a */ +/* Blender.Armature.__doc__ */ +/*****************************************************************************/ +char M_Armature_doc[] = +"The Blender Armature module\n\n\ +This module provides control over **Armature Data** objects in Blender.\n"; + +char M_Armature_New_doc[] = +"(name) - return a new Armature datablock of \n\ + optional name 'name'."; + +char M_Armature_Get_doc[] = +"(name) - return the armature with the name 'name', \ +returns None if not found.\n If 'name' is not specified, \ +it returns a list of all armatures in the\ncurrent scene."; + +char M_Armature_get_doc[] = +"(name) - DEPRECATED. Use 'Get' instead. \ +return the armature with the name 'name', \ +returns None if not found.\n If 'name' is not specified, \ +it returns a list of all armatures in the\ncurrent scene."; + +/*****************************************************************************/ +/* Python method structure definition for Blender.Armature module: */ +/*****************************************************************************/ +struct PyMethodDef M_Armature_methods[] = { + {"New",(PyCFunction)M_Armature_New, METH_VARARGS|METH_KEYWORDS, + M_Armature_New_doc}, + {"Get", M_Armature_Get, METH_VARARGS, M_Armature_Get_doc}, + {"get", M_Armature_Get, METH_VARARGS, M_Armature_get_doc}, + {NULL, NULL, 0, NULL} +}; + +/*****************************************************************************/ +/* Python C_Armature structure definition: */ +/*****************************************************************************/ +typedef struct { + PyObject_HEAD + bArmature *armature; +} C_Armature; + +/*****************************************************************************/ +/* Python C_Armature methods declarations: */ +/*****************************************************************************/ +static PyObject *Armature_getName(C_Armature *self); +static PyObject *Armature_getBones(C_Armature *self); +static PyObject *Armature_setName(C_Armature *self, PyObject *args); +//static PyObject *Armature_setBones(C_Armature *self, PyObject *args); + +/*****************************************************************************/ +/* Python C_Armature methods table: */ +/*****************************************************************************/ +static PyMethodDef C_Armature_methods[] = { + /* name, method, flags, doc */ + {"getName", (PyCFunction)Armature_getName, METH_NOARGS, + "() - return Armature name"}, + {"getBones", (PyCFunction)Armature_getBones, METH_NOARGS, + "() - return Armature root bones"}, + {"setName", (PyCFunction)Armature_setName, METH_VARARGS, + "(str) - rename Armature"}, + /* {"setBones", (PyCFunction)Armature_setBones, METH_VARARGS, + "(list of bones) - replace the whole bone list of the armature"}, + */ + {0} +}; + +/*****************************************************************************/ +/* Python TypeArmature callback function prototypes: */ +/*****************************************************************************/ +static void ArmatureDeAlloc (C_Armature *armature); +static PyObject *ArmatureGetAttr (C_Armature *armature, char *name); +static int ArmatureSetAttr (C_Armature *armature, char *name, PyObject *v); +static int ArmatureCmp (C_Armature *a1, C_Armature *a2); +static PyObject *ArmatureRepr (C_Armature *armature); +static int ArmaturePrint (C_Armature *armature, FILE *fp, int flags); + +/*****************************************************************************/ +/* Python TypeArmature structure definition: */ +/*****************************************************************************/ +static PyTypeObject Armature_Type = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "Armature", /* tp_name */ + sizeof (C_Armature), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)ArmatureDeAlloc, /* tp_dealloc */ + (printfunc)ArmaturePrint, /* tp_print */ + (getattrfunc)ArmatureGetAttr, /* tp_getattr */ + (setattrfunc)ArmatureSetAttr, /* tp_setattr */ + (cmpfunc)ArmatureCmp, /* tp_compare */ + (reprfunc)ArmatureRepr, /* 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_Armature_methods, /* tp_methods */ + 0, /* tp_members */ +}; + + + + +#endif /* EXPP_ARMATURE_H */ diff --git a/source/blender/python/api2_2x/Blender.c b/source/blender/python/api2_2x/Blender.c index bce6f5130fe..7f475238d1d 100644 --- a/source/blender/python/api2_2x/Blender.c +++ b/source/blender/python/api2_2x/Blender.c @@ -221,11 +221,10 @@ void M_Blender_Init (void) PyDict_SetItemString (dict, "Camera", M_Camera_Init()); PyDict_SetItemString (dict, "Lamp", M_Lamp_Init()); PyDict_SetItemString (dict, "Curve", M_Curve_Init()); + PyDict_SetItemString (dict, "Armature", M_Armature_Init()); PyDict_SetItemString (dict, "Image", M_Image_Init()); PyDict_SetItemString (dict, "Window", M_Window_Init()); PyDict_SetItemString (dict, "Draw", M_Draw_Init()); PyDict_SetItemString (dict, "BGL", M_BGL_Init()); PyDict_SetItemString (dict, "Text", M_Text_Init()); -/* PyDict_SetItemString (dict, "Effect", M_Effect_Init()); */ } - diff --git a/source/blender/python/api2_2x/Bone.c b/source/blender/python/api2_2x/Bone.c new file mode 100644 index 00000000000..ca9b6764220 --- /dev/null +++ b/source/blender/python/api2_2x/Bone.c @@ -0,0 +1,731 @@ +/* + * + * ***** BEGIN GPL/BL DUAL 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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. + * + * This is a new part of Blender. + * + * Contributor(s): Jordi Rovira i Bonet + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#include "Bone.h" + +#include <BKE_main.h> +#include <BKE_global.h> +#include <BKE_object.h> +#include <BKE_armature.h> +#include <BKE_library.h> + +#include "constant.h" +#include "gen_utils.h" +#include "modules.h" + + +/*****************************************************************************/ +/* Python API function prototypes for the Bone module. */ +/*****************************************************************************/ +static PyObject *M_Bone_New(PyObject *self, PyObject *args, PyObject *keywords); + + +/*****************************************************************************/ +/* The following string definitions are used for documentation strings. */ +/* In Python these will be written to the console when doing a */ +/* Blender.Armature.Bone.__doc__ */ +/*****************************************************************************/ +char M_Bone_doc[] = +"The Blender Bone module\n\n\ +This module provides control over **Bone Data** objects in Blender.\n\n\ +Example::\n\n\ + from Blender import Armature.Bone\n\ + l = Armature.Bone.New()\n"; + +char M_Bone_New_doc[] = +"(name) - return a new Bone of name 'name'."; + + +/*****************************************************************************/ +/* Python method structure definition for Blender.Armature.Bone module: */ +/*****************************************************************************/ +struct PyMethodDef M_Bone_methods[] = { + {"New",(PyCFunction)M_Bone_New, METH_VARARGS|METH_KEYWORDS,M_Bone_New_doc}, + {NULL, NULL, 0, NULL} +}; + +/*****************************************************************************/ +/* Python C_Bone methods declarations: */ +/*****************************************************************************/ +static PyObject *Bone_getName(C_Bone *self); +static PyObject *Bone_getRoll(C_Bone *self); +static PyObject *Bone_getHead(C_Bone *self); +static PyObject *Bone_getTail(C_Bone *self); +static PyObject *Bone_getLoc(C_Bone *self); +static PyObject *Bone_getSize(C_Bone *self); +static PyObject *Bone_getQuat(C_Bone *self); +static PyObject *Bone_getParent(C_Bone *self); +static PyObject *Bone_hasParent(C_Bone *self); +static PyObject *Bone_getChildren(C_Bone *self); +static PyObject *Bone_setName(C_Bone *self, PyObject *args); +static PyObject *Bone_setRoll(C_Bone *self, PyObject *args); +static PyObject *Bone_setHead(C_Bone *self, PyObject *args); +static PyObject *Bone_setTail(C_Bone *self, PyObject *args); +static PyObject *Bone_setLoc(C_Bone *self, PyObject *args); +static PyObject *Bone_setSize(C_Bone *self, PyObject *args); +static PyObject *Bone_setQuat(C_Bone *self, PyObject *args); +//static PyObject *Bone_setParent(C_Bone *self, PyObject *args); +//static PyObject *Bone_setChildren(C_Bone *self, PyObject *args); + +/*****************************************************************************/ +/* Python C_Bone methods table: */ +/*****************************************************************************/ +static PyMethodDef C_Bone_methods[] = { + /* name, method, flags, doc */ + {"getName", (PyCFunction)Bone_getName, METH_NOARGS, "() - return Bone name"}, + {"getRoll", (PyCFunction)Bone_getRoll, METH_NOARGS, "() - return Bone roll"}, + {"getHead", (PyCFunction)Bone_getHead, METH_NOARGS, "() - return Bone head"}, + {"getTail", (PyCFunction)Bone_getTail, METH_NOARGS, "() - return Bone tail"}, + {"getLoc", (PyCFunction)Bone_getLoc, METH_NOARGS, "() - return Bone loc"}, + {"getSize", (PyCFunction)Bone_getSize, METH_NOARGS, "() - return Bone size"}, + {"getQuat", (PyCFunction)Bone_getQuat, METH_NOARGS, "() - return Bone quat"}, + {"getParent", (PyCFunction)Bone_hasParent, METH_NOARGS, + "() - return the parent bone of this one if it exists." + " Otherwise raise an error. Check this condition with the " + "hasParent() method."}, + {"hasParent", (PyCFunction)Bone_hasParent, METH_NOARGS, + "() - return true if bone has a parent"}, + {"getChildren", (PyCFunction)Bone_getChildren, METH_NOARGS, + "() - return Bone children list"}, + {"setName", (PyCFunction)Bone_setName, METH_VARARGS, "(str) - rename Bone"}, + {"setRoll", (PyCFunction)Bone_setRoll, METH_VARARGS, + "(float) - set Bone roll"}, + {"setHead", (PyCFunction)Bone_setHead, METH_VARARGS, + "(float,float,float) - set Bone head pos"}, + {"setTail", (PyCFunction)Bone_setTail, METH_VARARGS, + "(float,float,float) - set Bone tail pos"}, + {"setLoc", (PyCFunction)Bone_setLoc, METH_VARARGS, + "(float,float,float) - set Bone loc"}, + {"setSize", (PyCFunction)Bone_setSize, METH_VARARGS, + "(float,float,float) - set Bone size"}, + {"setQuat", (PyCFunction)Bone_setQuat, METH_VARARGS, + "(float,float,float,float) - set Bone quat"}, + /* {"setParent", (PyCFunction)Bone_setParent, METH_NOARGS, "() - set the Bone parent of this one."}, + {"setChildren", (PyCFunction)Bone_setChildren, METH_NOARGS, "() - replace the children list of the bone."},*/ + {0} +}; + +/*****************************************************************************/ +/* Python TypeBone callback function prototypes: */ +/*****************************************************************************/ +static void BoneDeAlloc (C_Bone *bone); +static PyObject *BoneGetAttr (C_Bone *bone, char *name); +static int BoneSetAttr (C_Bone *bone, char *name, PyObject *v); +static int BoneCmp (C_Bone *a1, C_Bone *a2); +static PyObject *BoneRepr (C_Bone *bone); +static int BonePrint (C_Bone *bone, FILE *fp, int flags); + +/*****************************************************************************/ +/* Python TypeBone structure definition: */ +/*****************************************************************************/ +static PyTypeObject Bone_Type = +{ + PyObject_HEAD_INIT(&PyType_Type) + 0, /* ob_size */ + "Bone", /* tp_name */ + sizeof (C_Bone), /* tp_basicsize */ + 0, /* tp_itemsize */ + /* methods */ + (destructor)BoneDeAlloc, /* tp_dealloc */ + (printfunc)BonePrint, /* tp_print */ + (getattrfunc)BoneGetAttr, /* tp_getattr */ + (setattrfunc)BoneSetAttr, /* tp_setattr */ + (cmpfunc)BoneCmp, /* tp_compare */ + (reprfunc)BoneRepr, /* 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_Bone_methods, /* tp_methods */ + 0, /* tp_members */ +}; + + + + +/*****************************************************************************/ +/* Function: M_Bone_New */ +/* Python equivalent: Blender.Armature.Bone.New */ +/*****************************************************************************/ +static PyObject *M_Bone_New(PyObject *self, PyObject *args, PyObject *keywords) +{ + char *name_str = "BoneName"; + C_Bone *py_bone = NULL; /* for Bone Data object wrapper in Python */ + Bone *bl_bone = NULL; /* for actual Bone Data we create in Blender */ + + printf ("In Bone_New()\n"); + + if (!PyArg_ParseTuple(args, "|s", &name_str)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string or empty argument")); + + // Create the C structure for the newq bone + bl_bone = (Bone*)malloc(sizeof(Bone)); + strncpy(bl_bone->name,name_str,sizeof(bl_bone->name)); + + if (bl_bone) /* now create the wrapper obj in Python */ + py_bone = (C_Bone *)PyObject_NEW(C_Bone, &Bone_Type); + else + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't create Bone Data in Blender")); + + if (py_bone == NULL) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create Bone Data object")); + + py_bone->bone = bl_bone; /* link Python bone wrapper with Blender Bone */ + + if (strcmp(name_str, "BoneData") == 0) + return (PyObject *)py_bone; + else { /* user gave us a name for the bone, use it */ + // TODO: check that name is not already in use? + PyOS_snprintf(bl_bone->name, sizeof(bl_bone->name), "%s", name_str); + } + + return (PyObject *)py_bone; +} + + +/*****************************************************************************/ +/* Function: M_Bone_Init */ +/*****************************************************************************/ +PyObject *M_Bone_Init (void) +{ + PyObject *submodule; + + printf ("In M_Bone_Init()\n"); + + submodule = Py_InitModule3("Blender.Armature.Bone", + M_Bone_methods, M_Bone_doc); + + return (submodule); +} + +/*****************************************************************************/ +/* Python C_Bone methods: */ +/*****************************************************************************/ +static PyObject *Bone_getName(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = PyString_FromString(self->bone->name); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.name attribute")); +} + + +static PyObject *Bone_getRoll(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("f", self->bone->roll); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.roll attribute")); +} + + +static PyObject *Bone_getHead(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("[fff]", self->bone->head[0],self->bone->head[1], + self->bone->head[2]); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.head attribute")); +} + + +static PyObject *Bone_getTail(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("[fff]", self->bone->tail[0],self->bone->tail[1], + self->bone->tail[2]); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.tail attribute")); +} + + +static PyObject *Bone_getLoc (C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("[fff]", self->bone->loc[0],self->bone->loc[1], + self->bone->loc[2]); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.loc attribute")); +} + + +static PyObject *Bone_getSize(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("[fff]", self->bone->size[0],self->bone->size[1], + self->bone->size[2]); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.size attribute")); +} + + +static PyObject *Bone_getQuat(C_Bone *self) +{ + PyObject *attr=NULL; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + attr = Py_BuildValue("[ffff]", self->bone->quat[0],self->bone->quat[1], + self->bone->quat[2],self->bone->quat[3]); + + if (attr) return attr; + + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get Bone.tail attribute")); +} + + +static PyObject *Bone_hasParent(C_Bone *self) +{ + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + /* + return M_BoneCreatePyObject(self->bone->parent); + */ + if (self->bone->parent) + { + Py_INCREF(Py_True); + return Py_True; + } + else + { + Py_INCREF(Py_False); + return Py_False; + } + +} + + +static PyObject *Bone_getParent(C_Bone *self) +{ + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (self->bone->parent) return M_BoneCreatePyObject(self->bone->parent); + else /*(EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get parent bone, because bone hasn't got a parent."));*/ + { + Py_INCREF(Py_None); + return Py_None; + } + +} + + +static PyObject *Bone_getChildren(C_Bone *self) +{ + int totbones = 0; + Bone* current = NULL; + PyObject *listbones = NULL; + int i; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + /* Count the number of bones to create the list */ + current = self->bone->childbase.first; + for (;current; current=current->next) totbones++; + + /* Create a list with a bone wrapper for each bone */ + current = self->bone->childbase.first; + listbones = PyList_New(totbones); + for (i=0; i<totbones; i++) { + assert(current); + PyList_SetItem(listbones, i, M_BoneCreatePyObject(current)); + current = current->next; + } + + return listbones; +} + + +static PyObject *Bone_setName(C_Bone *self, PyObject *args) +{ + char *name; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "s", &name)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string argument")); + + PyOS_snprintf(self->bone->name, sizeof(self->bone->name), "%s", name); + + Py_INCREF(Py_None); + return Py_None; +} + + +PyObject *Bone_setRoll(C_Bone *self, PyObject *args) +{ + float roll; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "f", &roll)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); + + self->bone->roll = roll; + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyObject *Bone_setHead(C_Bone *self, PyObject *args) +{ + float f1,f2,f3; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "fff", &f1,&f2,&f3)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected 3 float arguments")); + + self->bone->head[0] = f1; + self->bone->head[1] = f2; + self->bone->head[2] = f3; + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyObject *Bone_setTail(C_Bone *self, PyObject *args) +{ + float f1,f2,f3; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "fff", &f1,&f2,&f3)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected 3 float arguments")); + + self->bone->tail[0] = f1; + self->bone->tail[1] = f2; + self->bone->tail[2] = f3; + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyObject *Bone_setLoc(C_Bone *self, PyObject *args) +{ + float f1,f2,f3; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "fff", &f1,&f2,&f3)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected 3 float arguments")); + + self->bone->loc[0] = f1; + self->bone->loc[1] = f2; + self->bone->loc[2] = f3; + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyObject *Bone_setSize(C_Bone *self, PyObject *args) +{ + float f1,f2,f3; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "fff", &f1,&f2,&f3)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected 3 float arguments")); + + self->bone->size[0] = f1; + self->bone->size[1] = f2; + self->bone->size[2] = f3; + + Py_INCREF(Py_None); + return Py_None; +} + + +static PyObject *Bone_setQuat(C_Bone *self, PyObject *args) +{ + float f1,f2,f3,f4; + + if (!self->bone) (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't get attribute from a NULL bone")); + + if (!PyArg_ParseTuple(args, "ffff", &f1,&f2,&f3,&f4)) + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected 4 float arguments")); + + self->bone->head[0] = f1; + self->bone->head[1] = f2; + self->bone->head[2] = f3; + self->bone->head[3] = f4; + + Py_INCREF(Py_None); + return Py_None; +} + + +/*****************************************************************************/ +/* Function: BoneDeAlloc */ +/* Description: This is a callback function for the C_Bone type. It is */ +/* the destructor function. */ +/*****************************************************************************/ +static void BoneDeAlloc (C_Bone *self) +{ + PyObject_DEL (self); +} + +/*****************************************************************************/ +/* Function: BoneGetAttr */ +/* Description: This is a callback function for the C_Bone type. It is */ +/* the function that accesses C_Bone member variables and */ +/* methods. */ +/*****************************************************************************/ +static PyObject* BoneGetAttr (C_Bone *self, char *name) +{ + PyObject *attr = Py_None; + + if (strcmp(name, "name") == 0) + attr = Bone_getName(self); + else if (strcmp(name, "roll") == 0) + attr = Bone_getRoll(self); + else if (strcmp(name, "head") == 0) + attr = Bone_getHead(self); + else if (strcmp(name, "tail") == 0) + attr = Bone_getTail(self); + else if (strcmp(name, "size") == 0) + attr = Bone_getSize(self); + else if (strcmp(name, "loc") == 0) + attr = Bone_getLoc(self); + else if (strcmp(name, "quat") == 0) + attr = Bone_getQuat(self); + else if (strcmp(name, "parent") == 0) + // Skip the checks for Py_None as its a valid result to this call. + return Bone_getParent(self); + else if (strcmp(name, "children") == 0) + attr = Bone_getChildren(self); + else if (strcmp(name, "__members__") == 0) { + /* 9 entries */ + attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s]", + "name","roll","head","tail","loc","size", + "quat","parent","children"); + } + + if (!attr) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create PyObject")); + + if (attr != Py_None) return attr; /* member attribute found, return it */ + + /* not an attribute, search the methods table */ + return Py_FindMethod(C_Bone_methods, (PyObject *)self, name); +} + +/*****************************************************************************/ +/* Function: BoneSetAttr */ +/* Description: This is a callback function for the C_Bone type. It is the */ +/* function that changes Bone Data members values. If this */ +/* data is linked to a Blender Bone, it also gets updated. */ +/*****************************************************************************/ +static int BoneSetAttr (C_Bone *self, char *name, PyObject *value) +{ + PyObject *valtuple; + PyObject *error = NULL; + + valtuple = Py_BuildValue("(N)", value); /* the set* functions expect a tuple */ + + if (!valtuple) + return EXPP_ReturnIntError(PyExc_MemoryError, + "BoneSetAttr: couldn't create tuple"); + + if (strcmp (name, "name") == 0) + error = Bone_setName (self, valtuple); + else { /* Error */ + Py_DECREF(valtuple); + + /* ... member with the given name was found */ + return (EXPP_ReturnIntError (PyExc_KeyError, + "attribute not found")); + } + + Py_DECREF(valtuple); + + if (error != Py_None) return -1; + + Py_DECREF(Py_None); /* was incref'ed by the called Bone_set* function */ + return 0; /* normal exit */ +} + +/*****************************************************************************/ +/* Function: BonePrint */ +/* Description: This is a callback function for the C_Bone type. It */ +/* builds a meaninful string to 'print' bone objects. */ +/*****************************************************************************/ +static int BonePrint(C_Bone *self, FILE *fp, int flags) +{ + if (self->bone) fprintf(fp, "[Bone \"%s\"]", self->bone->name); + else fprintf(fp, "[Bone NULL]"); + return 0; +} + +/*****************************************************************************/ +/* Function: BoneRepr */ +/* Description: This is a callback function for the C_Bone type. It */ +/* builds a meaninful string to represent bone objects. */ +/*****************************************************************************/ +static PyObject *BoneRepr (C_Bone *self) +{ + if (self->bone) return PyString_FromString(self->bone->name); + else return PyString_FromString("NULL"); +} + +/**************************************************************************/ +/* Function: BoneCmp */ +/* Description: This is a callback function for the C_Bone type. It */ +/* compares the two bones: translate comparison to the */ +/* C pointers. */ +/**************************************************************************/ +static int BoneCmp (C_Bone *a, C_Bone *b) +{ + if (a<b) return -1; + else if (a==b) return 0; + else return 1; +} + + + + +/*****************************************************************************/ +/* Function: M_BoneCreatePyObject */ +/* Description: This function will create a new BlenBone from an existing */ +/* Bone structure. */ +/*****************************************************************************/ +PyObject* M_BoneCreatePyObject (struct Bone *obj) +{ + C_Bone * blen_bone; + + printf (" In M_BoneCreatePyObject\n"); + + blen_bone = (C_Bone*)PyObject_NEW (C_Bone, &Bone_Type); + + if (blen_bone == NULL) + { + return (NULL); + } + blen_bone->bone = obj; + return ((PyObject*)blen_bone); +} + +/*****************************************************************************/ +/* Function: M_BoneCheckPyObject */ +/* Description: This function returns true when the given PyObject is of the */ +/* type Bone. Otherwise it will return false. */ +/*****************************************************************************/ +int M_BoneCheckPyObject (PyObject *py_obj) +{ + return (py_obj->ob_type == &Bone_Type); +} + +/*****************************************************************************/ +/* Function: M_BoneFromPyObject */ +/* Description: This function returns the Blender bone from the given */ +/* PyObject. */ +/*****************************************************************************/ +struct Bone* M_BoneFromPyObject (PyObject *py_obj) +{ + C_Bone * blen_obj; + + blen_obj = (C_Bone*)py_obj; + return (blen_obj->bone); +} diff --git a/source/blender/python/api2_2x/Bone.h b/source/blender/python/api2_2x/Bone.h new file mode 100644 index 00000000000..ec71a8f33df --- /dev/null +++ b/source/blender/python/api2_2x/Bone.h @@ -0,0 +1,54 @@ +/* + * + * ***** BEGIN GPL/BL DUAL 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. The Blender + * Foundation also sells licenses for use in proprietary software under + * the Blender License. See http://www.blender.org/BL/ for information + * about this. + * + * 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. + * + * This is a new part of Blender. + * + * Contributor(s): Jordi Rovira i Bonet + * + * ***** END GPL/BL DUAL LICENSE BLOCK ***** +*/ + +#ifndef EXPP_BONE_H +#define EXPP_BONE_H + +#include <Python.h> + +#include <DNA_armature_types.h> + +/** Bone module initialization function. */ +PyObject *M_Bone_Init (void); + + +/** Python C_Bone structure definition. */ +typedef struct { + PyObject_HEAD + Bone *bone; +} C_Bone; + + +PyObject* M_BoneCreatePyObject (struct Bone *obj); +int M_BoneCheckPyObject (PyObject *py_obj); +Bone* M_BoneFromPyObject (PyObject *py_obj); + +#endif diff --git a/source/blender/python/api2_2x/Curve.c b/source/blender/python/api2_2x/Curve.c index 05fbd5a2d1c..c1400bce146 100644 --- a/source/blender/python/api2_2x/Curve.c +++ b/source/blender/python/api2_2x/Curve.c @@ -38,25 +38,27 @@ /*****************************************************************************/ static PyObject *M_Curve_New(PyObject *self, PyObject *args) { - char buf[21]; - char *name = NULL; + char buf[24]; + char*name=NULL ; C_Curve *pycurve; /* for Curve Data object wrapper in Python */ Curve *blcurve = 0; /* for actual Curve Data we create in Blender */ printf ("In Curve_New()\n"); if (!PyArg_ParseTuple(args, "|s", &name)) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected string argument or no argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected string argument or no argument")); blcurve = add_curve(OB_CURVE); /* first create the Curve Data in Blender */ - if (blcurve == NULL) return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't create Curve Data in Blender")); + if (blcurve == NULL) + return (EXPP_ReturnPyObjError (PyExc_RuntimeError, + "couldn't create Curve Data in Blender")); pycurve = (C_Curve *)PyObject_NEW(C_Curve, &Curve_Type); - if (pycurve == NULL) return (EXPP_ReturnPyObjError (PyExc_MemoryError, - "couldn't create Curve Data object")); + if (pycurve == NULL) + return (EXPP_ReturnPyObjError (PyExc_MemoryError, + "couldn't create Curve Data object")); pycurve->curve = blcurve; /* link Python curve wrapper to Blender Curve */ if (name) @@ -69,21 +71,21 @@ static PyObject *M_Curve_New(PyObject *self, PyObject *args) } /*****************************************************************************/ -/* Function: M_Curve_Get */ -/* Python equivalent: Blender.Curve.Get */ +/* Function: M_Curve_Get */ +/* Python equivalent: Blender.Curve.Get */ /*****************************************************************************/ static PyObject *M_Curve_Get(PyObject *self, PyObject *args) { - char *name = 0; + char *name = NULL; Curve *curv_iter; C_Curve *wanted_curv; printf ("In Curve_Get()\n"); - if (!PyArg_ParseTuple(args, "|s", &name))/* expects nothing or a string */ + if (!PyArg_ParseTuple(args, "|s", &name))//expects nothing or a string return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected string argument")); - if(name){/* a name has been given */ + "expected string argument")); + if(name){//a name has been given /* Use the name to search for the curve requested */ wanted_curv = NULL; curv_iter = G.main->curve.first; @@ -91,8 +93,8 @@ static PyObject *M_Curve_Get(PyObject *self, PyObject *args) while ((curv_iter) && (wanted_curv == NULL)) { if (strcmp (name, curv_iter->id.name+2) == 0) { - wanted_curv = (C_Curve *)PyObject_NEW(C_Curve, &Curve_Type); - if (wanted_curv) wanted_curv->curve = curv_iter; + wanted_curv = (C_Curve *)PyObject_NEW(C_Curve, &Curve_Type); + if (wanted_curv) wanted_curv->curve = curv_iter; } curv_iter = curv_iter->id.next; @@ -107,8 +109,8 @@ static PyObject *M_Curve_Get(PyObject *self, PyObject *args) return (PyObject*)wanted_curv; - }/* if(name) */ - else{/* no name has been given; return a list of all curves by name. */ + }//if(name) + else{//no name has been given; return a list of all curves by name. int index = 0; PyObject *curvlist, *pystr; @@ -117,14 +119,14 @@ static PyObject *M_Curve_Get(PyObject *self, PyObject *args) if (curvlist == NULL) return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyList")); + "couldn't create PyList")); while (curv_iter) { pystr = PyString_FromString (curv_iter->id.name+2); if (!pystr) - return (PythonReturnErrorObject (PyExc_MemoryError, - "couldn't create PyString")); + return (PythonReturnErrorObject (PyExc_MemoryError, + "couldn't create PyString")); PyList_SET_ITEM (curvlist, index, pystr); @@ -137,7 +139,7 @@ static PyObject *M_Curve_Get(PyObject *self, PyObject *args) } /*****************************************************************************/ -/* Function: M_Curve_Init */ +/* Function: M_Curve_Init */ /*****************************************************************************/ PyObject *M_Curve_Init (void) { @@ -149,7 +151,13 @@ PyObject *M_Curve_Init (void) /*****************************************************************************/ /* Python C_Curve methods: */ +/* gives access to */ +/* name, pathlen totcol flag bevresol */ +/* resolu resolv width ext1 ext2 */ +/* controlpoint loc rot size */ /*****************************************************************************/ + + static PyObject *Curve_getName(C_Curve *self) { PyObject *attr = PyString_FromString(self->curve->id.name+2); @@ -157,17 +165,17 @@ static PyObject *Curve_getName(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.name attribute")); + "couldn't get Curve.name attribute")); } static PyObject *Curve_setName(C_Curve *self, PyObject *args) { char*name; - char buf[21]; + char buf[50]; if (!PyArg_ParseTuple(args, "s", &(name))) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected string argument")); + "expected string argument")); PyOS_snprintf(buf, sizeof(buf), "%s", name); rename_id(&self->curve->id, buf); /* proper way in Blender */ @@ -182,7 +190,7 @@ static PyObject *Curve_getPathLen(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.pathlen attribute")); + "couldn't get Curve.pathlen attribute")); } @@ -190,8 +198,8 @@ static PyObject *Curve_setPathLen(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->pathlen))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -205,7 +213,7 @@ static PyObject *Curve_getTotcol(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.totcol attribute")); + "couldn't get Curve.totcol attribute")); } @@ -213,8 +221,8 @@ static PyObject *Curve_setTotcol(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->totcol))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -228,7 +236,7 @@ static PyObject *Curve_getMode(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.flag attribute")); + "couldn't get Curve.flag attribute")); } @@ -236,8 +244,8 @@ static PyObject *Curve_setMode(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->flag))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -251,7 +259,7 @@ static PyObject *Curve_getBevresol(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.bevresol attribute")); + "couldn't get Curve.bevresol attribute")); } @@ -259,8 +267,8 @@ static PyObject *Curve_setBevresol(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->bevresol))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -274,7 +282,7 @@ static PyObject *Curve_getResolu(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.resolu attribute")); + "couldn't get Curve.resolu attribute")); } @@ -282,8 +290,8 @@ static PyObject *Curve_setResolu(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->resolu))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -298,7 +306,7 @@ static PyObject *Curve_getResolv(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.resolv attribute")); + "couldn't get Curve.resolv attribute")); } @@ -306,8 +314,8 @@ static PyObject *Curve_setResolv(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "i", &(self->curve->resolv))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected int argument")); Py_INCREF(Py_None); return Py_None; @@ -322,7 +330,7 @@ static PyObject *Curve_getWidth(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.width attribute")); + "couldn't get Curve.width attribute")); } @@ -330,8 +338,8 @@ static PyObject *Curve_setWidth(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "f", &(self->curve->width))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); Py_INCREF(Py_None); return Py_None; @@ -345,7 +353,7 @@ static PyObject *Curve_getExt1(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.ext1 attribute")); + "couldn't get Curve.ext1 attribute")); } @@ -353,8 +361,8 @@ static PyObject *Curve_setExt1(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "f", &(self->curve->ext1))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); Py_INCREF(Py_None); return Py_None; @@ -369,7 +377,7 @@ static PyObject *Curve_getExt2(C_Curve *self) if (attr) return attr; return (EXPP_ReturnPyObjError (PyExc_RuntimeError, - "couldn't get Curve.ext2 attribute")); + "couldn't get Curve.ext2 attribute")); } @@ -377,8 +385,8 @@ static PyObject *Curve_setExt2(C_Curve *self, PyObject *args) { if (!PyArg_ParseTuple(args, "f", &(self->curve->ext2))) - return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float argument")); + return (EXPP_ReturnPyObjError (PyExc_AttributeError, + "expected float argument")); Py_INCREF(Py_None); return Py_None; @@ -392,17 +400,18 @@ static PyObject *Curve_setControlPoint(C_Curve *self, PyObject *args) int numcourbe,numpoint,i,j; float x,y,z,w; float bez[9]; - + if (!ptrnurb){ Py_INCREF(Py_None);return Py_None;} + if (ptrnurb->bp) if (!PyArg_ParseTuple(args, "iiffff", &numcourbe,&numpoint,&x,&y,&z,&w)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int int float float float float arguments")); + "expected int int float float float float arguments")); if (ptrnurb->bezt) if (!PyArg_ParseTuple(args, "iifffffffff", &numcourbe,&numpoint, - bez,bez+1,bez+2,bez+3,bez+4,bez+5,bez+6,bez+7,bez+8)) + bez,bez+1,bez+2,bez+3,bez+4,bez+5,bez+6,bez+7,bez+8)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int int float float float float float float float" - " float float arguments")); + "expected int int float float float float float float " + "float float float arguments")); for(i = 0;i< numcourbe;i++) ptrnurb=ptrnurb->next; @@ -416,43 +425,43 @@ static PyObject *Curve_setControlPoint(C_Curve *self, PyObject *args) if (ptrnurb->bezt) { for(i = 0;i<3;i++) - for(j = 0;j<3;j++) - ptrnurb->bezt[numpoint].vec[i][j] = bez[i*3+j]; + for(j = 0;j<3;j++) + ptrnurb->bezt[numpoint].vec[i][j] = bez[i*3+j]; } - + Py_INCREF(Py_None); return Py_None; } static PyObject *Curve_getControlPoint(C_Curve *self, PyObject *args) { - PyObject* liste = 0; + PyObject* liste = PyList_New(0); Nurb*ptrnurb; int numcourbe,numpoint,i,j; if (!PyArg_ParseTuple(args, "ii", &numcourbe,&numpoint)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected int int arguments")); - /* check args ??? */ + "expected int int arguments")); + //check args ??? + if (!self->curve->nurb.first)return liste; ptrnurb = self->curve->nurb.first; - for(i = 0;i< numcourbe;i++)/* selection of the first point of the curve */ + for(i = 0;i< numcourbe;i++)//selection of the first point of the curve ptrnurb=ptrnurb->next; if (ptrnurb->bp) { - liste = PyList_New(4); for(i = 0;i< 4;i++) - PyList_SetItem(liste, i, PyFloat_FromDouble( ptrnurb->bp[numpoint].vec[i])); + PyList_Append(liste, PyFloat_FromDouble( ptrnurb->bp[numpoint].vec[i])); } if (ptrnurb->bezt) { liste = PyList_New(9); for(i = 0;i< 3;i++) - for(j = 0;j< 3;j++) - PyList_SetItem(liste, i*3+j, - PyFloat_FromDouble( ptrnurb->bezt[numpoint].vec[i][j])); + for(j = 0;j< 3;j++) + PyList_Append(liste, + PyFloat_FromDouble( ptrnurb->bezt[numpoint].vec[i][j])); } return liste; @@ -475,7 +484,7 @@ static PyObject *Curve_setLoc(C_Curve *self, PyObject *args) if (!PyArg_ParseTuple(args, "fff",&x,&y,&z)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float float float arguments")); + "expected float float float arguments")); self->curve->loc[0] = x; self->curve->loc[1] = y; @@ -501,7 +510,7 @@ static PyObject *Curve_setRot(C_Curve *self, PyObject *args) if (!PyArg_ParseTuple(args, "fff",&x,&y,&z)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float float float arguments")); + "expected float float float arguments")); self->curve->rot[0] = x; self->curve->rot[1] = y; @@ -526,7 +535,7 @@ static PyObject *Curve_setSize(C_Curve *self, PyObject *args) if (!PyArg_ParseTuple(args, "fff",&x,&y,&z)) return (EXPP_ReturnPyObjError (PyExc_AttributeError, - "expected float float float arguments")); + "expected float float float arguments")); self->curve->size[0] = x; self->curve->size[1] = y; @@ -555,16 +564,38 @@ static void CurveDeAlloc (C_Curve *self) /* the function that accesses C_Curve "member variables" and */ /* methods. */ /*****************************************************************************/ -static PyObject *CurveGetAttr (C_Curve *self, char *name)/* getattr */ +static PyObject *CurveGetAttr (C_Curve *self, char *name)//getattr { PyObject *attr = Py_None; if (strcmp(name, "name") == 0) attr = PyString_FromString(self->curve->id.name+2); + if (strcmp(name, "pathlen") == 0) + attr = PyInt_FromLong(self->curve->pathlen); + if (strcmp(name, "totcol") == 0) + attr = PyInt_FromLong(self->curve->totcol); + if (strcmp(name, "flag") == 0) + attr = PyInt_FromLong(self->curve->flag); + if (strcmp(name, "bevresol") == 0) + attr = PyInt_FromLong(self->curve->bevresol); + if (strcmp(name, "resolu") == 0) + attr = PyInt_FromLong(self->curve->resolu); + if (strcmp(name, "resolv") == 0) + attr = PyInt_FromLong(self->curve->resolv); + if (strcmp(name, "width") == 0) + attr = PyFloat_FromDouble(self->curve->width); + if (strcmp(name, "ext1") == 0) + attr = PyFloat_FromDouble(self->curve->ext1); + if (strcmp(name, "ext2") == 0) + attr = PyFloat_FromDouble(self->curve->ext2); + + + + if (!attr) return (EXPP_ReturnPyObjError (PyExc_MemoryError, - "couldn't create PyObject")); + "couldn't create PyObject")); if (attr != Py_None) return attr; /* member attribute found, return it */ @@ -578,8 +609,47 @@ static PyObject *CurveGetAttr (C_Curve *self, char *name)/* getattr */ /* function that sets Curve Data attributes (member variables). */ /*****************************************************************************/ static int CurveSetAttr (C_Curve *self, char *name, PyObject *value) -{ /* setattr (mandatory?) */ - return 0; /* normal exit */ +{ PyObject *valtuple; + PyObject *error = NULL; + valtuple = Py_BuildValue("(N)", value); + //resolu resolv width ext1 ext2 + if (!valtuple) + return EXPP_ReturnIntError(PyExc_MemoryError, + "CurveSetAttr: couldn't create PyTuple"); + + if (strcmp (name, "name") == 0) + error = Curve_setName (self, valtuple); + else if (strcmp (name, "pathlen") == 0) + error = Curve_setPathLen(self, valtuple); + else if (strcmp (name, "resolu") == 0) + error = Curve_setResolu (self, valtuple); + else if (strcmp (name, "resolv") == 0) + error = Curve_setResolv (self, valtuple); + else if (strcmp (name, "width") == 0) + error = Curve_setWidth (self, valtuple); + else if (strcmp (name, "ext1") == 0) + error = Curve_setExt1 (self, valtuple); + else if (strcmp (name, "ext2") == 0) + error = Curve_setExt2 (self, valtuple); + + else { /* Error */ + Py_DECREF(valtuple); + + if ((strcmp (name, "Types") == 0) || + (strcmp (name, "Modes") == 0)) + return (EXPP_ReturnIntError (PyExc_AttributeError, + "constant dictionary -- cannot be changed")); + + else + return (EXPP_ReturnIntError (PyExc_KeyError, + "attribute not found")); + } + + Py_DECREF(valtuple); + + if (error != Py_None) return -1; + Py_DECREF(Py_None); + return 0; } /*****************************************************************************/ @@ -587,11 +657,11 @@ static int CurveSetAttr (C_Curve *self, char *name, PyObject *value) /* Description: This is a callback function for the C_Curve type. It */ /* builds a meaninful string to 'print' curve objects. */ /*****************************************************************************/ -static int CurvePrint(C_Curve *self, FILE *fp, int flags) /* print */ +static int CurvePrint(C_Curve *self, FILE *fp, int flags) //print { fprintf(fp, "[Curve \"%s\"]\n", self->curve->id.name+2); - + return 0; } @@ -600,17 +670,12 @@ static int CurvePrint(C_Curve *self, FILE *fp, int flags) /* print */ /* Description: This is a callback function for the C_Curve type. It */ /* builds a meaninful string to represent curve objects. */ /*****************************************************************************/ -static PyObject *CurveRepr (C_Curve *self) /* used by 'repr' */ +static PyObject *CurveRepr (C_Curve *self) //used by 'repr' { return PyString_FromString(self->curve->id.name+2); } -/*****************************************************************************/ -/* Functions: CurveCreatePyObject, CurveCheckPyObject, CurveFromPyObject */ -/* Description: These helper functions are needed by the Object module to */ -/* work with its specific object.data, Curve Data in this case. */ -/*****************************************************************************/ PyObject* CurveCreatePyObject (struct Curve *curve) { C_Curve * blen_object; @@ -630,9 +695,10 @@ PyObject* CurveCreatePyObject (struct Curve *curve) int CurveCheckPyObject (PyObject *py_obj) { - return (py_obj->ob_type == &Curve_Type); +return (py_obj->ob_type == &Curve_Type); } + struct Curve* CurveFromPyObject (PyObject *py_obj) { C_Curve * blen_obj; @@ -641,3 +707,4 @@ struct Curve* CurveFromPyObject (PyObject *py_obj) return (blen_obj->curve); } + diff --git a/source/blender/python/api2_2x/Curve.h b/source/blender/python/api2_2x/Curve.h index 711ce5c3692..37d86e408e5 100644 --- a/source/blender/python/api2_2x/Curve.h +++ b/source/blender/python/api2_2x/Curve.h @@ -47,7 +47,7 @@ #include "gen_utils.h" /*****************************************************************************/ -/* Python API function prototypes for the Curve module. */ +/* Python API function prototypes for the Curve module. */ /*****************************************************************************/ static PyObject *M_Curve_New (PyObject *self, PyObject *args); static PyObject *M_Curve_Get (PyObject *self, PyObject *args); @@ -55,13 +55,18 @@ static PyObject *M_Curve_Get (PyObject *self, PyObject *args); /*****************************************************************************/ /* The following string definitions are used for documentation strings. */ /* In Python these will be written to the console when doing a */ -/* Blender.Curve.__doc__ */ -/*****************************************************************************/ -char M_Curve_doc[] = ""; +/* Blender.Curve.__doc__ */ +/*****************************************************************************/ +char M_Curve_doc[] = "The Blender Curve module\n\n\ +This module provides access to **Curve Data** in Blender.\n\ +Functions :\n\ + New(opt name) : creates a new curve object with the given name (optional)\n\ + Get(name) : retreives a curve with the given name (mandatory)\n\ + get(name) : same as Get. Kept for compatibility reasons"; char M_Curve_New_doc[] =""; char M_Curve_Get_doc[] ="xxx"; /*****************************************************************************/ -/* Python method structure definition for Blender.Curve module: */ +/* Python method structure definition for Blender.Curve module: */ /*****************************************************************************/ struct PyMethodDef M_Curve_methods[] = { {"New",(PyCFunction)M_Curve_New, METH_VARARGS,M_Curve_New_doc}, @@ -79,7 +84,7 @@ typedef struct { } C_Curve; /*****************************************************************************/ -/* Python C_Curve methods declarations: */ +/* Python C_Curve methods declarations: */ /*****************************************************************************/ static PyObject *Curve_getName(C_Curve *self); static PyObject *Curve_setName(C_Curve *self, PyObject *args); @@ -111,60 +116,74 @@ static PyObject *Curve_getSize(C_Curve *self); static PyObject *Curve_setSize(C_Curve *self, PyObject *args); /*****************************************************************************/ -/* Python C_Curve methods table: */ +/* Python C_Curve methods table: */ /*****************************************************************************/ static PyMethodDef C_Curve_methods[] = { /* name, method, flags, doc */ - {"getName", (PyCFunction)Curve_getName, METH_NOARGS, - "() - Return Curve Data name"}, - {"setName", (PyCFunction)Curve_setName, METH_NOARGS, - "() - Sets Curve Data name"}, - {"getPathLen", (PyCFunction)Curve_getPathLen, METH_NOARGS, - "() - Return Curve path length"}, - {"setPathLen", (PyCFunction)Curve_setPathLen, METH_VARARGS, - "(int) - Sets Curve path length"}, - {"getTotcol", (PyCFunction)Curve_getTotcol, METH_NOARGS,"() - Return totcol"}, - {"setTotcol", (PyCFunction)Curve_setTotcol, METH_VARARGS, - "(int) - Sets totcol"}, - {"getFlag", (PyCFunction)Curve_getMode, METH_NOARGS,"() - Return flag"}, - {"setFlag", (PyCFunction)Curve_setMode, METH_VARARGS,"(int) - Sets flag"}, - {"getBevresol", (PyCFunction)Curve_getBevresol, METH_NOARGS, - "() - Return bevresol"}, - {"setBevresol", (PyCFunction)Curve_setBevresol, METH_VARARGS, - "(int) - Sets bevresol"}, - {"getResolu", (PyCFunction)Curve_getResolu, METH_NOARGS,"() - Return resolu"}, - {"setResolu", (PyCFunction)Curve_setResolu, METH_VARARGS, - "(int) - Sets resolu"}, - {"getResolv", (PyCFunction)Curve_getResolv, METH_NOARGS,"() - Return resolv"}, - {"setResolv", (PyCFunction)Curve_setResolv, METH_VARARGS, - "(int) - Sets resolv"}, - {"getWidth", (PyCFunction)Curve_getWidth, METH_NOARGS,"() - Return width"}, - {"setWidth", (PyCFunction)Curve_setWidth, METH_VARARGS,"(int) - Sets width"}, - {"getExt1", (PyCFunction)Curve_getExt1, METH_NOARGS,"() - Return ext1"}, - {"setExt1", (PyCFunction)Curve_setExt1, METH_VARARGS,"(int) - Sets ext1"}, - {"getExt2", (PyCFunction)Curve_getExt2, METH_NOARGS,"() - Return ext2"}, - {"setExt2", (PyCFunction)Curve_setExt2, METH_VARARGS,"(int) - Sets ext2"}, - {"getControlPoint", (PyCFunction)Curve_getControlPoint, METH_VARARGS, - "(int numcurve,int numpoint) - Gets a control point.Depending upon" - " the curve type, returne a list of 4 or 9 floats"}, - {"setControlPoint", (PyCFunction)Curve_setControlPoint, METH_VARARGS, - "(int numcurve,int numpoint,float x,float y,float z, float w)(nurbs)" - " or (int numcurve,int numpoint,float x1,...,x9(bezier) " - "Sets a control point "}, - {"getLoc", (PyCFunction)Curve_getLoc, METH_NOARGS,"() - Gets Location"}, - {"setLoc", (PyCFunction)Curve_setLoc, METH_VARARGS, - "(float x,float y,float z) - Sets Location"}, - {"getRot", (PyCFunction)Curve_getRot, METH_NOARGS,"() - Gets Rotation"}, - {"setRot", (PyCFunction)Curve_setRot, METH_VARARGS, - "(float x,float y,float z) - Sets Rotation"}, - {"getSize", (PyCFunction)Curve_getSize, METH_NOARGS,"() - Gets Size"}, - {"setSize", (PyCFunction)Curve_setSize, METH_VARARGS, - "(float x,float y,float z) - Sets Size"}, + {"getName", (PyCFunction)Curve_getName, + METH_NOARGS,"() - Return Curve Data name"}, + {"setName", (PyCFunction)Curve_setName, + METH_VARARGS,"() - Sets Curve Data name"}, + {"getPathLen", (PyCFunction)Curve_getPathLen, + METH_NOARGS,"() - Return Curve path length"}, + {"setPathLen", (PyCFunction)Curve_setPathLen, + METH_VARARGS,"(int) - Sets Curve path length"}, + {"getTotcol", (PyCFunction)Curve_getTotcol, + METH_NOARGS,"() - Return totcol"}, + {"setTotcol", (PyCFunction)Curve_setTotcol, + METH_VARARGS,"(int) - Sets totcol"}, + {"getFlag", (PyCFunction)Curve_getMode, + METH_NOARGS,"() - Return flag"}, + {"setFlag", (PyCFunction)Curve_setMode, + METH_VARARGS,"(int) - Sets flag"}, + {"getBevresol", (PyCFunction)Curve_getBevresol, + METH_NOARGS,"() - Return bevresol"}, + {"setBevresol", (PyCFunction)Curve_setBevresol, + METH_VARARGS,"(int) - Sets bevresol"}, + {"getResolu", (PyCFunction)Curve_getResolu, + METH_NOARGS,"() - Return resolu"}, + {"setResolu", (PyCFunction)Curve_setResolu, + METH_VARARGS,"(int) - Sets resolu"}, + {"getResolv", (PyCFunction)Curve_getResolv, + METH_NOARGS,"() - Return resolv"}, + {"setResolv", (PyCFunction)Curve_setResolv, + METH_VARARGS,"(int) - Sets resolv"}, + {"getWidth", (PyCFunction)Curve_getWidth, + METH_NOARGS,"() - Return width"}, + {"setWidth", (PyCFunction)Curve_setWidth, + METH_VARARGS,"(int) - Sets width"}, + {"getExt1", (PyCFunction)Curve_getExt1, + METH_NOARGS,"() - Return ext1"}, + {"setExt1", (PyCFunction)Curve_setExt1, + METH_VARARGS,"(int) - Sets ext1"}, + {"getExt2", (PyCFunction)Curve_getExt2, + METH_NOARGS,"() - Return ext2"}, + {"setExt2", (PyCFunction)Curve_setExt2, + METH_VARARGS,"(int) - Sets ext2"}, + {"getControlPoint", (PyCFunction)Curve_getControlPoint, + METH_VARARGS,"(int numcurve,int numpoint) - Gets a control point." + "\nDepending upon the curve type, returne a list of 4 or 9 floats"}, + {"setControlPoint", (PyCFunction)Curve_setControlPoint, + METH_VARARGS,"(int numcurve,int numpoint,float x,float y,float z,\ +loat w)(nurbs) or (int numcurve,int numpoint,float x1,...,x9(bezier)\ +Sets a control point "}, + {"getLoc", (PyCFunction)Curve_getLoc, + METH_NOARGS,"() - Gets Location"}, + {"setLoc", (PyCFunction)Curve_setLoc, + METH_VARARGS,"(float x,float y,float z) - Sets Location"}, + {"getRot", (PyCFunction)Curve_getRot, + METH_NOARGS,"() - Gets Rotation"}, + {"setRot", (PyCFunction)Curve_setRot, + METH_VARARGS,"(float x,float y,float z) - Sets Rotation"}, + {"getSize", (PyCFunction)Curve_getSize, + METH_NOARGS,"() - Gets Size"}, + {"setSize", (PyCFunction)Curve_setSize, + METH_VARARGS,"(float x,float y,float z) - Sets Size"}, {0} }; /*****************************************************************************/ -/* Python Curve_Type callback function prototypes: */ +/* Python Curve_Type callback function prototypes: */ /*****************************************************************************/ static void CurveDeAlloc (C_Curve *msh); static int CurvePrint (C_Curve *msh, FILE *fp, int flags); @@ -175,8 +194,9 @@ PyObject* CurveCreatePyObject (struct Curve *curve); int CurveCheckPyObject (PyObject *py_obj); struct Curve* CurveFromPyObject (PyObject *py_obj); + /*****************************************************************************/ -/* Python Curve_Type structure definition: */ +/* Python Curve_Type structure definition: */ /*****************************************************************************/ static PyTypeObject Curve_Type = { diff --git a/source/blender/python/api2_2x/NMesh.c b/source/blender/python/api2_2x/NMesh.c index 08eeddb35e6..13ceb064c98 100644 --- a/source/blender/python/api2_2x/NMesh.c +++ b/source/blender/python/api2_2x/NMesh.c @@ -24,7 +24,7 @@ * * This is a new part of Blender. * - * Contributor(s): Willian P. Germano + * Contributor(s): Willian P. Germano, Jordi Rovira i Bonet * * ***** END GPL/BL DUAL LICENSE BLOCK ***** */ @@ -684,6 +684,61 @@ static PyObject *NMesh_update(PyObject *self, PyObject *args) return PyInt_FromLong(1); } + +/** Implementation of the python method getVertexInfluence for an NMesh object. + * This method returns a list of pairs (string,float) with bone nemaes and influences that this vertex receives. + * @author Jordi Rovira i Bonet + */ +static PyObject *NMesh_getVertexInfluences(PyObject *self, PyObject *args) +{ + int index; + PyObject* influence_list = NULL; + + // Get a reference to the mesh object wrapped in here. + Mesh *me= ((C_NMesh*)self)->mesh; + + // Parse the parameters: only on integer (vertex index) + if (!PyArg_ParseTuple(args, "i", &index)) + return EXPP_ReturnPyObjError (PyExc_TypeError, + "expected int argument (index of the vertex)"); + + // Proceed only if we have vertex deformation information and index is valid + if (me->dvert) + if ((index>=0) && (index<me->totvert)) + { + int i; + MDeformWeight *sweight = NULL; + + // Number of bones influencig the vertex + int totinfluences=me->dvert[index].totweight; + + // Build the list only with weights and names of the influent bones + influence_list = PyList_New(totinfluences); + + //Get the reference of the first wwight structure + sweight = me->dvert[index].dw; + for (i=0; i<totinfluences; i++) { + + // Some check that should always be true + assert(sweight->data); + + //Add the weight and the name of the bone, which is used to identify it + PyList_SetItem(influence_list, i, Py_BuildValue("[sf]", sweight->data->name, sweight->weight)); + + //Next weight + sweight++; + } + } + else influence_list = PyList_New(0); + else influence_list = PyList_New(0); + + // Return the list. !QUESTION! Should i reincrement the number of references like i'm doing? + return EXPP_incr_ret(influence_list); + +} + + + Mesh *Mesh_fromNMesh(C_NMesh *nmesh) { Mesh *mesh = NULL; @@ -717,6 +772,7 @@ static struct PyMethodDef NMesh_methods[] = MethodDef(hasVertexUV), MethodDef(getActiveFace), MethodDef(getSelectedFaces), + MethodDef(getVertexInfluences), MethodDef(update), {NULL, NULL} }; diff --git a/source/blender/python/api2_2x/NMesh.h b/source/blender/python/api2_2x/NMesh.h index a1d31876080..3bbc1406cc9 100644 --- a/source/blender/python/api2_2x/NMesh.h +++ b/source/blender/python/api2_2x/NMesh.h @@ -56,6 +56,7 @@ #include "DNA_mesh_types.h" #include "DNA_object_types.h" #include "DNA_material_types.h" +#include "DNA_armature_types.h" #include "mydevice.h" #include "gen_utils.h" @@ -113,6 +114,12 @@ static char NMesh_hasVertexColours_doc[] = "(flag = None) - returns 1 if Mesh has vertex colours.\n\ The optional argument sets the vertex colour flag"; +static char NMesh_getVertexInfluences_doc[] = +"Return a list of the influences of bones in the vertex \n\ +specified by index. The list contains pairs with the \n\ +bone name and the weight."; + + static char NMesh_update_doc[] = "updates the Mesh"; /* static char NMesh_asMesh_doc[] = "returns free Mesh datablock object from NMesh"; diff --git a/source/blender/python/api2_2x/modules.h b/source/blender/python/api2_2x/modules.h index 71375aaddc8..7a0942a1150 100644 --- a/source/blender/python/api2_2x/modules.h +++ b/source/blender/python/api2_2x/modules.h @@ -39,6 +39,7 @@ #include <DNA_lamp_types.h> #include <DNA_curve_types.h> #include <DNA_effect_types.h> +#include <DNA_armature_types.h> /*****************************************************************************/ /* Global variables */ @@ -85,12 +86,17 @@ PyObject * CurveCreatePyObject (struct Curve *curve); struct Curve * CurveFromPyObject (PyObject *py_obj); int CurveCheckPyObject (PyObject *py_obj); +/* Armature Data */ +PyObject * M_Armature_Init (void); +PyObject * ArtmatureCreatePyObject (bArmature *armature); +bArmature* ArmatureFromPyObject (PyObject *py_obj); +int ArmatureCheckPyObject (PyObject *py_obj); + /* Particle Effects Data */ -/* -PyObject * M_Effect_Init (void); +/*PyObject * M_Effect_Init (void); PyObject * EffectCreatePyObject (struct Effect *effect); -int EffectCheckPyObject (PyObject *py_obj); struct Effect * EffectFromPyObject (PyObject *py_obj); +int EffectCheckPyObject (PyObject *py_obj); */ /* Init functions for other modules */ |