700
724
Py_RETURN_NONE; /* normal return, image saved */
727
static PyObject *M_Image_SourceDict( void )
729
PyObject *Dict = PyConstant_New( );
731
BPy_constant *d = ( BPy_constant * ) Dict;
732
PyConstant_Insert(d, "STILL", PyInt_FromLong(IMA_SRC_FILE));
733
PyConstant_Insert(d, "MOVIE", PyInt_FromLong(IMA_SRC_MOVIE));
734
PyConstant_Insert(d, "SEQUENCE", PyInt_FromLong(IMA_SRC_SEQUENCE));
735
PyConstant_Insert(d, "GENERATED", PyInt_FromLong(IMA_SRC_GENERATED));
703
740
/*****************************************************************************/
704
741
/* Function: Image_Init */
705
742
/*****************************************************************************/
706
743
PyObject *Image_Init( void )
708
745
PyObject *submodule;
746
PyObject *Sources = M_Image_SourceDict( );
710
Image_Type.ob_type = &PyType_Type;
748
if( PyType_Ready( &Image_Type ) < 0 )
713
752
Py_InitModule3( "Blender.Image", M_Image_methods,
716
return ( submodule );
756
PyModule_AddObject( submodule, "Sources", Sources );
719
761
/*****************************************************************************/
720
762
/* Python Image_Type callback function prototypes: */
721
763
/*****************************************************************************/
722
static void Image_dealloc( BPy_Image * self );
723
static int Image_setAttr( BPy_Image * self, char *name, PyObject * v );
724
764
static int Image_compare( BPy_Image * a, BPy_Image * b );
725
static PyObject *Image_getAttr( BPy_Image * self, char *name );
726
765
static PyObject *Image_repr( BPy_Image * self );
728
767
/*****************************************************************************/
729
/* Python Image_Type structure definition: */
730
/*****************************************************************************/
731
PyTypeObject Image_Type = {
732
PyObject_HEAD_INIT( NULL ) /* required macro. ( no comma needed ) */
734
"Blender Image", /* tp_name */
735
sizeof( BPy_Image ), /* tp_basicsize */
738
( destructor ) Image_dealloc, /* tp_dealloc */
740
( getattrfunc ) Image_getAttr, /* tp_getattr */
741
( setattrfunc ) Image_setAttr, /* tp_setattr */
742
( cmpfunc ) Image_compare, /* tp_compare */
743
( reprfunc ) Image_repr, /* tp_repr */
744
0, /* tp_as_number */
745
0, /* tp_as_sequence */
746
0, /* tp_as_mapping */
751
BPy_Image_methods, /* tp_methods */
753
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 */
756
/*****************************************************************************/
757
/* Function: Image_dealloc */
758
/* Description: This is a callback function for the BPy_Image type. It is */
759
/* the destructor function. */
760
/*****************************************************************************/
761
static void Image_dealloc( BPy_Image * self )
763
PyObject_DEL( self );
766
/*****************************************************************************/
767
768
/* Function: Image_CreatePyObject */
768
769
/* Description: This function will create a new BPy_Image from an existing */
769
770
/* Blender image structure. */
1107
1072
Py_RETURN_NONE;
1110
/*****************************************************************************/
1111
/* Function: Image_getAttr */
1112
/* Description: This is a callback function for the BPy_Image type. It is */
1113
/* the function that accesses BPy_Image member variables and */
1115
/*****************************************************************************/
1116
static PyObject *Image_getAttr( BPy_Image * self, char *name )
1118
PyObject *attr = Py_None;
1120
if( strcmp( name, "name" ) == 0 )
1121
attr = PyString_FromString( self->image->id.name + 2 );
1122
else if ( strcmp( name, "properties" ) == 0 )
1123
return BPy_Wrap_IDProperty( (ID*)self->image, IDP_GetProperties((ID*)self->image, 1), NULL );
1124
else if( strcmp( name, "filename" ) == 0 )
1125
attr = PyString_FromString( self->image->name );
1126
else if( strcmp( name, "size" ) == 0 )
1127
return Image_getSize( self );
1128
else if( strcmp( name, "depth" ) == 0 )
1129
return Image_getDepth( self );
1130
else if( strcmp( name, "xrep" ) == 0 )
1131
attr = PyInt_FromLong( self->image->xrep );
1132
else if( strcmp( name, "yrep" ) == 0 )
1133
attr = PyInt_FromLong( self->image->yrep );
1134
else if( strcmp( name, "start" ) == 0 )
1135
attr = PyInt_FromLong( self->image->twsta );
1136
else if( strcmp( name, "end" ) == 0 )
1137
attr = PyInt_FromLong( self->image->twend );
1138
else if( strcmp( name, "speed" ) == 0 )
1139
attr = PyInt_FromLong( self->image->animspeed );
1140
else if( strcmp( name, "packed" ) == 0 ) {
1141
if (self->image->packedfile) attr = Py_True;
1142
else attr = Py_False;
1143
EXPP_incr_ret(attr);
1144
} else if( strcmp( name, "has_data" ) == 0 ) {
1145
if (self->image->ibufs.first) attr = Py_True;
1146
else attr = Py_False;
1147
EXPP_incr_ret(attr);
1148
} else if( strcmp( name, "fields" ) == 0 ) {
1149
if (self->image->flag & IMA_FIELDS) attr = Py_True;
1150
else attr = Py_False;
1151
EXPP_incr_ret(attr);
1152
} else if( strcmp( name, "fields_odd" ) == 0 ) {
1153
if (self->image->flag & IMA_STD_FIELD) attr = Py_True;
1154
else attr = Py_False;
1155
EXPP_incr_ret(attr);
1156
} else if( strcmp( name, "antialias" ) == 0 ) {
1157
if (self->image->flag & IMA_ANTIALI) attr = Py_True;
1158
else attr = Py_False;
1159
EXPP_incr_ret(attr);
1161
} else if( strcmp( name, "bindcode" ) == 0 )
1162
attr = PyInt_FromLong( self->image->bindcode );
1163
else if( strcmp( name, "users" ) == 0 )
1164
attr = PyInt_FromLong( self->image->id.us );
1165
else if( strcmp( name, "__members__" ) == 0 )
1166
attr = Py_BuildValue( "[s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s]",
1167
"name", "filename", "size", "depth",
1168
"xrep", "yrep", "start", "end",
1169
"speed", "packed", "has_data"
1170
"fields", "odd", "antialias",
1171
"bindcode", "users" );
1174
return ( EXPP_ReturnPyObjError( PyExc_MemoryError,
1175
"couldn't create PyObject" ) );
1177
if( attr != Py_None )
1178
return attr; /* attribute found, return its value */
1180
/* not an attribute, search the methods table */
1181
return Py_FindMethod( BPy_Image_methods, ( PyObject * ) self, name );
1184
/*****************************************************************************/
1185
/* Function: Image_setAttr */
1186
/* Description: This is a callback function for the BPy_Image type. It is the*/
1187
/* function that changes Image object members values. If this */
1188
/* data is linked to a Blender Image, it also gets updated. */
1189
/*****************************************************************************/
1190
static int Image_setAttr( BPy_Image * self, char *name, PyObject * value )
1193
PyObject *error = NULL;
1195
/* We're playing a trick on the Python API users here. Even if they use
1196
* Image.member = val instead of Image.setMember(value), we end up using the
1197
* function anyway, since it already has error checking, clamps to the right
1198
* interval and updates the Blender Image structure when necessary. */
1200
valtuple = Py_BuildValue( "(O)", value ); /*the set* functions expect a tuple */
1203
return EXPP_ReturnIntError( PyExc_MemoryError,
1204
"ImageSetAttr: couldn't create PyTuple" );
1206
if( strcmp( name, "name" ) == 0 )
1207
error = Image_setName( self, valtuple );
1208
else if( strcmp( name, "filename" ) == 0 )
1209
error = Image_setFilename( self, valtuple );
1210
else if( strcmp( name, "xrep" ) == 0 )
1211
error = Image_setXRep( self, valtuple );
1212
else if( strcmp( name, "yrep" ) == 0 )
1213
error = Image_setYRep( self, valtuple );
1214
else if( strcmp( name, "start" ) == 0 )
1215
error = Image_setStart( self, valtuple );
1216
else if( strcmp( name, "end" ) == 0 )
1217
error = Image_setEnd( self, valtuple );
1218
else if( strcmp( name, "speed" ) == 0 )
1219
error = Image_setSpeed( self, valtuple );
1221
else if( strcmp( name, "fields" ) == 0 ) {
1222
int param = PyObject_IsTrue( value );
1224
return EXPP_ReturnIntError( PyExc_TypeError,
1225
"expected true/false argument" );
1227
if (param) self->image->flag |= IMA_FIELDS;
1228
else self->image->flag &= ~IMA_FIELDS;
1229
Py_INCREF( Py_None );
1231
} else if( strcmp( name, "fields_odd" ) == 0 ) {
1232
int param = PyObject_IsTrue( value );
1234
return EXPP_ReturnIntError( PyExc_TypeError,
1235
"expected true/false argument" );
1237
if (param) self->image->flag |= IMA_STD_FIELD;
1238
else self->image->flag &= ~IMA_STD_FIELD;
1239
Py_INCREF( Py_None );
1241
} else if( strcmp( name, "antialias" ) == 0 ) {
1242
int param = PyObject_IsTrue( value );
1244
return EXPP_ReturnIntError( PyExc_TypeError,
1245
"expected true/false argument" );
1247
if (param) self->image->flag |= IMA_ANTIALI;
1248
else self->image->flag &= ~IMA_ANTIALI;
1249
Py_INCREF( Py_None );
1251
} else { /* Error: no such member in the Image object structure */
1252
/*Py_DECREF( value ); borrowed ref, no need to decref */
1253
Py_DECREF( valtuple );
1254
return ( EXPP_ReturnIntError( PyExc_KeyError,
1255
"attribute not found or immutable" ) );
1258
Py_DECREF( valtuple );
1260
if( error != Py_None )
1263
Py_DECREF( Py_None ); /* incref'ed by the called set* function */
1264
return 0; /* normal exit */
1267
1076
/*****************************************************************************/
1268
1077
/* Function: Image_compare */
1287
1096
return PyString_FromFormat( "[Image \"%s\"]",
1288
1097
self->image->id.name + 2 );
1100
static PyObject *Image_getPacked(BPy_Image *self, void *closure)
1102
if (self->image->packedfile)
1108
static PyObject *Image_hasData(BPy_Image *self, void *closure)
1110
if (self->image->ibufs.first)
1116
static PyObject *Image_getFlag(BPy_Image *self, void *flag)
1118
if (self->image->flag & (int)flag)
1125
static PyObject *Image_getFlagTpage(BPy_Image *self, void *flag)
1127
if (self->image->tpageflag & (int)flag)
1134
static int Image_setSource( BPy_Image *self, PyObject *args)
1136
PyObject* integer = PyNumber_Int( args );
1140
return EXPP_ReturnIntError( PyExc_TypeError,
1141
"expected integer argument" );
1143
value = ( short )PyInt_AS_LONG( integer );
1144
Py_DECREF( integer );
1146
if( value < IMA_SRC_FILE || value > IMA_SRC_GENERATED )
1147
return EXPP_ReturnIntError( PyExc_ValueError,
1148
"expected integer argument in range 1-4" );
1150
self->image->source = value;
1154
static int Image_setFlag(BPy_Image *self, PyObject *value, void *flag)
1156
if ( PyObject_IsTrue(value) )
1157
self->image->flag |= (int)flag;
1159
self->image->flag &= ~(int)flag;
1163
static int Image_setFlagTpage(BPy_Image *self, PyObject *value, void *flag)
1165
if ( PyObject_IsTrue(value) )
1166
self->image->tpageflag |= (int)flag;
1168
self->image->tpageflag &= ~(int)flag;
1173
* get integer attributes
1175
static PyObject *getIntAttr( BPy_Image *self, void *type )
1177
PyObject *attr = NULL;
1179
struct Image *image = self->image;
1181
switch( (int)type ) {
1182
case EXPP_IMAGE_ATTR_XREP:
1183
param = image->xrep;
1185
case EXPP_IMAGE_ATTR_YREP:
1186
param = image->xrep;
1188
case EXPP_IMAGE_ATTR_START:
1189
param = image->twsta;
1191
case EXPP_IMAGE_ATTR_END:
1192
param = image->twend;
1194
case EXPP_IMAGE_ATTR_SPEED:
1195
param = image->animspeed;
1197
case EXPP_IMAGE_ATTR_BINDCODE:
1198
param = image->bindcode;
1200
case EXPP_IMAGE_ATTR_SOURCE:
1201
param = image->source;
1204
return EXPP_ReturnPyObjError( PyExc_RuntimeError,
1205
"undefined type in getIntAttr" );
1208
attr = PyInt_FromLong( param );
1213
return EXPP_ReturnPyObjError( PyExc_MemoryError,
1214
"PyInt_FromLong() failed!" );
1219
* set integer attributes which require clamping
1222
static int setIntAttrClamp( BPy_Image *self, PyObject *value, void *type )
1225
struct Image *image = self->image;
1228
switch( (int)type ) {
1229
case EXPP_IMAGE_ATTR_XREP:
1230
min = EXPP_IMAGE_REP_MIN;
1231
max = EXPP_IMAGE_REP_MAX;
1233
param = (void *)&image->xrep;
1235
case EXPP_IMAGE_ATTR_YREP:
1236
min = EXPP_IMAGE_REP_MIN;
1237
max = EXPP_IMAGE_REP_MAX;
1239
param = (void *)&image->yrep;
1241
case EXPP_IMAGE_ATTR_START:
1245
param = (void *)&image->twsta;
1247
case EXPP_IMAGE_ATTR_END:
1251
param = (void *)&image->twend;
1253
case EXPP_IMAGE_ATTR_SPEED:
1257
param = (void *)&image->animspeed;
1260
return EXPP_ReturnIntError( PyExc_RuntimeError,
1261
"undefined type in setIntAttrClamp");
1263
return EXPP_setIValueClamped( value, param, min, max, size );
1266
/*****************************************************************************/
1267
/* Python attributes get/set structure: */
1268
/*****************************************************************************/
1269
static PyGetSetDef BPy_Image_getseters[] = {
1270
GENERIC_LIB_GETSETATTR,
1271
{"filename", (getter)Image_getFilename, (setter)Image_setFilename,
1272
"image path", NULL},
1274
{"depth", (getter)Image_getDepth, (setter)NULL,
1275
"image depth", NULL},
1276
{"size", (getter)Image_getSize, (setter)NULL,
1277
"image size", NULL},
1278
{"packed", (getter)Image_getPacked, (setter)NULL,
1279
"image packed state", NULL },
1280
{"has_data", (getter)Image_hasData, (setter)NULL,
1281
"is image data loaded", NULL },
1283
{"xrep", (getter)getIntAttr, (setter)setIntAttrClamp,
1284
"image xrep", (void *)EXPP_IMAGE_ATTR_XREP },
1285
{"yrep", (getter)getIntAttr, (setter)setIntAttrClamp,
1286
"image yrep", (void *)EXPP_IMAGE_ATTR_YREP },
1287
{"start", (getter)getIntAttr, (setter)setIntAttrClamp,
1288
"image start frame", (void *)EXPP_IMAGE_ATTR_START },
1289
{"end", (getter)getIntAttr, (setter)setIntAttrClamp,
1290
"image end frame", (void *)EXPP_IMAGE_ATTR_END },
1291
{"speed", (getter)getIntAttr, (setter)setIntAttrClamp,
1292
"image end frame", (void *)EXPP_IMAGE_ATTR_SPEED },
1293
{"bindcode", (getter)getIntAttr, (setter)NULL,
1294
"openGL bindcode", (void *)EXPP_IMAGE_ATTR_BINDCODE },
1295
{"source", (getter)getIntAttr, (setter)Image_setSource,
1296
"image source type", (void *)EXPP_IMAGE_ATTR_SOURCE },
1298
{"fields", (getter)Image_getFlag, (setter)Image_setFlag,
1299
"image fields toggle", (void *)IMA_FIELDS },
1300
{"fields_odd", (getter)Image_getFlag, (setter)Image_setFlag,
1301
"image odd fields toggle", (void *)IMA_STD_FIELD },
1302
{"antialias", (getter)Image_getFlag, (setter)Image_setFlag,
1303
"image antialiasing toggle", (void *)IMA_ANTIALI },
1304
{"reflect", (getter)Image_getFlag, (setter)Image_setFlag,
1305
"image reflect toggle", (void *)IMA_REFLECT },
1306
{"clampX", (getter)Image_getFlagTpage, (setter)Image_setFlagTpage,
1307
"disable tiling on the X axis", (void *)IMA_CLAMP_U },
1308
{"clampY", (getter)Image_getFlagTpage, (setter)Image_setFlagTpage,
1309
"disable tiling on the Y axis", (void *)IMA_CLAMP_V },
1310
{NULL,NULL,NULL,NULL,NULL} /* Sentinel */
1314
/*****************************************************************************/
1315
/* Python Image_Type structure definition: */
1316
/*****************************************************************************/
1317
PyTypeObject Image_Type = {
1318
PyObject_HEAD_INIT( NULL ) /* required macro. ( no comma needed ) */
1320
"Blender Image", /* tp_name */
1321
sizeof( BPy_Image ), /* tp_basicsize */
1322
0, /* tp_itemsize */
1324
NULL, /* tp_dealloc */
1326
NULL, /* tp_getattr */
1327
NULL, /* tp_setattr */
1328
( cmpfunc ) Image_compare, /* tp_compare */
1329
( reprfunc ) Image_repr, /* tp_repr */
1331
/* Method suites for standard classes */
1333
NULL, /* PyNumberMethods *tp_as_number; */
1334
NULL, /* PySequenceMethods *tp_as_sequence; */
1335
NULL, /* PyMappingMethods *tp_as_mapping; */
1337
/* More standard operations (here for binary compatibility) */
1339
( hashfunc ) GenericLib_hash, /* hashfunc tp_hash; */
1340
NULL, /* ternaryfunc tp_call; */
1341
NULL, /* reprfunc tp_str; */
1342
NULL, /* getattrofunc tp_getattro; */
1343
NULL, /* setattrofunc tp_setattro; */
1345
/* Functions to access object as input/output buffer */
1346
NULL, /* PyBufferProcs *tp_as_buffer; */
1348
/*** Flags to define presence of optional/expanded features ***/
1349
Py_TPFLAGS_DEFAULT, /* long tp_flags; */
1351
NULL, /* char *tp_doc; Documentation string */
1352
/*** Assigned meaning in release 2.0 ***/
1353
/* call function for all accessible objects */
1354
NULL, /* traverseproc tp_traverse; */
1356
/* delete references to contained objects */
1357
NULL, /* inquiry tp_clear; */
1359
/*** Assigned meaning in release 2.1 ***/
1360
/*** rich comparisons ***/
1361
NULL, /* richcmpfunc tp_richcompare; */
1363
/*** weak reference enabler ***/
1364
0, /* long tp_weaklistoffset; */
1366
/*** Added in release 2.2 ***/
1368
NULL, /* getiterfunc tp_iter; */
1369
NULL, /* iternextfunc tp_iternext; */
1371
/*** Attribute descriptor and subclassing stuff ***/
1372
BPy_Image_methods, /* struct PyMethodDef *tp_methods; */
1373
NULL, /* struct PyMemberDef *tp_members; */
1374
BPy_Image_getseters, /* struct PyGetSetDef *tp_getset; */
1375
NULL, /* struct _typeobject *tp_base; */
1376
NULL, /* PyObject *tp_dict; */
1377
NULL, /* descrgetfunc tp_descr_get; */
1378
NULL, /* descrsetfunc tp_descr_set; */
1379
0, /* long tp_dictoffset; */
1380
NULL, /* initproc tp_init; */
1381
NULL, /* allocfunc tp_alloc; */
1382
NULL, /* newfunc tp_new; */
1383
/* Low-level free-memory routine */
1384
NULL, /* freefunc tp_free; */
1385
/* For PyObject_IS_GC */
1386
NULL, /* inquiry tp_is_gc; */
1387
NULL, /* PyObject *tp_bases; */
1388
/* method resolution order */
1389
NULL, /* PyObject *tp_mro; */
1390
NULL, /* PyObject *tp_cache; */
1391
NULL, /* PyObject *tp_subclasses; */
1392
NULL, /* PyObject *tp_weaklist; */