Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Texture.c « api2_2x « python « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6c6dda84ed960ac9becc084ea59d049d63a15f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
/* 
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA    02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * This is a new part of Blender.
 *
 * Contributor(s): Alex Mole
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
*/

#include <BKE_main.h>
#include <BKE_global.h>
#include <BKE_object.h>
#include <BKE_library.h>
#include <BLI_blenlib.h>
#include <BKE_texture.h>
#include <BKE_utildefines.h>

#include "MTex.h"
#include "Texture.h"
#include "Image.h"
#include "constant.h"
#include "gen_utils.h"
#include "modules.h"


/*****************************************************************************/
/* Blender.Texture constants                                                 */
/*****************************************************************************/
#define EXPP_TEX_TYPE_NONE                  0
#define EXPP_TEX_TYPE_CLOUDS                TEX_CLOUDS
#define EXPP_TEX_TYPE_WOOD                  TEX_WOOD
#define EXPP_TEX_TYPE_MARBLE                TEX_MARBLE
#define EXPP_TEX_TYPE_MAGIC                 TEX_MAGIC
#define EXPP_TEX_TYPE_BLEND                 TEX_BLEND
#define EXPP_TEX_TYPE_STUCCI                TEX_STUCCI
#define EXPP_TEX_TYPE_NOISE                 TEX_NOISE
#define EXPP_TEX_TYPE_IMAGE                 TEX_IMAGE
#define EXPP_TEX_TYPE_PLUGIN                TEX_PLUGIN
#define EXPP_TEX_TYPE_ENVMAP                TEX_ENVMAP

#define EXPP_TEX_TYPE_MIN                   EXPP_TEX_TYPE_NONE
#define EXPP_TEX_TYPE_MAX                   EXPP_TEX_TYPE_ENVMAP

/* i can't find these defined anywhere- they're just taken from looking at   */
/* the button creation code in source/blender/src/buttons_shading.c          */
#define EXPP_TEX_STYPE_CLD_DEFAULT          0
#define EXPP_TEX_STYPE_CLD_COLOR            1
#define EXPP_TEX_STYPE_WOD_BANDS            0
#define EXPP_TEX_STYPE_WOD_RINGS            1
#define EXPP_TEX_STYPE_WOD_BANDNOISE        2
#define EXPP_TEX_STYPE_WOD_RINGNOISE        3
#define EXPP_TEX_STYPE_MAG_DEFAULT          0
#define EXPP_TEX_STYPE_MBL_SOFT             0
#define EXPP_TEX_STYPE_MBL_SHARP            1
#define EXPP_TEX_STYPE_MBL_SHARPER          2
#define EXPP_TEX_STYPE_BLN_LIN              0
#define EXPP_TEX_STYPE_BLN_QUAD             1
#define EXPP_TEX_STYPE_BLN_EASE             2
#define EXPP_TEX_STYPE_BLN_DIAG             3
#define EXPP_TEX_STYPE_BLN_SPHERE           4
#define EXPP_TEX_STYPE_BLN_HALO             5
#define EXPP_TEX_STYPE_STC_PLASTIC          0
#define EXPP_TEX_STYPE_STC_WALLIN           1
#define EXPP_TEX_STYPE_STC_WALLOUT          2
#define EXPP_TEX_STYPE_NSE_DEFAULT          0
#define EXPP_TEX_STYPE_IMG_DEFAULT          0
#define EXPP_TEX_STYPE_PLG_DEFAULT          0
#define EXPP_TEX_STYPE_ENV_STATIC           0
#define EXPP_TEX_STYPE_ENV_ANIM             1
#define EXPP_TEX_STYPE_ENV_LOAD             2

#define EXPP_TEX_FLAG_COLORBAND             TEX_COLORBAND
#define EXPP_TEX_FLAG_FLIPBLEND             TEX_FLIPBLEND
#define EXPP_TEX_FLAG_NEGALPHA              TEX_NEGALPHA

#define EXPP_TEX_IMAGEFLAG_INTERPOL         TEX_INTERPOL
#define EXPP_TEX_IMAGEFLAG_USEALPHA         TEX_USEALPHA
#define EXPP_TEX_IMAGEFLAG_MIPMAP           TEX_MIPMAP
#define EXPP_TEX_IMAGEFLAG_FIELDS           TEX_FIELDS
#define EXPP_TEX_IMAGEFLAG_ROT90            TEX_IMAROT
#define EXPP_TEX_IMAGEFLAG_CALCALPHA        TEX_CALCALPHA
#define EXPP_TEX_IMAGEFLAG_CYCLIC           TEX_ANIMCYCLIC
#define EXPP_TEX_IMAGEFLAG_MOVIE            TEX_ANIM5
#define EXPP_TEX_IMAGEFLAG_STFIELD          TEX_STD_FIELD
#define EXPP_TEX_IMAGEFLAG_ANTI             TEX_ANTIALI

#define EXPP_TEX_EXTEND_EXTEND              TEX_EXTEND
#define EXPP_TEX_EXTEND_CLIP                TEX_CLIP
#define EXPP_TEX_EXTEND_REPEAT              TEX_REPEAT
#define EXPP_TEX_EXTEND_CLIPCUBE            TEX_CLIPCUBE

#define EXPP_TEX_EXTEND_MIN                 EXPP_TEX_EXTEND_EXTEND
#define EXPP_TEX_EXTEND_MAX                 EXPP_TEX_EXTEND_CLIPCUBE

#define EXPP_TEX_TEXCO_ORCO                 TEXCO_ORCO
#define EXPP_TEX_TEXCO_REFL                 TEXCO_REFL
#define EXPP_TEX_TEXCO_NOR                  TEXCO_NORM
#define EXPP_TEX_TEXCO_GLOB                 TEXCO_GLOB
#define EXPP_TEX_TEXCO_UV                   TEXCO_UV
#define EXPP_TEX_TEXCO_OBJECT               TEXCO_OBJECT
#define EXPP_TEX_TEXCO_WIN                  TEXCO_WINDOW
#define EXPP_TEX_TEXCO_VIEW                 TEXCO_VIEW
#define EXPP_TEX_TEXCO_STICK                TEXCO_STICKY

