Revision: 38378
          
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38378
Author:   campbellbarton
Date:     2011-07-13 23:45:47 +0000 (Wed, 13 Jul 2011)
Log Message:
-----------
rename bgl.Buffer attribute list to a method, to_list() as used for IDProps, 
also made repr function nicer.

Modified Paths:
--------------
    trunk/blender/doc/python_api/rst/bgl.rst
    trunk/blender/source/blender/python/generic/bgl.c

Modified: trunk/blender/doc/python_api/rst/bgl.rst
===================================================================
--- trunk/blender/doc/python_api/rst/bgl.rst    2011-07-13 23:24:21 UTC (rev 
38377)
+++ trunk/blender/doc/python_api/rst/bgl.rst    2011-07-13 23:45:47 UTC (rev 
38378)
@@ -1379,16 +1379,17 @@
 
       .. code-block:: python
 
-        import Blender
-        from Blender.BGL import *
+        import bgl
         xval, yval= 100, 40
         # Get the scale of the view matrix
-        viewMatrix = Buffer(GL_FLOAT, 16)
-        glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix)
-        f = 1/viewMatrix[0]
-        glRasterPos2f(xval*f, yval*f) # Instead of the usual 
glRasterPos2i(xval, yval)
+        view_matrix = bgl.Buffer(bgl.GL_FLOAT, 16)
+        bgl.glGetFloatv(bgl.GL_MODELVIEW_MATRIX, view_matrix)
+        f = 1.0 / view_matrix[0]
 
+               # Instead of the usual glRasterPos2i(xval, yval)
+        bgl.glRasterPos2f(xval * f, yval * f)
 
+
 .. function:: glReadBuffer(mode):
 
    Select a color buffer source for pixels.
@@ -1839,32 +1840,32 @@
    The Buffer object is simply a block of memory that is delineated and 
initialized by the
    user. Many OpenGL functions return data to a C-style pointer, however, 
because this
    is not possible in python the Buffer object can be used to this end. 
Wherever pointer
-   notation is used in the OpenGL functions the Buffer object can be used in 
it's BGL
+   notation is used in the OpenGL functions the Buffer object can be used in 
it's bgl
    wrapper. In some instances the Buffer object will need to be initialized 
with the template
    parameter, while in other instances the user will want to create just a 
blank buffer
    which will be zeroed by default.
 
-   Example with Buffer::
-      import Blender
-      from Blender import BGL
-      myByteBuffer = BGL.Buffer(BGL.GL_BYTE, [32,32])
-      BGL.glGetPolygonStipple(myByteBuffer)
-      print myByteBuffer.dimensions
-      print myByteBuffer.list
+   .. code-block:: python
+
+      import bgl
+      myByteBuffer = bgl.Buffer(bgl.GL_BYTE, [32, 32])
+      bgl.glGetPolygonStipple(myByteBuffer)
+      print(myByteBuffer.dimensions)
+      print(myByteBuffer.to_list())
       sliceBuffer = myByteBuffer[0:16]
-      print sliceBuffer
+      print(sliceBuffer)
 
-   .. attribute:: list
+   .. attribute:: dimensions
 
-      The contents of the Buffer.
+      The number of dimensions of the Buffer.
 
-   .. attribute:: dimensions
+   .. method:: to_list()
 
-      The size of the Buffer.
+      The contents of the Buffer as a python list.
 
    .. method:: __init__(type, dimensions, template = None):
 
-      This will create a new Buffer object for use with other BGL OpenGL 
commands.
+      This will create a new Buffer object for use with other bgl OpenGL 
commands.
       Only the type of argument to store in the buffer and the dimensions of 
the buffer
       are necessary. Buffers are zeroed by default unless a template is 
supplied, in
       which case the buffer is initialized to the template.

Modified: trunk/blender/source/blender/python/generic/bgl.c
===================================================================
--- trunk/blender/source/blender/python/generic/bgl.c   2011-07-13 23:24:21 UTC 
(rev 38377)
+++ trunk/blender/source/blender/python/generic/bgl.c   2011-07-13 23:45:47 UTC 
(rev 38378)
@@ -72,6 +72,7 @@
 static PyObject *Buffer_tolist(PyObject *self, void *arg);
 static PyObject *Buffer_dimensions(PyObject *self, void *arg);
 static PyObject *Buffer_repr(PyObject *self);
+static PyMethodDef Buffer_methods[];
 static PyGetSetDef Buffer_getseters[];
 
 PyTypeObject BGL_bufferType = {
@@ -123,7 +124,7 @@
        NULL, /* getiterfunc tp_iter; */
        NULL,                       /* iternextfunc tp_iternext; */
        /*** Attribute descriptor and subclassing stuff ***/
-       NULL,        /* struct PyMethodDef *tp_methods; */
+       Buffer_methods,             /* struct PyMethodDef *tp_methods; */
        NULL,                       /* struct PyMemberDef *tp_members; */
        Buffer_getseters,           /* struct PyGetSetDef *tp_getset; */
        NULL,                                           /*tp_base*/
@@ -459,7 +460,7 @@
        PyObject_DEL(self);
 }
 
-static PyObject *Buffer_tolist(PyObject *self, void *UNUSED(arg))
+static PyObject *Buffer_to_list(PyObject *self)
 {
        int i, len= ((Buffer *)self)->dimensions[0];
        PyObject *list= PyList_New(len);
@@ -484,18 +485,35 @@
        return list;
 }
 
+static PyMethodDef Buffer_methods[] = {
+       {"to_list", (PyCFunction)Buffer_to_list, METH_NOARGS,
+     "return the buffer as a list"},
+       {NULL, NULL, 0, NULL}
+};
+
 static PyGetSetDef Buffer_getseters[] = {
-       {(char *)"list", (getter)Buffer_tolist, NULL, NULL, NULL},
        {(char *)"dimensions", (getter)Buffer_dimensions, NULL, NULL, NULL},
         {NULL, NULL, NULL, NULL, NULL}
 };
 
 static PyObject *Buffer_repr(PyObject *self)
 {
-       PyObject *list= Buffer_tolist(self, NULL);
-       PyObject *repr= PyObject_Repr(list);
+       PyObject *list= Buffer_to_list(self);
+       PyObject *repr;
+       const char *typestr= "UNKNOWN";
+       Buffer *buffer= (Buffer *)self;
+
+       switch(buffer->type) {
+       case GL_BYTE:   typestr= "GL_BYTE"; break;
+       case GL_SHORT:  typestr= "GL_SHORT"; break;
+       case GL_INT:    typestr= "GL_BYTE"; break;
+       case GL_FLOAT:  typestr= "GL_FLOAT"; break;
+       case GL_DOUBLE: typestr= "GL_DOUBLE"; break;
+       }
+
+       repr= PyUnicode_FromFormat("Buffer(%s, %R)", typestr, list);
        Py_DECREF(list);
-       
+
        return repr;
 }
 

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to