forked from blurstudio/Py3dsMax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_struct.cpp
260 lines (207 loc) · 7.33 KB
/
python_struct.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
/*
\file python_struct.cpp
\remarks This file defines the 3dsMax struct called python
\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 <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <import.h>
#include <graminit.h>
#include <pythonrun.h>
// updated to work with max 2012's new maxscript organization
#if __MAXSCRIPT_2012__ || __MAXSCRIPT_2013__ || __MAXSCRIPT_2015__
#include "macros/define_external_functions.h"
#include "macros/define_instantiation_functions.h"
// include settings for previous versions of 3dsmax
#else
#include "defextfn.h"
#include "definsfn.h"
#endif
#include "macros.h"
#include "wrapper.h"
// Define the python struct in 3dsMax
def_struct_primitive( import, pymax, "import" );
def_struct_primitive( reload, pymax, "reload" );
def_struct_primitive( run, pymax, "run" );
def_struct_primitive( exec, pymax, "exec" );
// python.import function: import a python module to maxscript
Value*
import_cf( Value** arg_list, int count ) {
// Step 1: make sure the arguments supplied are correct
check_arg_count( python.import, 1, count );
// Step 2: protect the maxscript memory
MXS_PROTECT( two_value_locals( mxs_check, mxs_return ) );
// Step 3: convert the maxscript value
MXS_EVAL( arg_list[0], vl.mxs_check );
// Step 4: calculate the python module name to import
const MCHAR* module_name = NULL;
try { module_name = vl.mxs_check->to_string(); }
MXS_CATCHERRORS();
// Step 5: import the module
if ( module_name ) {
MCharToPyString pys(module_name);
if( pys.pyString() ) {
PyObject* module = PyImport_Import( pys.pyString() );
vl.mxs_return = ( module ) ? ObjectWrapper::intern( module ) : &undefined;
}
PY_ERROR_PROPAGATE_MXS_CLEANUP();
}
else {
mprintf( _T("python.import() error: importing modules must be done with a string value\n") );
vl.mxs_return = &undefined;
}
MXS_RETURN( vl.mxs_return );
}
// python.reload function: reload an existing module in maxscript
Value*
reload_cf( Value** arg_list, int count ) {
// Step 1: make sure the arguments supplied are correct in count
check_arg_count( python.reload, 1, count );
// Step 2: evaluate the input item
MXS_PROTECT(one_value_local(mxs_check));
MXS_EVAL( arg_list[0], vl.mxs_check );
// Step 3: make sure the item is a proper type
if ( is_objectwrapper(vl.mxs_check) ) {
PyImport_ReloadModule( ((ObjectWrapper*) vl.mxs_check)->object() );
PY_ERROR_PROPAGATE_MXS_CLEANUP();
}
else { mprintf( _T("python.reload() error: you need to supply a valid python module to reload\n") ); }
MXS_CLEANUP();
return &ok;
}
// python.run function: run a python file from maxscript
Value*
run_cf( Value** arg_list, int count ) {
// Step 1: make sure the arguments supplied are correct in count
check_arg_count( python.run, 1, count );
// Step 2: protect the maxscript memory
MXS_PROTECT(one_value_local(mxs_filename));
MXS_EVAL( arg_list[0], vl.mxs_filename );
// Step 2: create a python file based on the filename
const MCHAR * filename = vl.mxs_filename->to_string();
//mprintf( _T("Got Filename to run: %s\n"), filename );
MCharToPyString pys(filename);
PyObject* args = PyTuple_New(2);
PyTuple_SET_ITEM(args,0,pys.pyStringRef());
PyTuple_SET_ITEM(args,1,PyString_FromString("r"));
//mprintf( _T("Arg tuple created, creating python file object\n") );
PyObject* py_file = PyObject_Call((PyObject*)&PyFile_Type, args, NULL);
Py_DECREF(args);
if( !py_file ) {
mprintf( _T("Call to python file object creation failed\n") );
PY_ERROR_PROPAGATE_MXS_CLEANUP();
return &false_value;
}
//mprintf( _T("File opened, calling PyRun_SimpleFile\n") );
// Step 4: run the file
PyRun_SimpleFile( PyFile_AsFile(py_file), pys.data() );
//mprintf( _T("File ran, cleaning up\n") );
// Step 5: cleanup the memory
Py_DECREF( py_file );
PY_ERROR_PROPAGATE_MXS_CLEANUP();
return &true_value;
}
// python.exec function: execute a python command through a maxscript string
Value*
exec_cf( Value** arg_list, int count ) {
// Step 1: make sure the arguments supplied are correct in count
check_arg_count( python.exec, 1, count );
// Step 2: protect the maxscript memory
MXS_PROTECT(one_value_local(mxs_command));
MXS_EVAL( arg_list[0], vl.mxs_command );
// Step 2: create a python file based on the filename
const MCHAR* command = NULL;
try { command = vl.mxs_command->to_string(); }
MXS_CATCHERRORS();
// Step 3: check to make sure the command is valid
if ( !command ) {
MXS_CLEANUP();
return &false_value;
}
{
MCharToPyString ascii(command);
if( ascii.pyString() )
PyRun_SimpleString( ascii.data() );
PY_ERROR_PROPAGATE_MXS_CLEANUP();
}
// Step 5: cleanup the memory
MXS_CLEANUP();
return &ok;
}
PyExcRuntimeError::PyExcRuntimeError( MCHAR * _error )
: RuntimeError( _error )
, error( _error )
{}
PyExcRuntimeError::~PyExcRuntimeError()
{
delete error;
}
// Returns a new string
MCHAR * pythonExceptionTraceback( bool clearException )
{
/*
import traceback
return '\n'.join(traceback.format_exc()).encode('ascii','replace')
*/
MCHAR * ret = 0;
bool success = false;
PyObject * type, * value, * traceback;
/* Save the current exception */
PyErr_Fetch(&type, &value, &traceback);
if( type ) {
PyObject * traceback_module = PyImport_ImportModule("traceback");
if (traceback_module) {
/* Call the traceback module's format_exception function, which returns a list */
PyObject * traceback_list = PyObject_CallMethod(traceback_module, "format_exception", "OOO", type, value ? value : Py_None, traceback ? traceback : Py_None);
if( traceback_list ) {
PyObject * separator = PyString_FromString("");
if( separator ) {
PyObject * retUni = PyUnicode_Join(separator, traceback_list);
if( retUni ) {
#ifdef UNICODE
ret = _tcsdup( PyUnicode_AsUnicode(retUni) );
#else
PyObject * retAscii = PyUnicode_AsEncodedString(retUni, "ascii", "replace");
if( retAscii ) {
Py_ssize_t len = 0;
char * tmp;
if( PyString_AsStringAndSize( retAscii, &tmp, &len ) != -1 ) {
ret = strdup( tmp );//, len );
success = true;
} else {
ret = _tcsdup( _T("Uhoh, failed to get pointer to ascii representation of the exception") );
success = false;
}
Py_DECREF( retAscii );
} else {
ret = _tcsdup( _T("Uhoh, encoding exception to ascii failed") );
success = false;
}
#endif
Py_DECREF(retUni);
} else
ret = _tcsdup(_T("PyUnicode_Join failed"));
Py_DECREF(separator);
} else
ret = _tcsdup(_T("PyUnicode_FromString failed"));
Py_DECREF(traceback_list);
} else
ret = _tcsdup(_T("Failure calling traceback.format_exception"));
Py_DECREF(traceback_module);
} else
ret = _tcsdup(_T("Unable to load the traceback module, can't get exception text"));
} else
ret = _tcsdup(_T("pythonExceptionTraceback called, but no exception set"));
if( clearException ) {
Py_DECREF(type);
Py_XDECREF(value);
Py_XDECREF(traceback);
} else
PyErr_Restore(type,value,traceback);
return ret;
}