#define EXPP_TEX_MAPTO_COL                  MAP_COL
#define EXPP_TEX_MAPTO_NOR                  MAP_NORM
#define EXPP_TEX_MAPTO_CSP                  MAP_COLSPEC
#define EXPP_TEX_MAPTO_CMIR                 MAP_COLMIR
#define EXPP_TEX_MAPTO_REF                  MAP_REF
#define EXPP_TEX_MAPTO_SPEC                 MAP_SPEC
#define EXPP_TEX_MAPTO_HARD                 MAP_HAR
#define EXPP_TEX_MAPTO_ALPHA                MAP_ALPHA
#define EXPP_TEX_MAPTO_EMIT                 MAP_EMIT

/****************************************************************************/
/* Texture String->Int maps                                                 */
/****************************************************************************/

static const EXPP_map_pair tex_type_map[] = {
    { "None",   EXPP_TEX_TYPE_NONE },
    { "Clouds", EXPP_TEX_TYPE_CLOUDS },
    { "Wood",   EXPP_TEX_TYPE_WOOD },
    { "Marble", EXPP_TEX_TYPE_MARBLE },
    { "Magic",  EXPP_TEX_TYPE_MAGIC },
    { "Blend",  EXPP_TEX_TYPE_BLEND },
    { "Stucci", EXPP_TEX_TYPE_STUCCI },
    { "Noise",  EXPP_TEX_TYPE_NOISE },
    { "Image",  EXPP_TEX_TYPE_IMAGE },
    { "Plugin", EXPP_TEX_TYPE_PLUGIN },
    { "EnvMap", EXPP_TEX_TYPE_ENVMAP },
    { NULL, 0 }
};

static const EXPP_map_pair tex_flag_map[] = {
    /* we don't support this yet! */
/*    { "ColorBand",  EXPP_TEX_FLAG_COLORBAND },  */
    { "FlipBlend",  EXPP_TEX_FLAG_FLIPBLEND },
    { "NegAlpha",   EXPP_TEX_FLAG_NEGALPHA },
    { NULL, 0 }
};

static const EXPP_map_pair tex_imageflag_map[] = {
    { "InterPol",   EXPP_TEX_IMAGEFLAG_INTERPOL },
    { "UseAlpha",   EXPP_TEX_IMAGEFLAG_USEALPHA },
    { "MipMap",     EXPP_TEX_IMAGEFLAG_MIPMAP },
    { "Fields",     EXPP_TEX_IMAGEFLAG_FIELDS },
    { "Rot90",      EXPP_TEX_IMAGEFLAG_ROT90 },
    { "CalcAlpha",  EXPP_TEX_IMAGEFLAG_CALCALPHA },
    { "Cyclic",     EXPP_TEX_IMAGEFLAG_CYCLIC },
    { "Movie",      EXPP_TEX_IMAGEFLAG_MOVIE },
    { "StField",    EXPP_TEX_IMAGEFLAG_STFIELD },
    { "Anti",       EXPP_TEX_IMAGEFLAG_ANTI },
    { NULL, 0 }
};

static const EXPP_map_pair tex_extend_map[] = {
    { "Extend",     EXPP_TEX_EXTEND_EXTEND },
    { "Clip",       EXPP_TEX_EXTEND_CLIP },
    { "ClipCube",   EXPP_TEX_EXTEND_CLIPCUBE },
    { "Repeat",     EXPP_TEX_EXTEND_REPEAT },
    { NULL, 0 }
};

/* array of maps for stype */
static const EXPP_map_pair tex_stype_default_map[] = { 
    { "Default", 0 }, 
    { NULL, 0 } 
};
static const EXPP_map_pair tex_stype_clouds_map[] = {
    { "Default",        0 }, 
    { "CloudDefault",   EXPP_TEX_STYPE_CLD_DEFAULT },
    { "CloudColor",     EXPP_TEX_STYPE_CLD_COLOR }, 
    { NULL, 0 }
};
static const EXPP_map_pair tex_stype_wood_map[] = {
    { "Default",        0 }, 
    { "WoodBands",      EXPP_TEX_STYPE_WOD_BANDS },
    { "WoodRings",      EXPP_TEX_STYPE_WOD_RINGS },
    { "WoodBandNoise",  EXPP_TEX_STYPE_WOD_BANDNOISE },
    { "WoodRingNoise",  EXPP_TEX_STYPE_WOD_RINGNOISE },
    { NULL, 0 }
};
static const EXPP_map_pair tex_stype_marble_map[] = {
    { "Default",        0 }, 
    { "MarbleSoft",     EXPP_TEX_STYPE_MBL_SOFT },
    { "MarbleSharp",    EXPP_TEX_STYPE_MBL_SHARP },
    { "MarbleSharper",  EXPP_TEX_STYPE_MBL_SHARPER },
    { NULL , 0 }
};
static const EXPP_map_pair tex_stype_blend_map[] = {
    { "Default",        0 }, 
    { "BlendLin",       EXPP_TEX_STYPE_BLN_LIN },
    { "BlendQuad",      EXPP_TEX_STYPE_BLN_QUAD },
    { "BlendEase",      EXPP_TEX_STYPE_BLN_EASE },
    { "BlendDiag",      EXPP_TEX_STYPE_BLN_DIAG },
    { "BlendSphere",    EXPP_TEX_STYPE_BLN_SPHERE },
    { "BlendHalo",      EXPP_TEX_STYPE_BLN_HALO },
    { NULL , 0 }
};
static const EXPP_map_pair tex_stype_stucci_map[] = {
    { "Default",        0 }, 
    { "StucciPlastic",  EXPP_TEX_STYPE_STC_PLASTIC },
    { "StucciWallIn",   EXPP_TEX_STYPE_STC_WALLIN },
    { "StucciWallOut",  EXPP_TEX_STYPE_STC_WALLOUT },
    { NULL , 0 }
};
static const EXPP_map_pair tex_stype_envmap_map[] = {
    { "Default",        0 }, 
    { "EnvmapStatic",   EXPP_TEX_STYPE_ENV_STATIC },
    { "EnvmapAnim",     EXPP_TEX_STYPE_ENV_ANIM },
    { "EnvmapLoad",     EXPP_TEX_STYPE_ENV_LOAD },
    { NULL , 0 }
};

static const EXPP_map_pair *tex_stype_map[] = {
    tex_stype_default_map,    /* none */
    tex_stype_clouds_map,
    tex_stype_wood_map,
    tex_stype_marble_map,
    tex_stype_default_map,    /* magic */
    tex_stype_blend_map,
    tex_stype_stucci_map,
    tex_stype_default_map,    /* noise */
    tex_stype_default_map,    /* image */
    tex_stype_default_map,    /* plugin */
    tex_stype_envmap_map
};


