52
58
/************************/
54
60
/*****************************************************************************/
55
/* Python API function prototypes for the Image module. */
56
/*****************************************************************************/
57
static PyObject *M_Image_New (PyObject *self, PyObject *args,
59
static PyObject *M_Image_Get (PyObject *self, PyObject *args);
60
static PyObject *M_Image_Load (PyObject *self, PyObject *args);
62
/*****************************************************************************/
63
/* The following string definitions are used for documentation strings. */
64
/* In Python these will be written to the console when doing a */
65
/* Blender.Image.__doc__ */
66
/*****************************************************************************/
67
static char M_Image_doc[] =
68
"The Blender Image module\n\n";
70
static char M_Image_New_doc[] =
71
"() - return a new Image object -- unimplemented";
61
/* Python API function prototypes for the Image module. */
62
/*****************************************************************************/
63
/*static PyObject *M_Image_New( PyObject * self, PyObject * args,
64
PyObject * keywords );*/
65
static PyObject *M_Image_Get( PyObject * self, PyObject * args );
66
static PyObject *M_Image_Load( PyObject * self, PyObject * args );
68
/*****************************************************************************/
69
/* The following string definitions are used for documentation strings. */
70
/* In Python these will be written to the console when doing a */
71
/* Blender.Image.__doc__ */
72
/*****************************************************************************/
73
static char M_Image_doc[] = "The Blender Image module\n\n";
75
/*static char M_Image_New_doc[] =
76
"() - return a new Image object -- unimplemented";*/
73
78
static char M_Image_Get_doc[] =
74
"(name) - return the image with the name 'name', \
79
"(name) - return the image with the name 'name', \
75
80
returns None if not found.\n If 'name' is not specified, \
76
81
it returns a list of all images in the\ncurrent scene.";
78
83
static char M_Image_Load_doc[] =
79
"(filename) - return image from file filename as Image Object, \
84
"(filename) - return image from file filename as Image Object, \
80
85
returns None if not found.\n";
82
87
/*****************************************************************************/
83
/* Python method structure definition for Blender.Image module: */
88
/* Python method structure definition for Blender.Image module: */
84
89
/*****************************************************************************/
85
90
struct PyMethodDef M_Image_methods[] = {
86
{"New",(PyCFunction)M_Image_New, METH_VARARGS|METH_KEYWORDS,
88
{"Get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
89
{"get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
90
{"Load", M_Image_Load, METH_VARARGS, M_Image_Load_doc},
91
/*{"New", ( PyCFunction ) M_Image_New, METH_VARARGS | METH_KEYWORDS,
93
{"Get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
94
{"get", M_Image_Get, METH_VARARGS, M_Image_Get_doc},
95
{"Load", M_Image_Load, METH_VARARGS, M_Image_Load_doc},
94
99
/*****************************************************************************/
95
/* Function: M_Image_New */
96
/* Python equivalent: Blender.Image.New */
97
/*****************************************************************************/
98
static PyObject *M_Image_New(PyObject *self, PyObject *args, PyObject *keywords)
100
printf ("In Image_New() - unimplemented in 2.25\n");
106
/*****************************************************************************/
107
/* Function: M_Image_Get */
108
/* Python equivalent: Blender.Image.Get */
109
/* Description: Receives a string and returns the image object */
110
/* whose name matches the string. If no argument is */
111
/* passed in, a list of all image names in the */
112
/* current scene is returned. */
113
/*****************************************************************************/
114
static PyObject *M_Image_Get(PyObject *self, PyObject *args)
119
if (!PyArg_ParseTuple(args, "|s", &name))
120
return (EXPP_ReturnPyObjError (PyExc_TypeError,
121
"expected string argument (or nothing)"));
123
img_iter = G.main->image.first;
125
if (name) { /* (name) - Search image by name */
127
BPy_Image *wanted_image = NULL;
129
while ((img_iter) && (wanted_image == NULL)) {
130
if (strcmp (name, img_iter->id.name+2) == 0) {
131
wanted_image = (BPy_Image *)PyObject_NEW(BPy_Image, &Image_Type);
132
if (wanted_image) wanted_image->image = img_iter;
134
img_iter = img_iter->id.next;
137
if (wanted_image == NULL) { /* Requested image doesn't exist */
139
PyOS_snprintf(error_msg, sizeof(error_msg),
140
"Image \"%s\" not found", name);
141
return (EXPP_ReturnPyObjError (PyExc_NameError, error_msg));
144
return (PyObject *)wanted_image;
147
else { /* () - return a list of all images in the scene */
149
PyObject *img_list, *pyobj;
151
img_list = PyList_New (BLI_countlist (&(G.main->image)));
153
if (img_list == NULL)
154
return (PythonReturnErrorObject (PyExc_MemoryError,
155
"couldn't create PyList"));
158
pyobj = Image_CreatePyObject (img_iter);
161
return (PythonReturnErrorObject (PyExc_MemoryError,
162
"couldn't create PyObject"));
164
PyList_SET_ITEM (img_list, index, pyobj);
166
img_iter = img_iter->id.next;
174
/*****************************************************************************/
175
/* Function: M_Image_Load */
176
/* Python equivalent: Blender.Image.Load */
177
/* Description: Receives a string and returns the image object */
178
/* whose filename matches the string. */
179
/*****************************************************************************/
180
static PyObject *M_Image_Load(PyObject *self, PyObject *args)
186
if (!PyArg_ParseTuple(args, "s", &fname))
187
return (EXPP_ReturnPyObjError (PyExc_TypeError,
188
"expected string argument"));
190
img = (BPy_Image *)PyObject_NEW(BPy_Image, &Image_Type);
193
return (EXPP_ReturnPyObjError (PyExc_MemoryError,
194
"couldn't create PyObject Image_Type"));
196
img_ptr = add_image(fname);
198
return (EXPP_ReturnPyObjError (PyExc_IOError,
199
"couldn't load image"));
201
img->image = img_ptr;
203
return (PyObject *)img;
206
/*****************************************************************************/
207
/* Function: Image_Init */
208
/*****************************************************************************/
209
PyObject *Image_Init (void)
213
Image_Type.ob_type = &PyType_Type;
215
submodule = Py_InitModule3("Blender.Image", M_Image_methods, M_Image_doc);
100
/* Function: M_Image_New (unimplemented) */
101
/* Python equivalent: Blender.Image.New */
102
/*****************************************************************************/
103
/*static PyObject *M_Image_New( PyObject * self, PyObject * args,
104
PyObject * keywords )
106
Py_INCREF( Py_None );
110
/*****************************************************************************/
111
/* Function: M_Image_Get */
112
/* Python equivalent: Blender.Image.Get */
113
/* Description: Receives a string and returns the image object */
114
/* whose name matches the string. If no argument is */
115
/* passed in, a list of all image names in the */
116
/* current scene is returned. */
117
/*****************************************************************************/
118
static PyObject *M_Image_Get( PyObject * self, PyObject * args )
123
if( !PyArg_ParseTuple( args, "|s", &name ) )
124
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
125
"expected string argument (or nothing)" ) );
127
img_iter = G.main->image.first;
129
if( name ) { /* (name) - Search image by name */
131
BPy_Image *wanted_image = NULL;
133
while( ( img_iter ) && ( wanted_image == NULL ) ) {
134
if( strcmp( name, img_iter->id.name + 2 ) == 0 ) {
135
wanted_image = ( BPy_Image * )
136
PyObject_NEW( BPy_Image, &Image_Type );
138
wanted_image->image = img_iter;
140
img_iter = img_iter->id.next;
143
if( wanted_image == NULL ) { /* Requested image doesn't exist */
145
PyOS_snprintf( error_msg, sizeof( error_msg ),
146
"Image \"%s\" not found", name );
147
return ( EXPP_ReturnPyObjError
148
( PyExc_NameError, error_msg ) );
151
return ( PyObject * ) wanted_image;
154
else { /* () - return a list of all images in the scene */
156
PyObject *img_list, *pyobj;
158
img_list = PyList_New( BLI_countlist( &( G.main->image ) ) );
160
if( img_list == NULL )
161
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
162
"couldn't create PyList" ) );
165
pyobj = Image_CreatePyObject( img_iter );
168
return ( EXPP_ReturnPyObjError
170
"couldn't create PyObject" ) );
172
PyList_SET_ITEM( img_list, index, pyobj );
174
img_iter = img_iter->id.next;
182
/*****************************************************************************/
183
/* Function: M_Image_Load */
184
/* Python equivalent: Blender.Image.Load */
185
/* Description: Receives a string and returns the image object */
186
/* whose filename matches the string. */
187
/*****************************************************************************/
188
static PyObject *M_Image_Load( PyObject * self, PyObject * args )
194
if( !PyArg_ParseTuple( args, "s", &fname ) )
195
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
196
"expected string argument" ) );
198
img = ( BPy_Image * ) PyObject_NEW( BPy_Image, &Image_Type );
201
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
202
"couldn't create PyObject Image_Type" ) );
204
img_ptr = add_image( fname );
206
return ( EXPP_ReturnPyObjError( PyExc_IOError,
207
"couldn't load image" ) );
209
img->image = img_ptr;
211
return ( PyObject * ) img;
217
* returns float list of pixel colors in rgba order.
218
* returned values are floats normalized to 0.0 - 1.0.
219
* blender images are all 4x8 bit at the moment apr-2005
222
static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args )
226
Image *image = self->image;
227
char *pixel; /* image data */
228
int index; /* offset into image data */
231
int pixel_size = 4; /* each pixel is 4 x 8-bits packed in unsigned int */
233
if( !PyArg_ParseTuple( args, "ii", &x, &y ) )
234
return EXPP_ReturnPyObjError( PyExc_TypeError,
235
"expected 2 integers" );
237
if( !image->ibuf || !image->ibuf->rect ) /* if no image data available */
238
load_image( image, IB_rect, "", 0 ); /* loading it */
240
if( !image->ibuf || !image->ibuf->rect ) /* didn't work */
241
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
242
"couldn't load image data in Blender" );
244
if( image->ibuf->type == 1 ) /* bitplane image */
245
return EXPP_ReturnPyObjError( PyExc_TypeError,
246
"unsupported bitplane image format" );
248
if( x > ( image->ibuf->x - 1 )
249
|| y > ( image->ibuf->y - 1 )
250
|| x < image->ibuf->xorig || y < image->ibuf->yorig )
251
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
252
"x or y is out of range" );
255
assumption: from looking at source, skipx is often not set,
259
index = ( x + y * image->ibuf->x ) * pixel_size;
261
pixel = ( char * ) image->ibuf->rect;
262
attr = Py_BuildValue( "[f,f,f,f]",
263
( ( float ) pixel[index] ) / 255.0,
264
( ( float ) pixel[index + 1] ) / 255.0,
265
( ( float ) pixel[index + 2] ) / 255.0,
266
( ( float ) pixel[index + 3] / 255.0 ) );
268
if( attr ) /* normal return */
271
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
272
"couldn't get pixel colors" );
276
static PyObject *Image_getMaxXY( BPy_Image * self )
278
Image *image = self->image;
281
if( !image->ibuf || !image->ibuf->rect ) /* if no image data available */
282
load_image( image, IB_rect, "", 0 ); /* loading it */
284
if( !image->ibuf || !image->ibuf->rect ) /* didn't work */
285
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
286
"couldn't load image data in Blender" );
288
attr = Py_BuildValue( "[i,i]", image->ibuf->x, image->ibuf->y );
291
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
292
"could not determine max x or y" );
296
/*****************************************************************************/
297
/* Function: Image_Init */
298
/*****************************************************************************/
299
PyObject *Image_Init( void )
303
Image_Type.ob_type = &PyType_Type;
306
Py_InitModule3( "Blender.Image", M_Image_methods,
309
return ( submodule );
220
312
/************************/
222
314
/************************/
224
316
/*****************************************************************************/
225
/* Python BPy_Image methods declarations: */
317
/* Python BPy_Image methods declarations: */
226
318
/*****************************************************************************/
227
static PyObject *Image_getName(BPy_Image *self);
228
static PyObject *Image_getFilename(BPy_Image *self);
229
static PyObject *Image_getSize(BPy_Image *self);
230
static PyObject *Image_getDepth(BPy_Image *self);
231
static PyObject *Image_getXRep(BPy_Image *self);
232
static PyObject *Image_getYRep(BPy_Image *self);
233
static PyObject *Image_setName(BPy_Image *self, PyObject *args);
234
static PyObject *Image_setXRep(BPy_Image *self, PyObject *args);
235
static PyObject *Image_setYRep(BPy_Image *self, PyObject *args);
319
static PyObject *Image_getName( BPy_Image * self );
320
static PyObject *Image_getFilename( BPy_Image * self );
321
static PyObject *Image_getSize( BPy_Image * self );
322
static PyObject *Image_getDepth( BPy_Image * self );
323
static PyObject *Image_getXRep( BPy_Image * self );
324
static PyObject *Image_getYRep( BPy_Image * self );
325
static PyObject *Image_getBindCode( BPy_Image * self );
326
static PyObject *Image_setName( BPy_Image * self, PyObject * args );
327
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args );
328
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args );
329
static PyObject *Image_setYRep( BPy_Image * self, PyObject * args );
330
static PyObject *Image_reload( BPy_Image * self );
331
static PyObject *Image_glLoad( BPy_Image * self );
332
static PyObject *Image_glFree( BPy_Image * self );
333
static PyObject *Image_getPixelF( BPy_Image * self, PyObject * args );
334
static PyObject *Image_getMaxXY( BPy_Image * self );
237
336
/*****************************************************************************/
238
/* Python BPy_Image methods table: */
337
/* Python BPy_Image methods table: */
239
338
/*****************************************************************************/
240
339
static PyMethodDef BPy_Image_methods[] = {
241
/* name, method, flags, doc */
242
{"getName", (PyCFunction)Image_getName, METH_NOARGS,
243
"() - Return Image object name"},
244
{"getFilename", (PyCFunction)Image_getFilename, METH_NOARGS,
245
"() - Return Image object filename"},
246
{"getSize", (PyCFunction)Image_getSize, METH_NOARGS,
247
"() - Return Image object [width, height] dimension in pixels"},
248
{"getDepth", (PyCFunction)Image_getDepth, METH_NOARGS,
249
"() - Return Image object pixel depth"},
250
{"getXRep", (PyCFunction)Image_getXRep, METH_NOARGS,
251
"() - Return Image object x repetition value"},
252
{"getYRep", (PyCFunction)Image_getYRep, METH_NOARGS,
253
"() - Return Image object y repetition value"},
254
{"setName", (PyCFunction)Image_setName, METH_VARARGS,
255
"(str) - Change Image object name"},
256
{"setXRep", (PyCFunction)Image_setXRep, METH_VARARGS,
257
"(int) - Change Image object x repetition value"},
258
{"setYRep", (PyCFunction)Image_setYRep, METH_VARARGS,
259
"(int) - Change Image object y repetition value"},
263
/*****************************************************************************/
264
/* Python Image_Type callback function prototypes: */
265
/*****************************************************************************/
266
static void Image_dealloc (BPy_Image *self);
267
static int Image_setAttr (BPy_Image *self, char *name, PyObject *v);
268
static int Image_compare (BPy_Image *a, BPy_Image *b);
269
static PyObject *Image_getAttr (BPy_Image *self, char *name);
270
static PyObject *Image_repr (BPy_Image *self);
272
/*****************************************************************************/
273
/* Python Image_Type structure definition: */
274
/*****************************************************************************/
275
PyTypeObject Image_Type =
277
PyObject_HEAD_INIT(NULL)
279
"Blender Image", /* tp_name */
280
sizeof (BPy_Image), /* tp_basicsize */
283
(destructor)Image_dealloc, /* tp_dealloc */
285
(getattrfunc)Image_getAttr, /* tp_getattr */
286
(setattrfunc)Image_setAttr, /* tp_setattr */
287
(cmpfunc)Image_compare, /* tp_compare */
288
(reprfunc)Image_repr, /* tp_repr */
289
0, /* tp_as_number */
290
0, /* tp_as_sequence */
291
0, /* tp_as_mapping */
296
BPy_Image_methods, /* tp_methods */
300
/*****************************************************************************/
301
/* Function: Image_dealloc */
302
/* Description: This is a callback function for the BPy_Image type. It is */
303
/* the destructor function. */
304
/*****************************************************************************/
305
static void Image_dealloc (BPy_Image *self)
340
/* name, method, flags, doc */
341
{"getPixelF", ( PyCFunction ) Image_getPixelF, METH_VARARGS,
342
"(float, float) - Get colors of specified pixel as [r,g,b,a]"},
343
{"getMaxXY", ( PyCFunction ) Image_getMaxXY, METH_VARARGS,
344
"() - Get maximum x & y coordinates of current image as [x, y]"},
345
{"getName", ( PyCFunction ) Image_getName, METH_NOARGS,
346
"() - Return Image object name"},
347
{"getFilename", ( PyCFunction ) Image_getFilename, METH_NOARGS,
348
"() - Return Image object filename"},
349
{"getSize", ( PyCFunction ) Image_getSize, METH_NOARGS,
350
"() - Return Image object [width, height] dimension in pixels"},
351
{"getDepth", ( PyCFunction ) Image_getDepth, METH_NOARGS,
352
"() - Return Image object pixel depth"},
353
{"getXRep", ( PyCFunction ) Image_getXRep, METH_NOARGS,
354
"() - Return Image object x repetition value"},
355
{"getYRep", ( PyCFunction ) Image_getYRep, METH_NOARGS,
356
"() - Return Image object y repetition value"},
357
{"getBindCode", ( PyCFunction ) Image_getBindCode, METH_NOARGS,
358
"() - Return Image object's bind code value"},
359
{"reload", ( PyCFunction ) Image_reload, METH_NOARGS,
360
"() - Reload the image from the filesystem"},
361
{"glLoad", ( PyCFunction ) Image_glLoad, METH_NOARGS,
362
"() - Load the image data in OpenGL texture memory.\n\
363
The bindcode (int) is returned."},
364
{"glFree", ( PyCFunction ) Image_glFree, METH_NOARGS,
365
"() - Free the image data from OpenGL texture memory only,\n\
366
see also image.glLoad()."},
367
{"setName", ( PyCFunction ) Image_setName, METH_VARARGS,
368
"(str) - Change Image object name"},
369
{"setFilename", ( PyCFunction ) Image_setFilename, METH_VARARGS,
370
"(str) - Change Image file name"},
371
{"setXRep", ( PyCFunction ) Image_setXRep, METH_VARARGS,
372
"(int) - Change Image object x repetition value"},
373
{"setYRep", ( PyCFunction ) Image_setYRep, METH_VARARGS,
374
"(int) - Change Image object y repetition value"},
375
{NULL, NULL, 0, NULL}
378
/*****************************************************************************/
379
/* Python Image_Type callback function prototypes: */
380
/*****************************************************************************/
381
static void Image_dealloc( BPy_Image * self );
382
static int Image_setAttr( BPy_Image * self, char *name, PyObject * v );
383
static int Image_compare( BPy_Image * a, BPy_Image * b );
384
static PyObject *Image_getAttr( BPy_Image * self, char *name );
385
static PyObject *Image_repr( BPy_Image * self );
387
/*****************************************************************************/
388
/* Python Image_Type structure definition: */
389
/*****************************************************************************/
390
PyTypeObject Image_Type = {
391
PyObject_HEAD_INIT( NULL ) /* required macro. ( no comma needed ) */
393
"Blender Image", /* tp_name */
394
sizeof( BPy_Image ), /* tp_basicsize */
397
( destructor ) Image_dealloc, /* tp_dealloc */
399
( getattrfunc ) Image_getAttr, /* tp_getattr */
400
( setattrfunc ) Image_setAttr, /* tp_setattr */
401
( cmpfunc ) Image_compare, /* tp_compare */
402
( reprfunc ) Image_repr, /* tp_repr */
403
0, /* tp_as_number */
404
0, /* tp_as_sequence */
405
0, /* tp_as_mapping */
410
BPy_Image_methods, /* tp_methods */
412
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* up to tp_del, to avoid a warning */
415
/*****************************************************************************/
416
/* Function: Image_dealloc */
417
/* Description: This is a callback function for the BPy_Image type. It is */
418
/* the destructor function. */
419
/*****************************************************************************/
420
static void Image_dealloc( BPy_Image * self )
422
PyObject_DEL( self );
310
425
/*****************************************************************************/
311
/* Function: Image_CreatePyObject */
312
/* Description: This function will create a new BPy_Image from an existing */
313
/* Blender image structure. */
426
/* Function: Image_CreatePyObject */
427
/* Description: This function will create a new BPy_Image from an existing */
428
/* Blender image structure. */
314
429
/*****************************************************************************/
315
PyObject *Image_CreatePyObject (Image *image)
430
PyObject *Image_CreatePyObject( Image * image )
317
432
BPy_Image *py_img;
319
py_img = (BPy_Image *)PyObject_NEW (BPy_Image, &Image_Type);
434
py_img = ( BPy_Image * ) PyObject_NEW( BPy_Image, &Image_Type );
322
return EXPP_ReturnPyObjError (PyExc_MemoryError,
323
"couldn't create BPy_Image object");
437
return EXPP_ReturnPyObjError( PyExc_MemoryError,
438
"couldn't create BPy_Image object" );
325
440
py_img->image = image;
327
return (PyObject *)py_img;
442
return ( PyObject * ) py_img;
330
445
/*****************************************************************************/
331
/* Function: Image_CheckPyObject */
446
/* Function: Image_CheckPyObject */
332
447
/* Description: This function returns true when the given PyObject is of the */
333
/* type Image. Otherwise it will return false. */
334
/*****************************************************************************/
335
int Image_CheckPyObject (PyObject *pyobj)
337
return (pyobj->ob_type == &Image_Type);
340
/*****************************************************************************/
341
/* Function: Image_FromPyObject */
342
/* Description: Returns the Blender Image associated with this object */
343
/*****************************************************************************/
344
Image *Image_FromPyObject (PyObject *pyobj)
346
return ((BPy_Image *)pyobj)->image;
349
/*****************************************************************************/
350
/* Python BPy_Image methods: */
351
/*****************************************************************************/
352
static PyObject *Image_getName(BPy_Image *self)
354
PyObject *attr = PyString_FromString(self->image->id.name+2);
356
if (attr) return attr;
358
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
359
"couldn't get Image.name attribute"));
362
static PyObject *Image_getFilename(BPy_Image *self)
364
PyObject *attr = PyString_FromString(self->image->name);
366
if (attr) return attr;
368
return (EXPP_ReturnPyObjError (PyExc_RuntimeError,
369
"couldn't get Image.filename attribute"));
372
static PyObject *Image_getSize(BPy_Image *self)
375
Image *image = self->image;
377
if (!image->ibuf) /* if no image data available */
378
load_image(image, IB_rect, "", 0); /* loading it */
380
if (!image->ibuf) /* didn't work */
381
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
382
"couldn't load image data in Blender");
384
attr = Py_BuildValue("[hh]", image->ibuf->x, image->ibuf->y);
386
if (attr) return attr;
388
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
389
"couldn't get Image.size attribute");
392
static PyObject *Image_getDepth(BPy_Image *self)
395
Image *image = self->image;
397
if (!image->ibuf) /* if no image data available */
398
load_image(image, IB_rect, "", 0); /* loading it */
400
if (!image->ibuf) /* didn't work */
401
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
402
"couldn't load image data in Blender");
404
attr = Py_BuildValue("h", image->ibuf->depth);
406
if (attr) return attr;
408
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
409
"couldn't get Image.depth attribute");
413
static PyObject *Image_getXRep(BPy_Image *self)
415
PyObject *attr = PyInt_FromLong(self->image->xrep);
417
if (attr) return attr;
419
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
420
"couldn't get Image.xrep attribute");
423
static PyObject *Image_getYRep(BPy_Image *self)
425
PyObject *attr = PyInt_FromLong(self->image->yrep);
427
if (attr) return attr;
429
return EXPP_ReturnPyObjError (PyExc_RuntimeError,
430
"couldn't get Image.yrep attribute");
433
static PyObject *Image_setName(BPy_Image *self, PyObject *args)
438
if (!PyArg_ParseTuple(args, "s", &name))
439
return (EXPP_ReturnPyObjError (PyExc_TypeError,
440
"expected string argument"));
442
PyOS_snprintf(buf, sizeof(buf), "%s", name);
444
rename_id(&self->image->id, buf);
450
static PyObject *Image_setXRep(BPy_Image *self, PyObject *args)
454
if (!PyArg_ParseTuple(args, "h", &value))
455
return (EXPP_ReturnPyObjError (PyExc_TypeError,
456
"expected int argument in [1,16]"));
458
if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX)
459
self->image->xrep = value;
461
return (EXPP_ReturnPyObjError (PyExc_ValueError,
462
"expected int argument in [1,16]"));
468
static PyObject *Image_setYRep(BPy_Image *self, PyObject *args)
472
if (!PyArg_ParseTuple(args, "h", &value))
473
return (EXPP_ReturnPyObjError (PyExc_TypeError,
474
"expected int argument in [1,16]"));
476
if (value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX)
477
self->image->yrep = value;
479
return (EXPP_ReturnPyObjError (PyExc_ValueError,
480
"expected int argument in [1,16]"));
486
/*****************************************************************************/
487
/* Function: Image_getAttr */
488
/* Description: This is a callback function for the BPy_Image type. It is */
489
/* the function that accesses BPy_Image member variables and */
491
/*****************************************************************************/
492
static PyObject *Image_getAttr (BPy_Image *self, char *name)
494
PyObject *attr = Py_None;
496
if (strcmp(name, "name") == 0)
497
attr = PyString_FromString(self->image->id.name+2);
498
else if (strcmp(name, "filename") == 0)
499
attr = PyString_FromString(self->image->name);
500
else if (strcmp(name, "size") == 0)
501
attr = Image_getSize(self);
502
else if (strcmp(name, "depth") == 0)
503
attr = Image_getDepth(self);
504
else if (strcmp(name, "xrep") == 0)
505
attr = PyInt_FromLong(self->image->xrep);
506
else if (strcmp(name, "yrep") == 0)
507
attr = PyInt_FromLong(self->image->yrep);
509
else if (strcmp(name, "__members__") == 0)
510
attr = Py_BuildValue("[s,s,s,s,s,s]",
511
"name", "filename", "size", "depth", "xrep", "yrep");
514
return (EXPP_ReturnPyObjError (PyExc_MemoryError,
515
"couldn't create PyObject"));
517
if (attr != Py_None) return attr; /* attribute found, return its value */
519
/* not an attribute, search the methods table */
520
return Py_FindMethod(BPy_Image_methods, (PyObject *)self, name);
523
/*****************************************************************************/
524
/* Function: Image_setAttr */
448
/* type Image. Otherwise it will return false. */
449
/*****************************************************************************/
450
int Image_CheckPyObject( PyObject * pyobj )
452
return ( pyobj->ob_type == &Image_Type );
455
/*****************************************************************************/
456
/* Function: Image_FromPyObject */
457
/* Description: Returns the Blender Image associated with this object */
458
/*****************************************************************************/
459
Image *Image_FromPyObject( PyObject * pyobj )
461
return ( ( BPy_Image * ) pyobj )->image;
464
/*****************************************************************************/
465
/* Python BPy_Image methods: */
466
/*****************************************************************************/
467
static PyObject *Image_getName( BPy_Image * self )
469
PyObject *attr = PyString_FromString( self->image->id.name + 2 );
474
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
475
"couldn't get Image.name attribute" ) );
478
static PyObject *Image_getFilename( BPy_Image * self )
480
PyObject *attr = PyString_FromString( self->image->name );
485
return ( EXPP_ReturnPyObjError( PyExc_RuntimeError,
486
"couldn't get Image.filename attribute" ) );
489
static PyObject *Image_getSize( BPy_Image * self )
492
Image *image = self->image;
494
if( !image->ibuf ) /* if no image data available */
495
load_image( image, IB_rect, "", 0 ); /* loading it */
497
if( !image->ibuf ) /* didn't work */
498
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
499
"couldn't load image data in Blender" );
501
attr = Py_BuildValue( "[hh]", image->ibuf->x, image->ibuf->y );
506
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
507
"couldn't get Image.size attribute" );
510
static PyObject *Image_getDepth( BPy_Image * self )
513
Image *image = self->image;
515
if( !image->ibuf ) /* if no image data available */
516
load_image( image, IB_rect, "", 0 ); /* loading it */
518
if( !image->ibuf ) /* didn't work */
519
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
520
"couldn't load image data in Blender" );
522
attr = Py_BuildValue( "h", image->ibuf->depth );
527
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
528
"couldn't get Image.depth attribute" );
532
static PyObject *Image_getXRep( BPy_Image * self )
534
PyObject *attr = PyInt_FromLong( self->image->xrep );
539
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
540
"couldn't get Image.xrep attribute" );
543
static PyObject *Image_getYRep( BPy_Image * self )
545
PyObject *attr = PyInt_FromLong( self->image->yrep );
550
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
551
"couldn't get Image.yrep attribute" );
554
static PyObject *Image_getBindCode( BPy_Image * self )
556
PyObject *attr = PyLong_FromUnsignedLong( self->image->bindcode );
561
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
562
"couldn't get Image.bindcode attribute" );
565
static PyObject *Image_reload( BPy_Image * self )
567
Image *img = self->image;
569
free_image_buffers( img ); /* force read again */
572
image_changed( G.sima, 0 );
574
Py_INCREF( Py_None );
578
static PyObject *Image_glFree( BPy_Image * self )
580
Image *img = self->image;
582
free_realtime_image( img );
583
return EXPP_incr_ret( Py_None );
586
static PyObject *Image_glLoad( BPy_Image * self )
588
Image *img = self->image;
589
unsigned int *bind = &img->bindcode;
593
if( !img->ibuf ) /* if no image data is available */
594
load_image( img, IB_rect, "", 0 ); /* loading it */
596
if( !img->ibuf ) /* didn't work */
597
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
598
"couldn't load image data in Blender" );
600
glGenTextures( 1, ( GLuint * ) bind );
601
glBindTexture( GL_TEXTURE_2D, *bind );
603
gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGBA, img->ibuf->x,
604
img->ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE,
606
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
607
GL_LINEAR_MIPMAP_NEAREST );
608
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
610
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
612
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, img->ibuf->x,
613
img->ibuf->y, 0, GL_RGBA, GL_UNSIGNED_BYTE,
617
return PyLong_FromUnsignedLong( img->bindcode );
620
static PyObject *Image_setName( BPy_Image * self, PyObject * args )
625
if( !PyArg_ParseTuple( args, "s", &name ) )
626
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
627
"expected string argument" ) );
629
PyOS_snprintf( buf, sizeof( buf ), "%s", name );
631
rename_id( &self->image->id, buf );
633
Py_INCREF( Py_None );
637
static PyObject *Image_setFilename( BPy_Image * self, PyObject * args )
642
/* max len is FILE_MAXDIR = 160 chars like done in DNA_image_types.h */
644
if( !PyArg_ParseTuple( args, "s#", &name, &namelen ) )
645
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
646
"expected a string argument" ) );
648
if( namelen >= FILE_MAXDIR )
649
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
650
"string argument is limited to 160 chars at most" ) );
652
PyOS_snprintf( self->image->name, FILE_MAXDIR * sizeof( char ), "%s",
655
Py_INCREF( Py_None );
659
static PyObject *Image_setXRep( BPy_Image * self, PyObject * args )
663
if( !PyArg_ParseTuple( args, "h", &value ) )
664
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
665
"expected int argument in [1,16]" ) );
667
if( value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX )
668
self->image->xrep = value;
670
return ( EXPP_ReturnPyObjError( PyExc_ValueError,
671
"expected int argument in [1,16]" ) );
673
Py_INCREF( Py_None );
677
static PyObject *Image_setYRep( BPy_Image * self, PyObject * args )
681
if( !PyArg_ParseTuple( args, "h", &value ) )
682
return ( EXPP_ReturnPyObjError( PyExc_TypeError,
683
"expected int argument in [1,16]" ) );
685
if( value >= EXPP_IMAGE_REP_MIN || value <= EXPP_IMAGE_REP_MAX )
686
self->image->yrep = value;
688
return ( EXPP_ReturnPyObjError( PyExc_ValueError,
689
"expected int argument in [1,16]" ) );
691
Py_INCREF( Py_None );
695
/*****************************************************************************/
696
/* Function: Image_getAttr */
697
/* Description: This is a callback function for the BPy_Image type. It is */
698
/* the function that accesses BPy_Image member variables and */
700
/*****************************************************************************/
701
static PyObject *Image_getAttr( BPy_Image * self, char *name )
703
PyObject *attr = Py_None;
705
if( strcmp( name, "name" ) == 0 )
706
attr = PyString_FromString( self->image->id.name + 2 );
707
else if( strcmp( name, "filename" ) == 0 )
708
attr = PyString_FromString( self->image->name );
709
else if( strcmp( name, "size" ) == 0 )
710
attr = Image_getSize( self );
711
else if( strcmp( name, "depth" ) == 0 )
712
attr = Image_getDepth( self );
713
else if( strcmp( name, "xrep" ) == 0 )
714
attr = PyInt_FromLong( self->image->xrep );
715
else if( strcmp( name, "yrep" ) == 0 )
716
attr = PyInt_FromLong( self->image->yrep );
717
else if( strcmp( name, "bindcode" ) == 0 )
718
attr = PyInt_FromLong( self->image->bindcode );
719
else if( strcmp( name, "users" ) == 0 )
720
attr = PyInt_FromLong( self->image->id.us );
721
else if( strcmp( name, "__members__" ) == 0 )
722
attr = Py_BuildValue( "[s,s,s,s,s,s,s,s]",
723
"name", "filename", "size", "depth",
724
"xrep", "yrep", "bindcode", "users" );
727
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
728
"couldn't create PyObject" ) );
730
if( attr != Py_None )
731
return attr; /* attribute found, return its value */
733
/* not an attribute, search the methods table */
734
return Py_FindMethod( BPy_Image_methods, ( PyObject * ) self, name );
737
/*****************************************************************************/
738
/* Function: Image_setAttr */
525
739
/* Description: This is a callback function for the BPy_Image type. It is the*/
526
/* function that changes Image object members values. If this */
527
/* data is linked to a Blender Image, it also gets updated. */
740
/* function that changes Image object members values. If this */
741
/* data is linked to a Blender Image, it also gets updated. */
528
742
/*****************************************************************************/
529
static int Image_setAttr (BPy_Image *self, char *name, PyObject *value)
743
static int Image_setAttr( BPy_Image * self, char *name, PyObject * value )
532
PyObject *error = NULL;
746
PyObject *error = NULL;
534
/* We're playing a trick on the Python API users here. Even if they use
748
/* We're playing a trick on the Python API users here. Even if they use
535
749
* Image.member = val instead of Image.setMember(value), we end up using the
536
750
* function anyway, since it already has error checking, clamps to the right
537
751
* interval and updates the Blender Image structure when necessary. */
539
valtuple = Py_BuildValue("(O)", value); /*the set* functions expect a tuple*/
542
return EXPP_ReturnIntError(PyExc_MemoryError,
543
"ImageSetAttr: couldn't create PyTuple");
545
if (strcmp (name, "name") == 0)
546
error = Image_setName (self, valtuple);
547
else if (strcmp (name, "xrep") == 0)
548
error = Image_setXRep (self, valtuple);
549
else if (strcmp (name, "yrep") == 0)
550
error = Image_setYRep (self, valtuple);
551
else { /* Error: no such member in the Image object structure */
554
return (EXPP_ReturnIntError (PyExc_KeyError,
555
"attribute not found or immutable"));
560
if (error != Py_None) return -1;
562
Py_DECREF(Py_None); /* incref'ed by the called set* function */
563
return 0; /* normal exit */
753
valtuple = Py_BuildValue( "(O)", value ); /*the set* functions expect a tuple */
756
return EXPP_ReturnIntError( PyExc_MemoryError,
757
"ImageSetAttr: couldn't create PyTuple" );
759
if( strcmp( name, "name" ) == 0 )
760
error = Image_setName( self, valtuple );
761
if( strcmp( name, "filename" ) == 0 )
762
error = Image_setFilename( self, valtuple );
763
else if( strcmp( name, "xrep" ) == 0 )
764
error = Image_setXRep( self, valtuple );
765
else if( strcmp( name, "yrep" ) == 0 )
766
error = Image_setYRep( self, valtuple );
767
else { /* Error: no such member in the Image object structure */
769
Py_DECREF( valtuple );
770
return ( EXPP_ReturnIntError( PyExc_KeyError,
771
"attribute not found or immutable" ) );
774
Py_DECREF( valtuple );
776
if( error != Py_None )
779
Py_DECREF( Py_None ); /* incref'ed by the called set* function */
780
return 0; /* normal exit */
566
783
/*****************************************************************************/
567
/* Function: Image_compare */
568
/* Description: This is a callback function for the BPy_Image type. It */
569
/* compares two Image_Type objects. Only the "==" and "!=" */
570
/* comparisons are meaninful. Returns 0 for equality and -1 if */
571
/* they don't point to the same Blender Image struct. */
572
/* In Python it becomes 1 if they are equal, 0 otherwise. */
784
/* Function: Image_compare */
785
/* Description: This is a callback function for the BPy_Image type. It */
786
/* compares two Image_Type objects. Only the "==" and "!=" */
787
/* comparisons are meaninful. Returns 0 for equality and -1 if */
788
/* they don't point to the same Blender Image struct. */
789
/* In Python it becomes 1 if they are equal, 0 otherwise. */
573
790
/*****************************************************************************/
574
static int Image_compare (BPy_Image *a, BPy_Image *b)
791
static int Image_compare( BPy_Image * a, BPy_Image * b )
576
793
Image *pa = a->image, *pb = b->image;
577
return (pa == pb) ? 0:-1;
794
return ( pa == pb ) ? 0 : -1;
580
797
/*****************************************************************************/
581
/* Function: Image_repr */
582
/* Description: This is a callback function for the BPy_Image type. It */
583
/* builds a meaninful string to represent image objects. */
798
/* Function: Image_repr */
799
/* Description: This is a callback function for the BPy_Image type. It */
800
/* builds a meaninful string to represent image objects. */
584
801
/*****************************************************************************/
585
static PyObject *Image_repr (BPy_Image *self)
802
static PyObject *Image_repr( BPy_Image * self )
587
return PyString_FromFormat("[Image \"%s\"]", self->image->id.name+2);
804
return PyString_FromFormat( "[Image \"%s\"]",
805
self->image->id.name + 2 );