PDA

View Full Version : Sourcing python script and using it in mel


rohitdantkale
06-13-2011, 01:19 PM
Hi All, i have build a function in python for using its modules in mel, like i want to use string.find("abc") so i wrote this in python -
def pyCommand(inputString='', operation = '', argument = ''):
val = "\""+inputString + "\"." + operation + "(\"" + argument + "\")"
output = eval (val)
return output

and when i call it from mel like this - it works

string $list = "this is mad cow";
int $i = python("pyCommand('" + $list+ "','find', 'mad')");

but it gives error when i try to source the script, then i put it in default script directory and tried python ("import pyCommand") then it got sourced but while using it, it shows error as given below -

# Error: line 1: 'module' object is not callable
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# TypeError: 'module' object is not callable #

so what can be done for it ?

Azrail
06-13-2011, 01:43 PM
you should access your command by:

import pyCommand
pyCommand.pyCommand()

or

from pyCommand import *
pyCommand()

or

import pyCommand as pc
pc.pyCommand()

.. or something like it

rohitdantkale
06-13-2011, 02:40 PM
you should access your command by:

import pyCommand
pyCommand.pyCommand()

or

from pyCommand import *
pyCommand()

or

import pyCommand as pc
pc.pyCommand()

.. or something like it
Thanx for the reply Azrail, but this is working in python, i wana use it in mel, its not working in it, it errors about module until i manually run the script in the editor, so what can we do here?

mlefevre
06-13-2011, 08:33 PM
python("import pyCommand");
string $list = "this is mad cow";
int $i = python("pyCommand.pyCommand('" + $list+ "','find', 'mad')");

rohitdantkale
06-14-2011, 06:39 AM
python("import pyCommand");
string $list = "this is mad cow";
int $i = python("pyCommand.pyCommand('" + $list+ "','find', 'mad')");

Thankyou, it works,:thumbsup: but i have a query, is this a way to call any of python scripts like function.funcion("...") :curious:

mlefevre
06-14-2011, 08:21 AM
No, pyCommand in this case is the name of the module (file), i.e. pyCommand.py and pyCommand is also the name of the function within the file.

When you call it like so pyCommand.pyCommand, you're really saying myModule.myFunction.
I think Azrail's examples can all be used in this case.

python("import pyCommand as pc");
string $list = "this is mad cow";
int $i = python("pc.pyCommand('" + $list+ "','find', 'mad')");

python("from pyCommand import pyCommand");
string $list = "this is mad cow";
int $i = python("pyCommand('" + $list+ "','find', 'mad')");

rohitdantkale
06-14-2011, 08:37 AM
thanx mlefevre :thumbsup:

CGTalk Moderation
06-14-2011, 08:37 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.