/*****************************************************************************/
/* Python API function prototypes for the Texture module.                    */
/*****************************************************************************/
static PyObject *M_Texture_New (PyObject *self, PyObject *args,
                               PyObject *keywords);
static PyObject *M_Texture_Get (PyObject *self, PyObject *args);

/*****************************************************************************/
/* The following string definitions are used for documentation strings.      */
/* In Python these will be written to the console when doing a               */
/* Blender.Texture.__doc__                                                   */
/*****************************************************************************/
static char M_Texture_doc[] =
"The Blender Texture module\n\
\n\
This module provides access to **Texture** objects in Blender\n";

static char M_Texture_New_doc[] =
"Texture.New (name = 'Tex'):\n\
        Return a new Texture object with the given type and name.";

static char M_Texture_Get_doc[] =
"Texture.Get (name = None):\n\
        Return the texture with the given 'name', None if not found, or\n\
        Return a list with all texture objects in the current scene,\n\
        if no argument was given.";

/*****************************************************************************/
/* Python method structure definition for Blender.Texture module:            */
/*****************************************************************************/
struct PyMethodDef M_Texture_methods[] = {
  {"New", (PyCFunction) M_Texture_New,  METH_VARARGS|METH_KEYWORDS, 
                                                        M_Texture_New_doc},
  {"Get",   M_Texture_Get,  METH_VARARGS,               M_Texture_Get_doc},
  {NULL, NULL, 0, NULL}
};

/*****************************************************************************/
/* Python BPy_Texture methods declarations:                                   */
/*****************************************************************************/
#define GETFUNC(name)   static PyObject *Texture_##name(BPy_Texture *self)
#define SETFUNC(name)   static PyObject *Texture_##name(BPy_Texture *self,   \
                                                        PyObject *args)

GETFUNC (getExtend);
GETFUNC (getImage);
GETFUNC (getName);
GETFUNC (getType);
GETFUNC (getSType);
SETFUNC (setAnimFrames);
SETFUNC (setAnimLength);
SETFUNC (setAnimMontage);
SETFUNC (setAnimOffset);
SETFUNC (setAnimStart);
SETFUNC (setBrightness);
SETFUNC (setContrast);
SETFUNC (setCrop);
SETFUNC (setExtend);
SETFUNC (setIntExtend);     /* special case used for ".extend = ..." */
SETFUNC (setFieldsPerImage);
SETFUNC (setFilterSize);
SETFUNC (setFlags);
SETFUNC (setIntFlags);      /* special case used for ".flags = ..." */
SETFUNC (setImage);
SETFUNC (setImageFlags);
SETFUNC (setIntImageFlags); /* special case used for ".imageFlags = ..." */
SETFUNC (setName);
SETFUNC (setNoiseDepth);
SETFUNC (setNoiseSize);
SETFUNC (setNoiseType);
SETFUNC (setRepeat);
SETFUNC (setRGBCol);
SETFUNC (setSType);
SETFUNC (setIntSType);      /* special case used for ".stype = ..." */
SETFUNC (setType);
SETFUNC (setIntType);       /* special case used for ".type = ..." */
SETFUNC (setTurbulence);

/*****************************************************************************/
/* Python BPy_Texture methods table:                                          */
/*****************************************************************************/
static PyMethodDef BPy_Texture_methods[] = {
 /* name, method, flags, doc */
  {"getExtend", (PyCFunction)Texture_getExtend, METH_NOARGS,
                            "() - Return Texture extend mode"},
  {"getImage", (PyCFunction)Texture_getImage, METH_NOARGS,
                            "() - Return Texture Image"},
  {"getName", (PyCFunction)Texture_getName, METH_NOARGS,
                            "() - Return Texture name"},
  {"getSType", (PyCFunction)Texture_getSType, METH_NOARGS,
                            "() - Return Texture stype as string"},
  {"getType", (PyCFunction)Texture_getType, METH_NOARGS,
                            "() - Return Texture type as string"},
  {"setExtend", (PyCFunction)Texture_setExtend, METH_VARARGS,
                            "(s) - Set Texture extend mode"},
  {"setFlags", (PyCFunction)Texture_setFlags, METH_VARARGS,
                            "(f1,f2,f3) - Set Texture flags"},
  {"setImage", (PyCFunction)Texture_setImage, METH_VARARGS,
                            "(Blender Image) - Set Texture Image"},
  {"setImageFlags", (PyCFunction)Texture_setImageFlags, METH_VARARGS,
                            "(s,s,s,s,...) - Set Texture image flags"},
  {"setName", (PyCFunction)Texture_setName, METH_VARARGS,
                            "(s) - Set Texture name"},
  {"setSType", (PyCFunction)Texture_setSType, METH_VARARGS,
                            "(s) - Set Texture stype"},
  {"setType", (PyCFunction)Texture_setType, METH_VARARGS,
                            "(s) - Set Texture type"},
  {NULL, NULL, 0, NULL}
};

/*****************************************************************************/
/* Python Texture_Type callback function prototypes:                          */
/*****************************************************************************/
static void Texture_dealloc (BPy_Texture *self);
static int Texture_setAttr (BPy_Texture *self, char *name, PyObject *v);
static int Texture_compare (BPy_Texture *a, BPy_Texture *b);
static PyObject *Texture_getAttr (BPy_Texture *self, char *name);
static PyObject *Texture_repr (BPy_Texture *self);


/*****************************************************************************/
/* Python Texture_Type structure definition:                                 */
/*****************************************************************************/
PyTypeObject Texture_Type =
{
    PyObject_HEAD_INIT(NULL)
    0,                              /* ob_size */
    "Blender Texture",              /* tp_name */
    sizeof (BPy_Texture),           /* tp_basicsize */
    0,                              /* tp_itemsize */
    /* methods */
    (destructor)Texture_dealloc,    /* tp_dealloc */
    0,                              /* tp_print */
    (getattrfunc)Texture_getAttr,   /* tp_getattr */
    (setattrfunc)Texture_setAttr,   /* tp_setattr */
    (cmpfunc)Texture_compare,       /* tp_compare */
    (reprfunc)Texture_repr,         /* tp_repr */
    0,                              /* tp_as_number */
    0,                              /* tp_as_sequence */
    0,                              /* tp_as_mapping */
    0,                              /* tp_as_hash */
    0,0,0,0,0,0,
    0,                              /* tp_doc */ 
    0,0,0,0,0,0,
    BPy_Texture_methods,            /* tp_methods */
    0,                              /* tp_members */
};

