forked from blurstudio/Py3dsMax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.cpp
1569 lines (1311 loc) · 46.7 KB
/
wrapper.cpp
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
/*
\file wrapper.cpp
\remarks Implementation of the ValueWrapper and ObjectWrapper classes
\author Blur Studio (c) 2010
\email [email protected]
\license This software is released under the GNU General Public License. For more info, visit: http://www.gnu.org/
*/
#include "imports.h"
#include <string>
#include <sstream> // for std::stringstream
#include "macros.h"
#include "wrapper.h"
#include "protector.h"
#include "Parser.h" // for print functions
// define these properties if the MAKE_PY26_COMPAT preprocessor definition exists
#ifdef MAKE_PY26_COMPAT
typedef inquiry lenfunc;
typedef int Py_ssize_t;
typedef intobjargproc ssizeobjargproc;
typedef intargfunc ssizeargfunc;
#endif
#ifndef UNICODE
// For some reason python doesn't actually use the active code page
// that it gets from windows for [En|De]codeMBC functionality.
// So we have to get it from python and use it for the encoding/decoding
// of data coming from max.
static const char * getEncoding()
{
static char codePage[50] = {0};
if( codePage[0] == 0 ) {
PyObject * locale_mod = PyImport_ImportModule("locale");
PyObject * enc_func = locale_mod ? PyObject_GetAttrString(locale_mod,"getpreferredencoding") : 0;
PyObject * enc = PyObject_CallFunction(enc_func,NULL);
if( enc && PyString_Check(enc) )
strncpy(codePage,PyString_AsString(enc),49);
Py_XDECREF(enc);
Py_XDECREF(enc_func);
Py_XDECREF(locale_mod);
}
return codePage;
}
#endif
PyStringToMCHAR::PyStringToMCHAR( PyObject * pyString, bool stealRef )
: mObject( 0 )
#ifndef UNICODE
, mData( 0 )
#endif
{
#ifdef UNICODE
if( PyString_Check(pyString) ) {
mObject = PyUnicode_DecodeUTF8( PyString_AsString(pyString), PyString_Size(pyString), NULL );
if( stealRef )
Py_DECREF(pyString);
} else if( PyUnicode_Check(pyString) ) {
mObject = pyString;
if( !stealRef )
Py_INCREF(mObject);
}
#else
if( pyString ) {
if( PyString_Check(pyString) ) {
mObject = pyString;
if( !stealRef )
Py_XINCREF(mObject);
mData = PyString_AsString(mObject);
} else if( PyUnicode_Check(pyString) ) {
mObject = PyUnicode_AsEncodedString(pyString,getEncoding(),NULL);
if( stealRef )
Py_DECREF(pyString);
mData = PyString_AsString(mObject);
}
}
#endif
}
PyStringToMCHAR::PyStringToMCHAR( const char * data )
: mObject( 0 )
#ifndef UNICODE
, mData( data )
#endif
{
#ifdef UNICODE
mObject = PyUnicode_DecodeUTF8( data, strlen(data), NULL );
if( !mObject ) {
PY_ERROR_PRINT_CLEAR
}
#endif
}
PyStringToMCHAR::~PyStringToMCHAR()
{
Py_XDECREF(mObject);
}
const MCHAR * PyStringToMCHAR::mchar()
{
#ifdef UNICODE
return mObject ? PyUnicode_AsUnicode(mObject) : 0;
#else
return mData;
#endif
}
MCharToPyString::MCharToPyString( const MCHAR * mchar )
: mObject( 0 )
, mUtf8Object( 0 )
, mMChars( mchar )
{
}
MCharToPyString::~MCharToPyString()
{
Py_XDECREF(mObject);
Py_XDECREF(mUtf8Object);
}
PyObject * MCharToPyString::pyString()
{
if( !mObject ) {
#ifdef UNICODE
mObject = PyUnicode_FromUnicode(mMChars,wcslen(mMChars));
#else
unsigned char * c = (unsigned char*)mMChars;
bool isAscii = false;
if( isAscii ) {
mObject = PyString_FromString(mMChars);
} else {
int len = _tcslen(mMChars);
mObject = PyUnicode_Decode(mMChars,len,getEncoding(),NULL);
if( !mObject ) {
PY_ERROR_PRINT_CLEAR
mObject = PyUnicode_DecodeUTF8(mMChars,len,NULL);
if( !mObject ) {
PY_ERROR_PRINT_CLEAR
mObject = PyUnicode_DecodeASCII(mMChars,len,"replace");
}
}
}
#endif
}
if( !mObject ) {
PY_ERROR_PRINT_CLEAR
mprintf( _T("%s\n"), _T("<critical error converting maxscript string to python, please contact IT>") );
mObject = PyString_FromString("<critical error converting maxscript string to python, please contact IT>");
}
return mObject;
}
PyObject * MCharToPyString::pyStringRef()
{
PyObject * ret = pyString();
Py_XINCREF(ret);
return ret;
}
const char * MCharToPyString::data()
{
if( mObject ) {
if( PyString_Check(mObject) )
return PyString_AsString(mObject);
}
#ifdef UNICODE
if( !mUtf8Object && mObject ) {
mUtf8Object = PyUnicode_AsUTF8String(mObject);
if( !mUtf8Object )
PyErr_Print();
}
return PyString_Check(mUtf8Object) ? PyString_AsString(mUtf8Object) : 0;
#else
return mMChars;
#endif
}
//---------------------------------------------------------------
// ValueWrapper Implementation
//---------------------------------------------------------------
// ctor
PyObject*
ValueWrapper_new( PyTypeObject* type, PyObject* args, PyObject* kwds ) {
ValueWrapper* self;
self = (ValueWrapper*)type->tp_alloc(type, 0);
self->mValue = NULL;
self->mNext = self->mPrev = 0;
return (PyObject*) self;
}
static int
ValueWrapper_init( PyObject *self, PyObject *args, PyObject *kwds ) {
// Since the ValueWrapper Python class is exposed via the Py3dsMax
// module, we need to make sure it is not directly instantiated. The
// reason for exposing it is to allow for isinstance checks and in
// general make it behave like a normal Python type/class.
PyErr_SetString(PyExc_NotImplementedError, "Py3dsMax.ValueWrapper may not be directly instantiated.");
return -1;
}
#ifndef PyMODINIT_FUNC
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
ValueWrapper_init(void) {
ValueWrapperType.ob_type = &PyType_Type;
PyType_Ready( &ValueWrapperType );
}
static long
ValueWrapper_hash( ValueWrapper* self ) {
PyObject * pyAddress = PyLong_FromUnsignedLongLong( (unsigned PY_LONG_LONG)self );
long hash = PyObject_Hash(pyAddress);
Py_DECREF(pyAddress);
return hash;
}
// dtor
void
ValueWrapper_dealloc( ValueWrapper* self ) {
Protector::unprotect( self );
self->ob_type->tp_free( (PyObject*) self );
}
// __call__ function: called when calling a function on a ValueWrapper instance
PyObject*
ValueWrapper_call( ValueWrapper* self, PyObject* args, PyObject* kwds ) {
// Step 1: calculate how many Value**'s we are going to need to pass the Value* method
Py_ssize_t arg_count = (args) ? PyTuple_Size( args ) : 0;
Py_ssize_t key_count = (kwds) ? PyDict_Size( kwds ) : -1;
Py_ssize_t mxs_count = (key_count == -1) ? arg_count : arg_count + 1 + (key_count*2);
// Step 2: protect the maxcript memory we are going to use
MXS_PROTECT( three_typed_value_locals( Value* method, Value* result, StringStream* log ) );
// Step 3: pull out the proper method from maxscript
MXS_EVAL( self->mValue, vl.method );
// override the current log for error catching purposes
vl.log = new StringStream();
CharStream* old_log = thread_local(current_stdout);
((MAXScript_TLS*)TlsGetValue(thread_locals_index))->current_stdout = vl.log;
vl.result = NULL;
// Step 4: call the maxscript method
if ( vl.method ) {
// Step 5: check to see if we need maxscript arguments
if ( mxs_count ) {
// Step 6: if we do, we need to create a volatile array and protect it
Value** mxs_args;
value_local_array(mxs_args,mxs_count);
// Step 7: add converted arguments
for ( int i = 0; i < arg_count; i++ ) {
mxs_args[i] = ObjectWrapper::intern( PyTuple_GetItem( args, i ) );
}
// Step 8: add converted keywords
if ( key_count > 0 ) {
PyObject *pkey, *pvalue;
Py_ssize_t pos = 0;
int key_pos = 0;
// Maxscript deliminates keywords from arguments by supplying the &keyarg_marker pointer
// for when the keywords start, and from there does a key, value pairing in the array
mxs_args[arg_count] = &keyarg_marker;
while ( PyDict_Next(kwds, &pos, &pkey, &pvalue ) ) {
{
PyStringToMCHAR mchar(pkey);
mxs_args[ arg_count + 1 + (key_pos*2) ] = Name::intern( mchar.mchar() );
}
mxs_args[ arg_count + 2 + (key_pos*2) ] = ObjectWrapper::intern( pvalue );
key_pos++;
}
// Release the key and value pointers
pkey = NULL;
pvalue = NULL;
}
// Step 9: call the method and use try/catch to protect the memory
try {
vl.result = vl.method->apply( mxs_args, mxs_count )->get_heap_ptr();
}
catch ( MAXScriptException& e ) {
//show_source_pos();
StringStream* buffer = new StringStream(_T("Maxscript error has occurred while calling function: ") );
buffer->puts(vl.log->to_string());
const MCHAR * filename = thread_local(source_file) ? thread_local(source_file)->to_string() : 0;
buffer->printf(_T("-- %s %i %i\n"), filename ? filename : _T("unknown file"), thread_local(source_pos), thread_local(source_line) );
e.sprin1(buffer);
MCharToPyString pybuf(buffer->to_string());
PyErr_SetObject( PyExc_RuntimeError, pybuf.pyString() );
restore_current_frames();
clear_error_source_data();
MAXScript_signals = 0;
return 0;
}
catch ( ... ) {
MXS_CLEARERRORS();
PyErr_SetString( PyExc_RuntimeError, "Unknown MAXScript Error Occurred" );
}
/* catch ( ... ) {
MXS_CLEARERRORS();
MCharToPyString pylog(vl.log->to_string());
PyErr_SetString( PyExc_RuntimeError, pylog.data() );
vl.result = NULL;
} */
// clear the value local memory
pop_value_local_array(mxs_args);
}
else {
// Step 6: if we don't, simply call the method with a NULL array
try { vl.result = vl.method->apply( NULL, 0 ); }
catch ( ... ) {
MXS_CLEARERRORS();
MCharToPyString pylog(vl.log->to_string());
PyErr_SetObject( PyExc_RuntimeError, pylog.pyString() );
vl.result = NULL;
}
}
}
// restore the old log
((MAXScript_TLS*)TlsGetValue(thread_locals_index))->current_stdout = old_log;
// Step 10: convert the result
PyObject* output = NULL;
if ( vl.result )
output = ObjectWrapper::py_intern( vl.result );
else if( !PyErr_Occurred() ) {
output = Py_None;
Py_INCREF(Py_None);
}
// Step 11: cleanup the maxscript errors
MXS_CLEANUP();
return output;
}
// __cmp__ function: called when comparing 2 ValueWrapper instances (a == b, a < b, a <= b, etc.)
int
ValueWrapper_compare( PyObject* self, PyObject* other ) {
int result = -1;
// Step 1: protect the maxscript memory
MXS_PROTECT( two_value_locals( mxs_self, mxs_other ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: make sure both items exist
if ( vl.mxs_self && vl.mxs_other ) {
// Step 4: compare direct pointers
if ( vl.mxs_self == vl.mxs_other || vl.mxs_self->eval() == vl.mxs_other->eval() )
result = 0;
else {
// Step 5: use maxscript's built in <, ==, > checks
try {
if ( vl.mxs_self->eq_vf( &vl.mxs_other, 1 ) == &true_value ) { result = 0; }
else if ( vl.mxs_self->lt_vf( &vl.mxs_other, 1 ) == &true_value ) { result = -1; }
else if ( vl.mxs_self->gt_vf( &vl.mxs_other, 1 ) == &true_value ) { result = 1; }
}
catch ( ... ) {
MXS_CLEARERRORS();
}
}
}
// Step 6: cleanup maxscript memory
MXS_CLEANUP();
return result;
}
// __str__ function: convert a Value* wrapper to a string
PyObject*
ValueWrapper_str( ValueWrapper* self ) {
// Step 1: protect values
MXS_PROTECT( two_typed_value_locals( Value* mxs_check, StringStream* mxs_stream ) );
// Step 2: evaluate the value
MXS_EVAL( self->mValue, vl.mxs_check );
// Step 3: check for simple conversions
PyObject* output = NULL;
if ( is_name( vl.mxs_check ) || is_string( vl.mxs_check ) ) {
output = MCharToPyString(vl.mxs_check->to_string()).pyStringRef();
}
// Step 4: block recursive struct printouts
else if ( is_struct( vl.mxs_check ) )
output = PyString_FromString( "<mxs.StructDef instance>" );
// Step 5: try to use the builtin maxscript print out
else {
vl.mxs_stream = new StringStream();
try {
vl.mxs_check->sprin1( vl.mxs_stream );
output = MCharToPyString(vl.mxs_stream->to_string()).pyStringRef();
}
MXS_CATCHERRORS();
}
// Step 6: cleanup maxscript memory
MXS_CLEANUP();
if( !output ) {
output = Py_None;
Py_INCREF(output);
}
return output;
}
// __getattr__ function: get a property by name from a Value* instance
PyObject*
ValueWrapper_getattr( ValueWrapper* self, char* key ) {
// check for pre-defined python properties
PyObject* tmp;
PyObject* check = PyString_FromString(key);
if ( !(tmp = PyObject_GenericGetAttr( (PyObject*) self, check )) ) {
Py_XDECREF(check);
// check for python internals
if ( key[0] == '_' || !PyErr_ExceptionMatches(PyExc_AttributeError) ) {
return NULL;
}
PyErr_Clear();
}
else {
Py_XDECREF(check);
return tmp;
}
// return the default maxscript value
one_value_local(result);
PyStringToMCHAR mkey(PyString_FromString(key),true);
try { vl.result = ((ValueWrapper*) self)->mValue->eval()->_get_property( Name::intern(mkey.mchar()) ); }
catch ( ... ) {
PyObject * self_str = ValueWrapper_str(self);
PyErr_Format( PyExc_AttributeError, "%s is not a property of %s", key, PyString_AsString(self_str) );
Py_DECREF(self_str);
pop_value_locals();
return 0;
}
tmp = ObjectWrapper::py_intern( vl.result );
pop_value_locals();
return tmp;
}
// __setattr__ function: sets a property by name for a Value*
int
ValueWrapper_setattr( ValueWrapper* self, char* key, PyObject* value ) {
bool success = true;
PyStringToMCHAR mkey(PyString_FromString(key),true);
try { self->mValue->eval()->_set_property( Name::intern(mkey.mchar()), ObjectWrapper::intern(value) ); }
catch ( ... ) { success = false; }
if ( success ) {
return 0;
}
else {
PyObject* pkey = PyString_FromString(key);
int ret = PyObject_GenericSetAttr( (PyObject*) self, pkey, value );
Py_DECREF(pkey);
return ret;
}
}
//------- Sequence Methods
// __len__ function: calculate the length of a maxscript value
int
ValueWrapper_length( PyObject* self ) {
// Step 1: protect the maxscript memory
MXS_PROTECT( one_value_local( mxs_check ) );
// Step 2: evaluate the value
MXS_EVAL( ((ValueWrapper*) (self))->mValue, vl.mxs_check );
int count = NULL;
// Step 3: use the count property to check if an object has length in maxscript
try { count = vl.mxs_check->_get_property( n_count )->to_int(); }
MXS_CATCHERRORS();
// Step 4: cleanup maxscript memory
MXS_CLEANUP();
return count;
}
// __getitem__ function: get an item from an index in a maxscript value
PyObject*
ValueWrapper_item( PyObject* self, int index ) {
MXS_PROTECT( three_value_locals( mxs_check, mindex, result ) );
MXS_EVAL( ((ValueWrapper*) self)->mValue, vl.mxs_check );
int count = 0;
// pull the lenght of the collection
try { count = vl.mxs_check->_get_property( n_count )->to_int(); }
catch ( ... ) {
MXS_CLEARERRORS();
PyErr_SetString( PyExc_IndexError, "__getitem__ cannot access item for index" );
MXS_CLEANUP();
return NULL;
}
// support pythonic negative lookup
if ( index < 0 ) {
index = count + index;
}
// make sure the index is valid
if ( !( 0 <= index && index < count) ) {
PyErr_SetString( PyExc_IndexError, "__getitem__ index is out of range" );
MXS_CLEANUP();
return NULL;
}
// pull the value
vl.mindex = Integer::intern( index + 1 );
try { vl.result = vl.mxs_check->get_vf( &vl.mindex, 1 ); }
MXS_CATCHERRORS();
PyObject* output = NULL;
if ( !vl.result ) { PyErr_SetString( PyExc_IndexError, "__getitem__ index is out of range" ); }
else { output = ObjectWrapper::py_intern(vl.result); }
MXS_CLEANUP();
return output;
}
// __getitem__ function: get an item based on an abstract python object
PyObject*
ValueWrapper_objitem( PyObject* self, PyObject* key ) {
// Step 1: check to see if the key is a number, then use the above method
if ( key->ob_type == &PyInt_Type ) {
return ValueWrapper_item( self, PyInt_AsLong( key ) );
}
// Step 2: protect the maxscript memory
MXS_PROTECT( three_value_locals( mxs_index, mxs_check, mxs_result ) );
// Step 3: evaluate the value
MXS_EVAL( ((ValueWrapper*) (self))->mValue, vl.mxs_check );
// Step 4: convert the python object to a maxscript key
vl.mxs_index = ObjectWrapper::intern( key );
// Step 5: try to pull the value from the sequence using virtual functions
try { vl.mxs_result = vl.mxs_check->get_vf( &vl.mxs_index, 1 ); }
MXS_CATCHERRORS();
// Step 6: convert the maxscript result to a python value
PyObject* output = NULL;
if ( vl.mxs_result ) { output = ObjectWrapper::py_intern( vl.mxs_result ); }
else { PyErr_SetString( PyExc_IndexError, "__getitem__ could not get a maxscript value" ); }
// Step 7: cleanup maxscript memory
MXS_CLEANUP();
return output;
}
// __setitem__ function: set a maxscript value by an index
int
ValueWrapper_setitem( PyObject* self, int index, PyObject* value ) {
// Step 1: protect maxscript memory
MXS_PROTECT( two_value_locals( mxs_check, mxs_result ) );
// Step 2: evaluate the value
MXS_EVAL( ((ValueWrapper*) (self))->mValue, vl.mxs_check );
// Step 3: calculate the length of the sequence
int count = 0;
try { count = vl.mxs_check->_get_property( n_count )->to_int(); }
MXS_CATCHERRORS();
if ( count ) {
// Step 3: resolve index differences in maxscript & python
if ( index < 0 )
index += count;
if ( 0 <= index && index < count ) {
// Step 4: create a volatile array of values to pass to the put virtual function
Value** arg_list;
value_local_array( arg_list, 2 );
arg_list[0] = Integer::intern( index + 1 );
arg_list[1] = ObjectWrapper::intern( value );
try { vl.mxs_result = vl.mxs_check->put_vf( arg_list, 2 ); }
MXS_CATCHERRORS();
// Step 5: pop the local array
pop_value_local_array(arg_list);
}
}
// Step 6: convert the result
int result = -1;
if ( vl.mxs_result ) { result = 0; }
else { PyErr_SetString( PyExc_IndexError, "__setitem__ index is out of range" ); }
// Step 6: cleanup maxscript memory
MXS_CLEANUP();
return result;
}
// __setitem__ function: set a maxscript value by python object
int
ValueWrapper_setobjitem( PyObject* self, PyObject* key, PyObject* value ) {
// Step 1: check to see if the python object is a number type, if so, use the above method
if ( key->ob_type == &PyInt_Type ) {
return ValueWrapper_setitem( self, PyInt_AsLong( key ), value );
}
// Step 2: protect the maxscript memory
MXS_PROTECT( one_value_local( mxs_check ) );
// Step 3: evaluate the value
MXS_EVAL( ((ValueWrapper*) (self))->mValue, vl.mxs_check );
// Step 4: create a volatile maxscript value array to pass to the put virtual function
Value** arg_list;
value_local_array( arg_list, 2 );
arg_list[0] = ObjectWrapper::intern( key );
arg_list[1] = ObjectWrapper::intern( value );
// Step 5: call the put virtual method
int result = -1;
try {
vl.mxs_check->put_vf( arg_list, 2 );
result = 0;
}
MXS_CATCHERRORS();
// Step 6: convert the results
if ( result == -1 ) { PyErr_SetString( PyExc_IndexError, "__setitem__ could not set the maxscript value" ); }
// Step 7: cleanup maxscript memory
pop_value_local_array( arg_list );
MXS_CLEANUP();
return result;
}
// Number Methods
// __add__ function: called when trying to add two wrapper values together
PyObject*
ValueWrapper_add( PyObject* self, PyObject* other ) {
// Step 1: protect maxscript memory
MXS_PROTECT( three_value_locals( mxs_self, mxs_other, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->plus_vf( &vl.mxs_other, 1 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__add__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __sub__ function: called when trying to subtract two wrapper values together
PyObject*
ValueWrapper_subtract( PyObject* self, PyObject* other ) {
// Step 1: protect maxscript memory
MXS_PROTECT( three_value_locals( mxs_self, mxs_other, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->minus_vf( &vl.mxs_other, 1 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__sub__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __div__ function: called when trying to divide two wrapper values together
PyObject*
ValueWrapper_divide( PyObject* self, PyObject* other ) {
// Step 1: protect maxscript memory
MXS_PROTECT( three_value_locals( mxs_self, mxs_other, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->div_vf( &vl.mxs_other, 1 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__div__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __mul__ function: called when trying to multiply two wrapper values together
PyObject*
ValueWrapper_multiply( PyObject* self, PyObject* other ) {
// Step 1: protect maxscript memory
MXS_PROTECT( three_value_locals( mxs_self, mxs_other, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->times_vf( &vl.mxs_other, 1 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__mul__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __pow__ function: called when trying to raise one number by the other
PyObject*
ValueWrapper_power( PyObject* self, PyObject* other, PyObject *args ) {
// Step 1: protect maxscript memory
MXS_PROTECT( three_value_locals( mxs_self, mxs_other, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
MXS_EVAL( ObjectWrapper::intern( other ), vl.mxs_other );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->pwr_vf( &vl.mxs_other, 1 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__pow__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __abs__ function: called when trying to divide two wrapper values together
PyObject*
ValueWrapper_absolute( PyObject* self ) {
// Step 1: protect maxscript memory
MXS_PROTECT( two_value_locals( mxs_self, mxs_value ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
// Step 3: calculate maxscript value
try { vl.mxs_value = vl.mxs_self->abs_vf( NULL, 0 ); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = NULL;
if ( vl.mxs_value ) { output = ObjectWrapper::py_intern( vl.mxs_value ); }
else { PyErr_SetString( PyExc_ArithmeticError, "__abs__ error: could not add maxscript values together" ); }
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __int__ function: convert an object to an integer
PyObject*
ValueWrapper_int( PyObject* self ) {
// Step 1: protect maxscript memory
MXS_PROTECT( one_value_local( mxs_self ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
// Step 3: calculate maxscript value
int result = 0;
try { result = vl.mxs_self->to_int(); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = PyInt_FromLong( result );
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __float__ function: convert an object to an integer
PyObject*
ValueWrapper_float( PyObject* self ) {
// Step 1: protect maxscript memory
MXS_PROTECT( one_value_local( mxs_self ) );
// Step 2: convert the two items to values
MXS_EVAL( ObjectWrapper::intern( self ), vl.mxs_self );
// Step 3: calculate maxscript value
float result = 0;
try { result = vl.mxs_self->to_float(); }
MXS_CATCHERRORS();
// Step 4: convert to python
PyObject* output = PyFloat_FromDouble( result );
// Step 5: cleanup maxscipt memory
MXS_CLEANUP();
return output;
}
// __neg__ function: return the negative of a wrapper object
PyObject*
ValueWrapper_negative( PyObject* self ) {
// Step 1: create the multiplier
PyObject* mult = PyInt_FromLong(-1);
PyObject* result = ValueWrapper_multiply( self, mult );
// Step 2: free the multiplier
Py_DECREF( mult );
return result;
}
// __nonzero__ function: return true always
int
ValueWrapper_nonzero( PyObject* self ) {
return 1;
}
// create custom methods
static PyObject*
ValueWrapper_property( PyObject* self, PyObject* args ) {
PyObject * ret = 0;
char* propName;
if ( !PyArg_ParseTuple( args, "s", &propName ) )
return NULL;
one_value_local(result);
PyStringToMCHAR mpropName(PyString_FromString(propName),true);
try { vl.result = ((ValueWrapper*) self)->mValue->eval()->_get_property( Name::intern(mpropName.mchar()) ); }
catch ( ... ) {
PyObject * self_str = ValueWrapper_str((ValueWrapper*)self);
PyErr_Format( PyExc_AttributeError, "%s is not a property of %s", propName, PyString_AsString(self_str) );
Py_DECREF(self_str);
}
if ( vl.result ) {
ret = ObjectWrapper::py_intern( vl.result );
}
pop_value_locals();
return ret;
}
static PyObject*
ValueWrapper_setProperty( PyObject* self, PyObject* args ) {
char* propName;
PyObject* propValue;
if ( !PyArg_ParseTuple( args, "sO", &propName, &propValue ) )
return NULL;
bool success = true;
PyStringToMCHAR mpropName(PyString_FromString(propName),true);
try { ((ValueWrapper*) self)->mValue->eval()->_set_property( Name::intern(mpropName.mchar()), ObjectWrapper::intern(propValue) ); }
catch ( ... ) { success = false; }
if ( success ) {
Py_INCREF(Py_True);
return Py_True;
}
else {
Py_INCREF(Py_False);
return Py_False;
}
}
//--------------------------------------------------------------
// create the python mappings
// define the custom methods for a wrapper
PyMethodDef ValueWrapper_methods[] = {
// windows methods
{ "property", (PyCFunction)ValueWrapper_property, METH_VARARGS, "Get a property from a MXS wrapper" },
{ "setProperty", (PyCFunction)ValueWrapper_setProperty, METH_VARARGS, "Set a property from a MXS wrapper" },
{ NULL, NULL, 0, NULL }
};
// sequence methods
static PySequenceMethods proxy_as_sequence = {
(lenfunc) ValueWrapper_length, // sq_length
0, // sq_concat
0, // sq_repeat
(ssizeargfunc) ValueWrapper_item, // sq_item
0, // sq_slice
(ssizeobjargproc) ValueWrapper_setitem, // sq_ass_item
0, // sq_ass_slice
0, // sq_contains
0, // sq_inplace_concat
0, // sq_inplace_repeat
};
// mapping methods
static PyMappingMethods proxy_as_mapping = {
(lenfunc) ValueWrapper_length, // mp_length
(binaryfunc) ValueWrapper_objitem, // mp_subscript
(objobjargproc) ValueWrapper_setobjitem, // mp_ass_subscript
};
// number methods
static PyNumberMethods proxy_as_number = {
(binaryfunc) ValueWrapper_add, // nb_add
(binaryfunc) ValueWrapper_subtract, // nb_subtract
(binaryfunc) ValueWrapper_multiply, // nb_multiply
(binaryfunc) ValueWrapper_divide, // nb_divide
0, // nb_remainder
0, // nb_divmod
(ternaryfunc) ValueWrapper_power, // nb_power
(unaryfunc) ValueWrapper_negative, // nb_negative
0, // nb_positive
(unaryfunc) ValueWrapper_absolute, // nb_absolute
(inquiry) ValueWrapper_nonzero, // nb_nonzero
0, // nb_invert
0, // nb_lshift
0, // nb_rshift
0, // nb_and
0, // nb_xor
0, // nb_or
0, // nb_coerce
(unaryfunc) ValueWrapper_int, // nb_int
0, // nb_long
(unaryfunc) ValueWrapper_float, // nb_float,
0, // nb_oct
0, // nb_hex
0, // nb_inplace_add
0, // nb_inplace_subtract
0, // nb_inplace_multiply
0, // nb_inplace_divide
0, // nb_inplace_remainder
0, // nb_inplace_power
0, // nb_inplace_lshift