Simon Eves wrote:
I am trying to write a Python module to embed the functionality of Maya (the 3D modelling and animation application from Autodesk, formerly Alias) for doing scripted scene manipulation and rendering processes in Python.
Here are both versions of the code, in case what I wrote didn't make sense. Simon
#include "Python.h" #include "maya/MStatus.h" #include "maya/MLibrary.h" #include "maya/MGlobal.h" // global init static bool _mayaInitialized = false; static PyObject* maya_initialize(PyObject* self, PyObject* args) { if (!_mayaInitialized) { // initialize Maya // script output to stdout // view-only license MStatus status = MLibrary::initialize(true, "mayamodule", true); if (status != MS::kSuccess) { printf("// Error: Failed to initialize Maya, exiting...\n"); exit(1); } // remember _mayaInitialized = true; } else printf("// Warning: Maya already initialized\n"); Py_INCREF(Py_None); return Py_None; } static PyObject* maya_mel(PyObject* self, PyObject* args) { const char* command; if (PyArg_ParseTuple(args, "s", &command)) { printf("Executing '%s'...\n", command); MStringArray results; MStatus status = MGlobal::executeCommand(MString(command), results); if (status != MS::kSuccess) printf("// Error: %s\n", status.errorString().asChar()); else for (unsigned int i = 0; i < results.length(); i++) printf("// %s\n", results[i].asChar()); } Py_INCREF(Py_None); return Py_None; } static PyObject* maya_cleanup(PyObject* self, PyObject* args) { // shut down Maya if (_mayaInitialized) { printf("// Shutting down Maya"); MLibrary::cleanup(); } // this will never be reached since MLibrary::cleanup() does an exit() Py_INCREF(Py_None); return Py_None; } static PyMethodDef maya_methods[] = { {"initialize", maya_initialize, METH_NOARGS, "Initialize Maya library"}, {"mel", maya_mel, METH_VARARGS, "Execute arbitrary MEL command"}, {"cleanup", maya_cleanup, METH_NOARGS, "Shut down Maya library and exit"}, {NULL, NULL} }; PyMODINIT_FUNC initmaya() { printf("// mayamodule v1.0\n"); Py_InitModule("maya", maya_methods); }
#include <stdlib.h> #include <stdio.h> #include <Python.h> #include <maya/MLibrary.h> #include <maya/MGlobal.h> #include <maya/MString.h> #include <maya/MStringArray.h> PyObject* mel(PyObject* self, PyObject* args) { const char* command; if (PyArg_ParseTuple(args, "s", &command)) { printf("Executing '%s'...\n", command); MStringArray results; MStatus status = MGlobal::executeCommand(MString(command), results); if (status != MS::kSuccess) printf("// Error: %s\n", status.errorString().asChar()); else for (unsigned int i = 0; i < results.length(); i++) printf("// %s\n", results[i].asChar()); } Py_INCREF(Py_None); return Py_None; } PyMethodDef mayaMethodTable[] = { {"mel", mel, METH_VARARGS, "Run MEL Command"}, {NULL, NULL} }; int main(int argc, char** argv) { printf("// pymaya v1.0\n"); // get filename if (argc > 2) { fprintf(stderr, "Usage: pymaya [<filename>]\n"); exit(1); } // initialize Maya if (MLibrary::initialize(true, argv[0], true) != MS::kSuccess) exit(1); // initialize Python Py_Initialize(); // add and initialize the module PyImport_AddModule("maya"); Py_InitModule("maya", mayaMethodTable); // import it so that the scene file doesn't have to PyRun_SimpleString("from maya import mel\n"); if (argc == 2) { MString command = MString("execfile('") + MString(argv[1]) + MString("')"); PyRun_SimpleString(command.asChar()); } else PyRun_InteractiveLoop(stdin, "stdin"); // shut down Python Py_Finalize(); // shut down Maya (and exit) MLibrary::cleanup(0); }
-- http://mail.python.org/mailman/listinfo/python-list