static PyObject *M_Texture_New(PyObject *self, PyObject *args, PyObject *kwords)
{
    char        *name_str = "Tex";
    static char *kwlist[] = {"name_str", NULL};
    PyObject    *pytex;     /* for Texture object wrapper in Python */
    Tex         *bltex;     /* for actual Tex we create in Blender */

    /* Parse the arguments passed in by the Python interpreter */
    if (!PyArg_ParseTupleAndKeywords(args, kwords, "|s", kwlist, &name_str))
        return EXPP_ReturnPyObjError(PyExc_AttributeError,
                        "expected zero, one or two strings as arguments");

    bltex = add_texture(name_str);  /* first create the texture in Blender */

    if (bltex)              /* now create the wrapper obj in Python */
        pytex = Texture_CreatePyObject(bltex);
    else
        return EXPP_ReturnPyObjError(PyExc_RuntimeError,
                                    "couldn't create Texture in Blender");

    /* let's return user count to zero, because add_texture() incref'd it */
    bltex->id.us = 0;

    if (pytex == NULL)
        return EXPP_ReturnPyObjError (PyExc_MemoryError,
                                            "couldn't create Tex PyObject");

    return pytex;
}

static PyObject *M_Texture_Get(PyObject *self, PyObject *args)
{
    char    * name = NULL;
    Tex     * tex_iter;

    if (!PyArg_ParseTuple(args, "|s", &name))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                               "expected string argument (or nothing)");

    tex_iter = G.main->tex.first;

    if (name) { /* (name) - Search for texture by name */
        
        PyObject *wanted_tex = NULL;

        while (tex_iter) {            
            if (STREQ(name, tex_iter->id.name+2)) {
                wanted_tex = Texture_CreatePyObject (tex_iter);
                break;
            }

            tex_iter = tex_iter->id.next;
        }

        if (!wanted_tex) { /* Requested texture doesn't exist */
            char error_msg[64];
            PyOS_snprintf(error_msg, sizeof(error_msg),
                                            "Texture \"%s\" not found", name);
            return EXPP_ReturnPyObjError (PyExc_NameError, error_msg);
        }

        return wanted_tex;
    }

    else { /* () - return a list of wrappers for all textures in the scene */
        int index = 0;
        PyObject *tex_pylist, *pyobj;

        tex_pylist = PyList_New (BLI_countlist (&(G.main->tex)));
        if (!tex_pylist)
            return EXPP_ReturnPyObjError(PyExc_MemoryError,
                                            "couldn't create PyList");

        while (tex_iter) {
            pyobj = Texture_CreatePyObject(tex_iter);
            if (!pyobj)
                return EXPP_ReturnPyObjError(PyExc_MemoryError,
                                    "couldn't create Texture PyObject");

            PyList_SET_ITEM(tex_pylist, index, pyobj);

            tex_iter = tex_iter->id.next;
            index++;
        }

        return tex_pylist;
    }
}


#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_TYPE_##name))
    
static PyObject *M_Texture_TypesDict (void)
{
    PyObject *Types = M_constant_New();
    if (Types) {
        BPy_constant *d = (BPy_constant*) Types;
        
        EXPP_ADDCONST (NONE);
        EXPP_ADDCONST (CLOUDS);
        EXPP_ADDCONST (WOOD);
        EXPP_ADDCONST (MARBLE);
        EXPP_ADDCONST (MAGIC);
        EXPP_ADDCONST (BLEND);
        EXPP_ADDCONST (STUCCI);
        EXPP_ADDCONST (NOISE);
        EXPP_ADDCONST (IMAGE);
        EXPP_ADDCONST (PLUGIN);
        EXPP_ADDCONST (ENVMAP);
    }
    return Types;
}

#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_STYPE_##name))
    
static PyObject *M_Texture_STypesDict (void)
{
    PyObject *STypes = M_constant_New();
    if (STypes) {
        BPy_constant *d = (BPy_constant*) STypes;

        EXPP_ADDCONST(CLD_DEFAULT);
        EXPP_ADDCONST(CLD_COLOR);
        EXPP_ADDCONST(WOD_BANDS);
        EXPP_ADDCONST(WOD_RINGS);
        EXPP_ADDCONST(WOD_BANDNOISE);
        EXPP_ADDCONST(WOD_RINGNOISE);
        EXPP_ADDCONST(MAG_DEFAULT);
        EXPP_ADDCONST(MBL_SOFT);
        EXPP_ADDCONST(MBL_SHARP);
        EXPP_ADDCONST(MBL_SHARPER);
        EXPP_ADDCONST(BLN_LIN);
        EXPP_ADDCONST(BLN_QUAD);
        EXPP_ADDCONST(BLN_EASE);
        EXPP_ADDCONST(BLN_DIAG);
        EXPP_ADDCONST(BLN_SPHERE);
        EXPP_ADDCONST(BLN_HALO);
        EXPP_ADDCONST(STC_PLASTIC);
        EXPP_ADDCONST(STC_WALLIN);
        EXPP_ADDCONST(STC_WALLOUT);
        EXPP_ADDCONST(NSE_DEFAULT);
        EXPP_ADDCONST(IMG_DEFAULT);
        EXPP_ADDCONST(PLG_DEFAULT);
        EXPP_ADDCONST(ENV_STATIC);
        EXPP_ADDCONST(ENV_ANIM);
        EXPP_ADDCONST(ENV_LOAD);
    }
    return STypes;
}

#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_TEXCO_##name))
    
static PyObject *M_Texture_TexCoDict (void)
{
    PyObject *TexCo = M_constant_New();
    if (TexCo) {
        BPy_constant *d = (BPy_constant*) TexCo;

        EXPP_ADDCONST(ORCO);
        EXPP_ADDCONST(REFL);
        EXPP_ADDCONST(NOR);
        EXPP_ADDCONST(GLOB);
        EXPP_ADDCONST(UV);
        EXPP_ADDCONST(OBJECT);
        EXPP_ADDCONST(WIN);
        EXPP_ADDCONST(VIEW);
        EXPP_ADDCONST(STICK);
    }
    return TexCo;
}


#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_MAPTO_##name))
    
static PyObject *M_Texture_MapToDict (void)
{
    PyObject *MapTo = M_constant_New();
    if (MapTo) {
        BPy_constant *d = (BPy_constant*) MapTo;

        EXPP_ADDCONST(COL);
        EXPP_ADDCONST(NOR);
        EXPP_ADDCONST(CSP);
        EXPP_ADDCONST(CMIR);
        EXPP_ADDCONST(REF);
        EXPP_ADDCONST(SPEC);
        EXPP_ADDCONST(HARD);
        EXPP_ADDCONST(ALPHA);
        EXPP_ADDCONST(EMIT);
    }
    return MapTo;
}


