[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [17120] trunk/blender/source/blender/ python/api2_2x: === Blender Python API ===

Campbell Barton ideasman42 at gmail.com
Mon Oct 20 02:09:30 CEST 2008


Could it be better to add the __sizeof__() function to these types?

Python already has __sizeof__ and its not used for internal functions
so adding it would not break anything.

To copy pythons
int.__sizeof__(1) -> type(mesh).__sizeof__(mesh)

or in py 2.6 ... sys.getsizeof()
http://www.python.org/doc/2.6/library/sys.html#sys.getsizeof

Also interested in how your using this function.

On Mon, Oct 20, 2008 at 10:20 AM, Nathan Letwory <jesterking at letwory.net> wrote:
> Revision: 17120
>          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17120
> Author:   jesterking
> Date:     2008-10-20 01:20:17 +0200 (Mon, 20 Oct 2008)
>
> Log Message:
> -----------
> === Blender Python API ===
>
> * add DataSize() to module level
>  with this one can get datablock struct size.
>
> Modified Paths:
> --------------
>    trunk/blender/source/blender/python/api2_2x/Armature.c
>    trunk/blender/source/blender/python/api2_2x/Material.c
>    trunk/blender/source/blender/python/api2_2x/Mesh.c
>    trunk/blender/source/blender/python/api2_2x/Object.c
>
> Modified: trunk/blender/source/blender/python/api2_2x/Armature.c
> ===================================================================
> --- trunk/blender/source/blender/python/api2_2x/Armature.c      2008-10-19 22:09:45 UTC (rev 17119)
> +++ trunk/blender/source/blender/python/api2_2x/Armature.c      2008-10-19 23:20:17 UTC (rev 17120)
> @@ -923,7 +923,7 @@
>                sArmatureError, "You are not allowed to change the .Bones attribute");
>  }
>
> -//------------------------Bone.layerMask (get)
> +//------------------------Armature.layerMask (get)
>  static PyObject *Armature_getLayerMask(BPy_Armature *self)
>  {
>        /* do this extra stuff because the short's bits can be negative values */
> @@ -931,7 +931,7 @@
>        laymask |= self->armature->layer;
>        return PyInt_FromLong((int)laymask);
>  }
> -//------------------------Bone.layerMask (set)
> +//------------------------Armature.layerMask (set)
>  static int Armature_setLayerMask(BPy_Armature *self, PyObject *value)
>  {
>        int laymask;
> @@ -1295,7 +1295,27 @@
>        return (PyObject *)obj;
>  }
>
> +static PyObject *M_Armature_DataSize(PyObject * self, PyObject *args)
> +{
> +       int t = 0;
> +       int ret = 0;
> +       if( !PyArg_ParseTuple(args, "|i", &t))
> +               return EXPP_ReturnPyObjError( PyExc_TypeError,
> +                                               "expected nothing or an int as argument" );
> +
> +       switch(t) {
> +               case 0:
> +                       ret = sizeof(struct bArmature);
> +                       break;
> +               default:
> +                       ret = sizeof(struct Bone);
> +                       break;
> +       }
> +
> +       return PyInt_FromLong(ret);
> +}
>
> +
>  //-------------------MODULE METHODS DEFINITION-----------------------------
>
>  static char M_Armature_Get_doc[] = "(name) - return the armature with the name 'name', \
> @@ -1304,9 +1324,12 @@
>
>  static char M_Armature_New_doc[] = "(name) - return a new armature object.";
>
> +static char M_Armature_DataSize_doc[] = "(type) - return sizeof of either Armature (0) or Bone (1).";
> +
>  struct PyMethodDef M_Armature_methods[] = {
>        {"Get", M_Armature_Get, METH_VARARGS, M_Armature_Get_doc},
>        {"New", M_Armature_New, METH_VARARGS, M_Armature_New_doc},
> +       {"DataSize", M_Armature_DataSize, METH_VARARGS, M_Armature_DataSize_doc},
>        {NULL, NULL, 0, NULL}
>  };
>  //------------------VISIBLE PROTOTYPE IMPLEMENTATION-----------------------
>
> Modified: trunk/blender/source/blender/python/api2_2x/Material.c
> ===================================================================
> --- trunk/blender/source/blender/python/api2_2x/Material.c      2008-10-19 22:09:45 UTC (rev 17119)
> +++ trunk/blender/source/blender/python/api2_2x/Material.c      2008-10-19 23:20:17 UTC (rev 17120)
> @@ -207,6 +207,7 @@
>  static PyObject *M_Material_New( PyObject * self, PyObject * args,
>                                 PyObject * keywords );
>  static PyObject *M_Material_Get( PyObject * self, PyObject * args );
> +static PyObject *M_Material_DataSize(PyObject *unused);
>
>  /*****************************************************************************/
>  /* The following string definitions are used for documentation strings.  In  */
> @@ -231,6 +232,8 @@
>         M_Material_New_doc},
>        {"Get", M_Material_Get, METH_VARARGS, M_Material_Get_doc},
>        {"get", M_Material_Get, METH_VARARGS, M_Material_Get_doc},
> +       {"DataSize", ( PyCFunction ) M_Material_DataSize, METH_NOARGS,
> +               "Get sizeof() of Material"},
>        {NULL, NULL, 0, NULL}
>  };
>
> @@ -335,6 +338,12 @@
>        }
>  }
>
> +static PyObject *M_Material_DataSize(PyObject *unused)
> +{
> +       return PyInt_FromLong(sizeof(Material));
> +}
> +
> +
>  static PyObject *Material_ModesDict( void )
>  {
>        PyObject *Modes = PyConstant_New(  );
>
> Modified: trunk/blender/source/blender/python/api2_2x/Mesh.c
> ===================================================================
> --- trunk/blender/source/blender/python/api2_2x/Mesh.c  2008-10-19 22:09:45 UTC (rev 17119)
> +++ trunk/blender/source/blender/python/api2_2x/Mesh.c  2008-10-19 23:20:17 UTC (rev 17120)
> @@ -8637,6 +8637,35 @@
>        return PVert_CreatePyObject( &vert );
>  }
>
> +static PyObject *M_Mesh_DataSize(PyObject * self, PyObject *args)
> +{
> +       int t = 0;
> +       int ret = 0;
> +       if( !PyArg_ParseTuple(args, "|i", &t))
> +               return EXPP_ReturnPyObjError( PyExc_TypeError,
> +                                               "expected nothing or an int as argument" );
> +
> +       switch(t) {
> +               case 0:
> +                       ret = sizeof(Mesh);
> +                       break;
> +               case 1:
> +                       ret = sizeof(MVert);
> +                       break;
> +               case 2:
> +                       ret = sizeof(MEdge);
> +                       break;
> +               case 3:
> +                       ret = sizeof(MFace);
> +                       break;
> +               default:
> +                       ret = sizeof(Mesh);
> +                       break;
> +       }
> +
> +       return PyInt_FromLong(ret);
> +}
> +
>  static PyObject *M_Mesh_Modes( PyObject * self_unused, PyObject * args )
>  {
>        int modes = 0;
> @@ -8668,6 +8697,8 @@
>                "Create a new MVert"},
>        {"Mode", (PyCFunction)M_Mesh_Modes, METH_VARARGS,
>                "Get/set edit selection mode(s)"},
> +       {"DataSize", (PyCFunction)M_Mesh_DataSize, METH_VARARGS,
> +               "Get sizeof() of Mesh (0), MVert (1), MEdge (2) or MFace (3)"},
>        {NULL, NULL, 0, NULL},
>  };
>
>
> Modified: trunk/blender/source/blender/python/api2_2x/Object.c
> ===================================================================
> --- trunk/blender/source/blender/python/api2_2x/Object.c        2008-10-19 22:09:45 UTC (rev 17119)
> +++ trunk/blender/source/blender/python/api2_2x/Object.c        2008-10-19 23:20:17 UTC (rev 17120)
> @@ -290,6 +290,7 @@
>  PyObject *M_Object_Get( PyObject * self, PyObject * args );
>  static PyObject *M_Object_GetSelected( PyObject * self );
>  static PyObject *M_Object_Duplicate( PyObject * self, PyObject * args, PyObject *kwd);
> +static PyObject *M_Object_DataSize( PyObject * self );
>
>  /* HELPER FUNCTION FOR PARENTING */
>  static PyObject *internal_makeParent(Object *parent, PyObject *py_child, int partype, int noninverse, int fast, int v1, int v2, int v3, char *bonename);
> @@ -299,25 +300,27 @@
>  /* In Python these will be written to the console when doing a          */
>  /* Blender.Object.__doc__                                               */
>  /*****************************************************************************/
> -char M_Object_doc[] = "The Blender Object module\n\n\
> +static char M_Object_doc[] = "The Blender Object module\n\n\
>  This module provides access to **Object Data** in Blender.\n";
>
> -char M_Object_New_doc[] =
> +static char M_Object_New_doc[] =
>        "(type) - Add a new object of type 'type' in the current scene";
>
> -char M_Object_Get_doc[] =
> +static char M_Object_Get_doc[] =
>        "(name) - return the object with the name 'name', returns None if not\
>        found.\n\
>        If 'name' is not specified, it returns a list of all objects in the\n\
>        current scene.";
>
> -char M_Object_GetSelected_doc[] =
> +static char M_Object_GetSelected_doc[] =
>        "() - Returns a list of selected Objects in the active layer(s)\n\
>  The active object is the first in the list, if visible";
>
> -char M_Object_Duplicate_doc[] =
> +static char M_Object_Duplicate_doc[] =
>        "(linked) - Duplicate all selected, visible objects in the current scene";
>
> +static char M_Object_DataSize_doc[] =
> +       "() - return the sizeof(Object)";
>
>  /*****************************************************************************/
>  /* Python method structure definition for Blender.Object module:        */
> @@ -331,6 +334,8 @@
>         M_Object_GetSelected_doc},
>        {"Duplicate", ( PyCFunction ) M_Object_Duplicate, METH_VARARGS | METH_KEYWORDS,
>         M_Object_Duplicate_doc},
> +       {"DataSize", ( PyCFunction ) M_Object_DataSize, METH_NOARGS,
> +        M_Object_DataSize_doc},
>        {NULL, NULL, 0, NULL}
>  };
>
> @@ -1037,7 +1042,12 @@
>        Py_RETURN_NONE;
>  }
>
> +static PyObject *M_Object_DataSize(PyObject * self)
> +{
> +       return PyInt_FromLong(sizeof(Object));
> +}
>
> +
>  /*****************************************************************************/
>  /* Python BPy_Object methods:                                  */
>  /*****************************************************************************/
>
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
>



-- 
- Campbell


More information about the Bf-committers mailing list