From d73e312f885b4deb89835582827887ceae111165 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 13 Dec 2005 00:00:11 +0000 Subject: Added Image.GetCurrent() Previously the only way to get the current image was flaky and relyd on the image being assigned to a mesh. try: me = Scene.GetCurrent().getAttiveObject().getData(mesh=1) image = me.faces[me.activeFace].image except: image = None ...Can new be replaced by the following, and works even when there is no mesh. image = Image.GetCurrent() epydocs: Get the currently displayed Image from Blenders UV/Image window. When multiple images are displayed, the last active UV/Image windows image is used. --- source/blender/python/api2_2x/Image.c | 37 ++++++++++++++++++++++-------- source/blender/python/api2_2x/doc/Image.py | 7 ++++++ 2 files changed, 35 insertions(+), 9 deletions(-) (limited to 'source/blender/python') diff --git a/source/blender/python/api2_2x/Image.c b/source/blender/python/api2_2x/Image.c index b81fb1aa5d2..3e121fae59f 100644 --- a/source/blender/python/api2_2x/Image.c +++ b/source/blender/python/api2_2x/Image.c @@ -69,6 +69,7 @@ short IMB_saveiff( struct ImBuf *ibuf, char *naam, int flags ); /*static PyObject *M_Image_New( PyObject * self, PyObject * args, PyObject * keywords );*/ static PyObject *M_Image_Get( PyObject * self, PyObject * args ); +static PyObject *M_Image_GetCurrent( PyObject * self ); static PyObject *M_Image_Load( PyObject * self, PyObject * args ); /*****************************************************************************/ @@ -86,6 +87,10 @@ static char M_Image_Get_doc[] = returns None if not found.\n If 'name' is not specified, \ it returns a list of all images in the\ncurrent scene."; +static char M_Image_GetCurrent_doc[] = + "() - return the current image, from last active the uv/image view, \ +returns None no image is in the view.\n"; + static char M_Image_Load_doc[] = "(filename) - return image from file filename as Image Object, \ returns None if not found.\n"; @@ -97,6 +102,7 @@ struct PyMethodDef M_Image_methods[] = { /*{"New", ( PyCFunction ) M_Image_New, METH_VARARGS | METH_KEYWORDS, M_Image_New_doc}, */ {"Get", M_Image_Get, METH_VARARGS, M_Image_Get_doc}, + {"GetCurrent", M_Image_GetCurrent, METH_NOARGS, M_Image_GetCurrent_doc}, {"get", M_Image_Get, METH_VARARGS, M_Image_Get_doc}, {"Load", M_Image_Load, METH_VARARGS, M_Image_Load_doc}, {NULL, NULL, 0, NULL} @@ -170,11 +176,6 @@ static PyObject *M_Image_Get( PyObject * self, PyObject * args ) while( img_iter ) { pyobj = Image_CreatePyObject( img_iter ); - if( !pyobj ) - return ( EXPP_ReturnPyObjError - ( PyExc_MemoryError, - "couldn't create PyObject" ) ); - PyList_SET_ITEM( img_list, index, pyobj ); img_iter = img_iter->id.next; @@ -185,6 +186,26 @@ static PyObject *M_Image_Get( PyObject * self, PyObject * args ) } } + + +/*****************************************************************************/ +/* Function: M_Image_GetCurrent */ +/* Python equivalent: Blender.Image.GetCurrent */ +/* Description: Returns the active current (G.sima) */ +/* This will be the image last under the mouse cursor */ +/* None if there is no Image. */ +/*****************************************************************************/ +static PyObject *M_Image_GetCurrent( PyObject * self ) +{ + PyObject *current_img; + if (!G.sima || !G.sima->image) Py_RETURN_NONE; + current_img = Image_CreatePyObject( G.sima->image ); + return current_img; +} + + + + /*****************************************************************************/ /* Function: M_Image_Load */ /* Python equivalent: Blender.Image.Load */ @@ -674,15 +695,13 @@ static void Image_dealloc( BPy_Image * self ) PyObject *Image_CreatePyObject( Image * image ) { BPy_Image *py_img; - py_img = ( BPy_Image * ) PyObject_NEW( BPy_Image, &Image_Type ); - + if( !py_img ) return EXPP_ReturnPyObjError( PyExc_MemoryError, "couldn't create BPy_Image object" ); - + py_img->image = image; - return ( PyObject * ) py_img; } diff --git a/source/blender/python/api2_2x/doc/Image.py b/source/blender/python/api2_2x/doc/Image.py index aad3d84781b..1d25c550b3d 100644 --- a/source/blender/python/api2_2x/doc/Image.py +++ b/source/blender/python/api2_2x/doc/Image.py @@ -52,6 +52,13 @@ def Get (name = None): - (): A list with all Image objects in the current scene. """ +def GetCurrent (): + """ + Get the currently displayed Image from Blenders UV/Image window. + When multiple images are displayed, the last active UV/Image windows image is used. + @rtype: Blender Image + @return: The Current Blender Image, If there is no current image it returns None. + """ class Image: """ -- cgit v1.2.3