#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_FLAG_##name))
    
static PyObject *M_Texture_FlagsDict (void)
{
    PyObject *Flags = M_constant_New();
    if (Flags) {
        BPy_constant *d = (BPy_constant*) Flags;

        EXPP_ADDCONST(COLORBAND);
        EXPP_ADDCONST(FLIPBLEND);
        EXPP_ADDCONST(NEGALPHA);
    }
    return Flags;
}


#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_EXTEND_##name))
    
static PyObject *M_Texture_ExtendModesDict (void)
{
    PyObject *ExtendModes = M_constant_New();
    if (ExtendModes) {
        BPy_constant *d = (BPy_constant*) ExtendModes;

        EXPP_ADDCONST(EXTEND);
        EXPP_ADDCONST(CLIP);
        EXPP_ADDCONST(CLIPCUBE);
        EXPP_ADDCONST(REPEAT);
    }
    return ExtendModes;
}


#undef EXPP_ADDCONST
#define EXPP_ADDCONST(name) \
	constant_insert(d, #name, PyInt_FromLong(EXPP_TEX_IMAGEFLAG_##name))
    
static PyObject *M_Texture_ImageFlagsDict (void)
{
    PyObject *ImageFlags = M_constant_New();
    if (ImageFlags) {
        BPy_constant *d = (BPy_constant*) ImageFlags;

        EXPP_ADDCONST(INTERPOL);
        EXPP_ADDCONST(USEALPHA);
        EXPP_ADDCONST(MIPMAP);
        EXPP_ADDCONST(FIELDS);
        EXPP_ADDCONST(ROT90);
        EXPP_ADDCONST(CALCALPHA);
        EXPP_ADDCONST(STFIELD);
        EXPP_ADDCONST(MOVIE);
        EXPP_ADDCONST(CYCLIC);
    }
    return ImageFlags;
}


PyObject *Texture_Init (void)
{
    PyObject *submodule;
    PyObject  *dict;
    
    /* constants */
    PyObject *Types = M_Texture_TypesDict();
    PyObject *STypes = M_Texture_STypesDict();
    PyObject *TexCo = M_Texture_TexCoDict();
    PyObject *MapTo = M_Texture_MapToDict();
    PyObject *Flags = M_Texture_FlagsDict();
    PyObject *ExtendModes = M_Texture_ExtendModesDict();
    PyObject *ImageFlags = M_Texture_ImageFlagsDict();
        
    Texture_Type.ob_type = &PyType_Type;

    submodule = Py_InitModule3("Blender.Texture", 
                                        M_Texture_methods, M_Texture_doc);

    if (Types)
        PyModule_AddObject(submodule, "Types", Types);
    if (STypes)
        PyModule_AddObject(submodule, "STypes", STypes);
    if (TexCo)
        PyModule_AddObject(submodule, "TexCo", TexCo);
    if (MapTo)
        PyModule_AddObject(submodule, "MapTo", MapTo);
    if (Flags)
        PyModule_AddObject(submodule, "Flags", Flags);
    if (ExtendModes)
        PyModule_AddObject(submodule, "ExtendModes", ExtendModes);
    if (ImageFlags)
        PyModule_AddObject(submodule, "ImageFlags", ImageFlags);
    
    /* Add the MTex submodule to this module */
    dict = PyModule_GetDict (submodule);
    PyDict_SetItemString (dict, "MTex", MTex_Init());
  
    return submodule;
}

PyObject *Texture_CreatePyObject (Tex *tex)
{
    BPy_Texture *pytex;

    pytex = (BPy_Texture *) PyObject_NEW (BPy_Texture, &Texture_Type);
    if (!pytex)
        return EXPP_ReturnPyObjError (PyExc_MemoryError,
                                  "couldn't create BPy_Texture PyObject");

    pytex->texture = tex;
    return (PyObject *) pytex;
}

Tex *Texture_FromPyObject (PyObject *pyobj)
{
    return ((BPy_Texture *)pyobj)->texture;
}


int Texture_CheckPyObject (PyObject *pyobj)
{
    return (pyobj->ob_type == &Texture_Type);
}


/*****************************************************************************/
/* Python BPy_Texture methods:                                               */
/*****************************************************************************/

static PyObject *Texture_getExtend(BPy_Texture *self)
{
    PyObject *attr = NULL;
    const char *extend = NULL;

    if (EXPP_map_getStrVal (tex_extend_map, self->texture->extend, &extend))
        attr = PyString_FromString (extend);
    
    if (!attr) 
        return EXPP_ReturnPyObjError (PyExc_RuntimeError,
                                    "invalid internal extend mode");
    
    return attr;
}

static PyObject *Texture_getImage(BPy_Texture *self)
{
    /* we need this to be an IMAGE texture, and we must have an image */
    if ((self->texture->type != TEX_IMAGE) || !self->texture->ima)
    {
        Py_INCREF (Py_None);
        return Py_None;
    }
        
    return Image_CreatePyObject (self->texture->ima);
}


static PyObject *Texture_getName(BPy_Texture *self)
{
    PyObject *attr = PyString_FromString(self->texture->id.name+2);
    if (!attr) 
        return EXPP_ReturnPyObjError (PyExc_RuntimeError,
                                    "couldn't get Texture.name attribute");
    
    return attr;
}

static PyObject *Texture_getSType(BPy_Texture *self)
{
    PyObject *attr = NULL;
    const char *stype = NULL;

    if (EXPP_map_getStrVal (tex_stype_map[self->texture->type], 
                                            self->texture->stype, &stype))
        attr = PyString_FromString (stype);
    
    if (!attr) 
        return EXPP_ReturnPyObjError (PyExc_RuntimeError,
                                    "invalid texture stype internally");
    
    return attr;
}

static PyObject *Texture_getType(BPy_Texture *self)
{
    PyObject *attr = NULL;
    const char *type = NULL;

    if (EXPP_map_getStrVal (tex_type_map, self->texture->type, &type))
        attr = PyString_FromString (type);
    
    if (!attr) 
        return EXPP_ReturnPyObjError (PyExc_RuntimeError,
                                    "invalid texture type internally");
    
    return attr;
}


static PyObject *Texture_setAnimFrames(BPy_Texture *self, PyObject *args)
{
    int frames;
    if (!PyArg_ParseTuple(args, "i", &frames))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    if (frames < 0)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "frames cannot be negative");

    self->texture->frames = frames;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setAnimLength(BPy_Texture *self, PyObject *args)
{
    int length;
    if (!PyArg_ParseTuple(args, "i", &length))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    if (length < 0)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "length cannot be negative");

    self->texture->len = length;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setAnimMontage(BPy_Texture *self, PyObject *args)
{
    int fradur[4][2];
    int i, j;
    if (!PyArg_ParseTuple(args, "((ii)(ii)(ii)(ii))", 
                                        &fradur[0][0], &fradur[0][1],
                                        &fradur[1][0], &fradur[1][1],
                                        &fradur[2][0], &fradur[2][1],
                                        &fradur[3][0], &fradur[3][1]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a tuple of tuples");

    for (i=0; i<4; ++i)
        for (j=0; j<2; ++j)
            if (fradur[i][j] < 0)
                return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "values must be greater than zero");

    for (i=0; i<4; ++i)
        for (j=0; j<2; ++j)
            self->texture->fradur[i][j] = fradur[i][j];
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setAnimOffset(BPy_Texture *self, PyObject *args)
{
    int offset;
    if (!PyArg_ParseTuple(args, "i", &offset))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    self->texture->offset = offset;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setAnimStart(BPy_Texture *self, PyObject *args)
{
    int sfra;
    if (!PyArg_ParseTuple(args, "i", &sfra))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    if (sfra < 1)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "start must be greater than zero");

    self->texture->sfra = sfra;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setBrightness(BPy_Texture *self, PyObject *args)
{
    float bright;
    if (!PyArg_ParseTuple(args, "f", &bright))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a float");

    if (bright<0 || bright>2)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "brightness must be in range [0,2]");

    self->texture->bright = bright;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setContrast(BPy_Texture *self, PyObject *args)
{
    float contrast;
    if (!PyArg_ParseTuple(args, "f", &contrast))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a float");

    if (contrast<0 || contrast>2)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "contrast must be in range [0,2]");

    self->texture->contrast = contrast;
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setCrop(BPy_Texture *self, PyObject *args)
{
    float crop[4];
    int i;
    if (!PyArg_ParseTuple(args, "(ffff)", 
                            &crop[0], &crop[1], &crop[2], &crop[3]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected tuple of 4 floats");
    
    for (i=0; i<4; ++i)
        if (crop[i]<-10 || crop[i]>10)
            return EXPP_ReturnPyObjError (PyExc_ValueError,
                                        "values must be in range [-10,10]");

    self->texture->cropxmin = crop[0];
    self->texture->cropymin = crop[1];
    self->texture->cropxmax = crop[2];
    self->texture->cropymax = crop[3];
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setExtend(BPy_Texture *self, PyObject *args)
{
    char *extend = NULL;
    if (!PyArg_ParseTuple(args, "s", &extend))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected string argument");

    if (!EXPP_map_getShortVal (tex_extend_map, extend, &self->texture->extend))
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                           "invalid extend mode");    

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setIntExtend(BPy_Texture *self, PyObject *args)
{
    int extend = 0;
    if (!PyArg_ParseTuple(args, "i", &extend))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected int argument");
    
    if (extend<EXPP_TEX_EXTEND_MIN || extend>EXPP_TEX_EXTEND_MAX)
        return EXPP_ReturnPyObjError (PyExc_ValueError, 
                                            "invalid extend mode");
    
    self->texture->extend = extend;
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setFieldsPerImage(BPy_Texture *self, PyObject *args)
{
    int fie_ima;
    if (!PyArg_ParseTuple(args, "i", &fie_ima))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    if (fie_ima<1 || fie_ima>200)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                     "value must be in range [1,200]");

    self->texture->fie_ima = fie_ima;
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setFilterSize(BPy_Texture *self, PyObject *args)
{
    float size;
    if (!PyArg_ParseTuple(args, "f", &size))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a float");

    if (size<0.1 || size>25)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                           "filter size must be in range [0.1,25]");

    self->texture->filtersize = size;
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setFlags(BPy_Texture *self, PyObject *args)
{
    char *sf[3] = { NULL, NULL, NULL };
    int i;
    short flags = 0;
    short thisflag;
    if (!PyArg_ParseTuple(args, "|sss", &sf[0], &sf[1], &sf[2]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected 0-3 string arguments");

    for (i=0; i<3; ++i)
    {
        if (!sf[i]) break;

        if (!EXPP_map_getShortVal(tex_flag_map, sf[i], &thisflag))
            return EXPP_ReturnPyObjError (PyExc_ValueError,
                                            "invalid texture flag name");

        flags |= thisflag;
    }

    self->texture->flag = flags;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setIntFlags(BPy_Texture *self, PyObject *args)
{
    int flags = 0;
    if (!PyArg_ParseTuple(args, "i", &flags))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected int argument");
    
    self->texture->flag = flags;
    
    Py_INCREF(Py_None);
    return Py_None;
}


static PyObject *Texture_setImage(BPy_Texture *self, PyObject *args)
{
    PyObject *pyimg;
    Image *blimg = NULL;

    if (!PyArg_ParseTuple(args, "O!", &Image_Type, &pyimg))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an Image");
    blimg = Image_FromPyObject (pyimg);

    if (self->texture->ima) {
        self->texture->ima->id.us--;
    }

    self->texture->ima = blimg;
    id_us_plus(&blimg->id);

    Py_INCREF(Py_None);
    return Py_None;
}


static PyObject *Texture_setImageFlags(BPy_Texture *self, PyObject *args)
{
    char *sf[9] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
    int i;
    short flags = 0;
    short thisflag;
    if (!PyArg_ParseTuple(args, "|sssssssss", &sf[0], &sf[1], &sf[2], &sf[3],
                                    &sf[4], &sf[5], &sf[6], &sf[7], &sf[8]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected 0-9 string arguments");

    for (i=0; i<9; ++i)
    {
        if (!sf[i]) break;

        if (!EXPP_map_getShortVal(tex_imageflag_map, sf[i], &thisflag))
            return EXPP_ReturnPyObjError (PyExc_ValueError,
                                            "invalid texture image flag name");

        flags |= thisflag;
    }

    /* MIPMAP and FIELDS can't be used together */
    if ((flags & EXPP_TEX_IMAGEFLAG_MIPMAP) &&
            (flags & EXPP_TEX_IMAGEFLAG_FIELDS))
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                  "image flags MIPMAP and FIELDS cannot be used together");
    
    self->texture->imaflag = flags;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setIntImageFlags(BPy_Texture *self, PyObject *args)
{
    int flags = 0;
    if (!PyArg_ParseTuple(args, "i", &flags))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected int argument");
    
    /* MIPMAP and FIELDS can't be used together */
    if ((flags & EXPP_TEX_IMAGEFLAG_MIPMAP) &&
            (flags & EXPP_TEX_IMAGEFLAG_FIELDS))
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                  "image flags MIPMAP and FIELDS cannot be used together");
    
    self->texture->imaflag = flags;
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setName(BPy_Texture *self, PyObject *args)
{
    char *name;
    char buf[21];

    if (!PyArg_ParseTuple(args, "s", &name))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected string argument");

    PyOS_snprintf(buf, sizeof(buf), "%s", name);
    rename_id(&self->texture->id, buf);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setNoiseDepth(BPy_Texture *self, PyObject *args)
{
    int depth;
    if (!PyArg_ParseTuple(args, "i", &depth))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected an int");

    if (depth<0 || depth>6)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                     "value must be in range [0,6]");

    self->texture->noisedepth = depth;
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setNoiseSize(BPy_Texture *self, PyObject *args)
{
    float size;
    if (!PyArg_ParseTuple(args, "f", &size))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a float");

    if (size<0 || size>2)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                           "noise size must be in range [0,2]");

    self->texture->noisesize = size;
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setNoiseType(BPy_Texture *self, PyObject *args)
{
    char *type;

    if (!PyArg_ParseTuple(args, "s", &type))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected string argument");

    if (STREQ(type, "soft"))
        self->texture->noisetype = TEX_NOISESOFT;
    else if (STREQ(type, "hard"))
        self->texture->noisetype = TEX_NOISEPERL;
    
    else
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                    "noise type must be 'soft' or 'hard'");

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setRepeat(BPy_Texture *self, PyObject *args)
{
    int repeat[2];
    int i;
    if (!PyArg_ParseTuple(args, "(ii)", &repeat[0], &repeat[1]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected tuple of 2 ints");
    
    for (i=0; i<2; ++i)
        if (repeat[i]<1 || repeat[i]>512)
            return EXPP_ReturnPyObjError (PyExc_ValueError,
                                    "values must be in range [1,512]");

    self->texture->xrepeat = repeat[0];
    self->texture->yrepeat = repeat[1];
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setRGBCol(BPy_Texture *self, PyObject *args)
{
    float rgb[3];
    int i;
    if (!PyArg_ParseTuple(args, "(fff)", &rgb[0], &rgb[1], &rgb[2]))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected tuple of 3 floats");
    
    for (i=0; i<3; ++i)
        if (rgb[i]<0 || rgb[i]>2)
            return EXPP_ReturnPyObjError (PyExc_ValueError,
                                    "values must be in range [0,2]");

    self->texture->rfac = rgb[0];
    self->texture->gfac = rgb[1];
    self->texture->bfac = rgb[2];
    
    Py_INCREF (Py_None);
    return Py_None;
}


static PyObject *Texture_setSType(BPy_Texture *self, PyObject *args)
{
    char *stype = NULL;
    if (!PyArg_ParseTuple(args, "s", &stype))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected string argument");

    /* can we really trust texture->type? */
    if (!EXPP_map_getShortVal (tex_stype_map[self->texture->type], 
                                            stype, &self->texture->stype))
        return EXPP_ReturnPyObjError (PyExc_ValueError, 
                                                "invalid texture stype");

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setIntSType(BPy_Texture *self, PyObject *args)
{
    int stype = 0;
    const char *dummy = NULL;
    if (!PyArg_ParseTuple(args, "i", &stype))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected int argument");
    
    /* use the stype map to find out if this is a valid stype for this type *
     * note that this will allow CLD_COLOR when type is ENVMAP. there's not *
     * much that we can do about this though.                               */
    if (!EXPP_map_getStrVal (tex_stype_map[self->texture->type], stype, &dummy))
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                            "invalid stype (for this type)");
    
    self->texture->stype = stype;
    
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setTurbulence(BPy_Texture *self, PyObject *args)
{
    float turb;
    if (!PyArg_ParseTuple(args, "f", &turb))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                        "expected a float");

    if (turb<0 || turb>200)
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                           "turbulence must be in range [0,200]");

    self->texture->turbul = turb;
    
    Py_INCREF (Py_None);
    return Py_None;
}

static PyObject *Texture_setType(BPy_Texture *self, PyObject *args)
{
    char *type = NULL;
    if (!PyArg_ParseTuple(args, "s", &type))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected string argument");

    if (!EXPP_map_getShortVal (tex_type_map, type, &self->texture->type))
        return EXPP_ReturnPyObjError (PyExc_ValueError,
                                           "invalid texture type");    

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *Texture_setIntType(BPy_Texture *self, PyObject *args)
{
    int type = 0;
    if (!PyArg_ParseTuple(args, "i", &type))
        return EXPP_ReturnPyObjError (PyExc_TypeError,
                                           "expected int argument");
    
    if (type<EXPP_TEX_TYPE_MIN || type>EXPP_TEX_TYPE_MAX)
        return EXPP_ReturnPyObjError (PyExc_ValueError, 
                                            "invalid type number");
    
    self->texture->type = type;
    
    Py_INCREF(Py_None);
    return Py_None;
}

static void Texture_dealloc (BPy_Texture *self)
{
    PyObject_DEL (self);
}

static PyObject *Texture_getAttr (BPy_Texture *self, char *name)
{
    PyObject *attr = Py_None;
    Tex *tex = self->texture;

    if (STREQ(name, "animFrames"))
        attr = PyInt_FromLong (tex->frames);
    else if (STREQ(name, "animLength"))
        attr = PyInt_FromLong (tex->len);
    else if (STREQ(name, "animMontage"))
        attr = Py_BuildValue("((i,i),(i,i),(i,i),(i,i))", 
                            tex->fradur[0][0], tex->fradur[0][1],
                            tex->fradur[1][0], tex->fradur[1][1],
                            tex->fradur[2][0], tex->fradur[2][1],
                            tex->fradur[3][0], tex->fradur[3][1] );
    else if (STREQ(name, "animOffset"))
        attr = PyInt_FromLong (tex->offset);
    else if (STREQ(name, "animStart"))
        attr = PyInt_FromLong (tex->sfra);
    else if (STREQ(name, "brightness"))
        attr = PyFloat_FromDouble (tex->bright);
    else if (STREQ(name, "contrast"))
        attr = PyFloat_FromDouble (tex->contrast);
    else if (STREQ(name, "crop"))
        attr = Py_BuildValue("(f,f,f,f)", tex->cropxmin, tex->cropymin, 
                                          tex->cropxmax, tex->cropymax);
    else if (STREQ(name, "extend"))
        attr = PyInt_FromLong (tex->extend);
    else if (STREQ(name, "fieldsPerImage"))
        attr = PyInt_FromLong (tex->fie_ima);
    else if (STREQ(name, "filterSize"))
        attr = PyFloat_FromDouble (tex->filtersize);
    else if (STREQ(name, "flags"))
        attr = PyInt_FromLong (tex->flag);
    else if (STREQ(name, "image"))
        attr = Texture_getImage (self);
    else if (STREQ(name, "imageFlags"))
        attr = PyInt_FromLong (tex->imaflag);
    else if (STREQ(name, "name"))
        attr = PyString_FromString(tex->id.name+2);
    else if (STREQ(name, "noiseDepth"))
        attr = PyInt_FromLong (tex->noisedepth);
    else if (STREQ(name, "noiseSize"))
        attr = PyFloat_FromDouble (tex->noisesize);
    else if (STREQ(name, "noiseType"))
    {
        if (tex->noisetype == TEX_NOISESOFT)
            attr = PyString_FromString ("soft");
        else
            attr = PyString_FromString ("hard");
    }
    else if (STREQ(name, "repeat"))
        attr = Py_BuildValue ("(i,i)", tex->xrepeat, tex->yrepeat);
    else if (STREQ(name, "rgbCol"))
        attr = Py_BuildValue ("(f,f,f)", tex->rfac, tex->gfac, tex->gfac);
    else if (STREQ(name, "stype"))
        attr = PyInt_FromLong (tex->stype);
    else if (STREQ(name, "turbulence"))
        attr = PyFloat_FromDouble (tex->turbul);
    else if (STREQ(name, "type"))
        attr = PyInt_FromLong (tex->type);

    
    else if (STREQ(name, "__members__"))
        attr = Py_BuildValue("[s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s]", 
                "animFrames", "animLength", "animMontage", "animOffset",
                "animStart", "brightness", "contrast", "crop", "extend",
                "fieldsPerImage", "filterSize", "flags", "image", 
                "imageFlags", "name", "noiseDepth", "noiseSize", "noiseType",
                "repeat", "rgbCol", "stype", "turbulence", "type");

    if (!attr)
        return EXPP_ReturnPyObjError (PyExc_MemoryError,
                                            "couldn't create PyObject");

    if (attr != Py_None) 
        return attr; /* member attribute found, return it */

    /* not an attribute, search the methods table */
    return Py_FindMethod(BPy_Texture_methods, (PyObject *)self, name);
}


static int Texture_setAttr (BPy_Texture *self, char *name, PyObject *value)
{
    PyObject *valtuple; 
    PyObject *error = NULL;

    /* Put "value" in a tuple, because we want to pass it to functions  *
     * that only accept PyTuples.                                       */
    valtuple = Py_BuildValue("(O)", value);
    if (!valtuple)
        return EXPP_ReturnIntError(PyExc_MemoryError,
                                "Texture_setAttr: couldn't create PyTuple");

    if (STREQ(name, "animFrames"))
        error = Texture_setAnimFrames (self, valtuple);
    else if (STREQ(name, "animLength"))
        error = Texture_setAnimLength(self, valtuple);
    else if (STREQ(name, "animMontage"))
        error = Texture_setAnimMontage(self, valtuple);
    else if (STREQ(name, "animOffset"))
        error = Texture_setAnimOffset(self, valtuple);
    else if (STREQ(name, "animStart"))
        error = Texture_setAnimStart(self, valtuple);
    else if (STREQ(name, "brightness"))
        error = Texture_setBrightness(self, valtuple);
    else if (STREQ(name, "contrast"))
        error = Texture_setContrast(self, valtuple);
    else if (STREQ(name, "crop"))
        error = Texture_setCrop(self, valtuple);
    else if (STREQ(name, "extend"))
        error = Texture_setIntExtend(self, valtuple);
    else if (STREQ(name, "fieldsPerImage"))
        error = Texture_setFieldsPerImage(self, valtuple);
    else if (STREQ(name, "filterSize"))
        error = Texture_setFilterSize(self, valtuple);
    else if (STREQ(name, "flags"))
        error = Texture_setIntFlags(self, valtuple);
    else if (STREQ(name, "image"))
        error = Texture_setImage (self, valtuple);
    else if (STREQ(name, "imageFlags"))
        error = Texture_setIntImageFlags(self, valtuple);
    else if (STREQ(name, "name"))
        error = Texture_setName(self, valtuple);
    else if (STREQ(name, "noiseDepth"))
        error = Texture_setNoiseDepth(self, valtuple);
    else if (STREQ(name, "noiseSize"))
        error = Texture_setNoiseSize(self, valtuple);
    else if (STREQ(name, "noiseType"))
        error = Texture_setNoiseType(self, valtuple);
    else if (STREQ(name, "repeat"))
        error = Texture_setRepeat(self, valtuple);
    else if (STREQ(name, "rgbCol"))
        error = Texture_setRGBCol(self, valtuple);
    else if (STREQ(name, "stype"))
        error = Texture_setIntSType(self, valtuple);
    else if (STREQ(name, "turbulence"))
        error = Texture_setTurbulence(self, valtuple);
    else if (STREQ(name, "type"))
        error = Texture_setIntType(self, valtuple);
    
    else { 
        /* Error */
        Py_DECREF(valtuple);
        return EXPP_ReturnIntError (PyExc_KeyError, "attribute not found");
    }

    Py_DECREF (valtuple);

    if (error != Py_None) 
        return -1;

    /* Py_None was INCREF'd by the set*() function, so we need to DECREF it */
    Py_DECREF (Py_None);
    
    return 0;
}

static int Texture_compare (BPy_Texture *a, BPy_Texture *b)
{
    return (a->texture == b->texture) ? 0 : -1;
}

static PyObject *Texture_repr (BPy_Texture *self)
{
    return PyString_FromFormat("[Texture \"%s\"]", self->texture->id.name+2);
}