QUOTE
Still,..I have what would appear to be a very simple task, but it is confounding me every which way.
Thats because your making it into a command and dont understand that your now in c++ land.
Mel only has integers, strings,floats,vectors, and matrices anlists of all but matrices. So whatever you return must be of one of those types no automatic type conversion in python.
Now theres 2 different paths to interface with python.
Maya api (but in python as opposed to c++)the mel python commandthe mel python command
Ok ill show the second first because its easier to understand. I know for fact your going for the api solution because you load it as a plug. You probably should not load a plug because what you do is a command and thats just extra overhead on you. And basically because YOU DONT EXPECT to use API really
CODE
python("import maya.cmds as mc");
$return=python("mc.ls(sl=1)");
Ok thats fine and simple so you should probably implement your code as a module instead of a plugin, if your looking for the easy option. (a pyhon module is just a file named yourModuleName.py in your Python\Lib\site-packages directory).
PS: theres a catch 13, it only returns if you call one line!
Maya api
The API way has NOTHING to do with how python works. Its just how maya api works your bound to the API because thats what your using. No returning whatsoever (or really just succes, failure actually)! The conduit of communication in maya api does not work that way. The api is C++ not python you just use python wrappers around thees e things, its still rooted in how C++ works just wirtten in python. Having good knowledge in python only takes you so far here. Basically its not just knowing python anymore.
So let us make a simple exmple Taht takes as arguments a string and return another, for this you will need to make a external file lets call it simpleApiCommandExample.py.
simpleApiCommandExample.py:
CODE
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
#change kPluginCmdName
kPluginCmdName = "simpleApiCommandExample"
class scriptedCommand(OpenMayaMPx.MPxCommand):
def __init__(self):
OpenMayaMPx.MPxCommand.__init__(self)
#basically all you do is re implement this function
def doIt(self, args):
if(args.length()>0):
string=args.asString(0)*2
#following lines are the return
OpenMayaMPx.MPxCommand.clearResult()
OpenMayaMPx.MPxCommand.setResult(string)
# Creator
def cmdCreator():
return OpenMayaMPx.asMPxPtr( scriptedCommand() )
# Initialize the script plug-in
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.registerCommand( kPluginCmdName, cmdCreator )
except:
sys.stderr.write( "Failed to register command: %s\n" % kPluginCmdName )
# Uninitialize the script plug-in
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterCommand( kPluginCmdName )
except:
sys.stderr.write( "Failed to unregister command: %s\n" % kPluginCmdName )
Once you load this call:
CODE
$return=simpleApiCommandExample "test";
print ("got: '"+$return[0]+"'")
you can go all elegant and also just return one thing (if you omit the .clearResult() line) Do read MPxCommand's manual entry in the api reference.
PS: threes one thing of wisdom about api development that states if your doing a command your doing it wrong (coined by Bateman, I happen to agree) so maybe jsut maybe you want to implement it as a module its less code and hassle. Reserve api for nodes, displays and exporters.
PPS: "With great power comes great responsibility"- Stan Lee. More power does not in this case mean simpler.