Passing Parameters

Hello all,

I'm trying to learn Python. I created an UI with a button.
That button needs to pass a parameter to a function:

CODE
mel.button(label = "myLabel", width = 480, command = self.checkUserInterface(temp), bgc=(tint1, tint1, tint1))

When I try this it returns an error:
CODE
# Error: Invalid arguments for flag 'command'.  Expected string or function, got NoneType
# Traceback (most recent call last):
#   File "", line 2, in ?
#   File "", line 262, in UI
# TypeError: Invalid arguments for flag 'command'.  Expected string or function, got NoneType #

is it at all possible to pass parameters to a function through the button mel command?

thanx in advance
Sjoerd

Yes but only globals have any meaning for the ELF items, because they are not being called form your script but outside form it. And your scipt has long since terminated its run wehan the button is being pressed (otherwise it would block the gui)

Its far better to chek everything at once every time a action and pack the name of some widget permanently into teh command program then you wouldn't need the global variable.

(besides doing it his way each time a item is changed to change some internal value that then gets passed to the execute is strictly speaking double bookkeeping, as long as the gui exists every piece of info is already being stored in it)

Thanks for the reply!
Sorry for the late respond time, I have been quite busy.

What if for example you want this setup:

import maya.cmds as cmds

class myTool:

a = myTool()
a.myBrowser()

The only way I can make the filebrowserdialog work is when I declare the command and the procedure globally
wich I dont want... I want the fileBrowserDialog and the file command to be within my class.

any suggestions how to fix this?
thanx in advance,
Sjoerd

Yes but you must leave something global there for you to call into. In this case i would make the object "a" global with some non clash able name. By the way if you execute that inside mayas console as is you just have made a and also mytool definitions global.

You cannot make the functions totally local or the succession would become this:

your script runs its class definitions
your scripts make a class isntance
you create a gui
your instance dies
your instance gets cleaned away
your class gets cleaned away
A aeon in computer time passes
User presses button
nothing happens

So you must leave some handle back to the originator, this is a very common gui programming problem events also allmost allways need to be bound to some globals that have some so scope they dont die before they get triggered. It does not just apply to maya it applies to quite many other stuffs too such as hooking into windows messages without a pump loop.

It would be different if the gui was a main loop you made but it inst its running inside mayas main loop. You arent, and you wont be in any case ever. But you cant lock into a loop because mayas then just running your code and never returning. Well technically speaking you could loop out in thread but thats just stupid, you could as easily kill the global variable later.

QUOTE
wich I dont want... I want the fileBrowserDialog and the file command to be within my class.

global or not they are within your class. The proc definition does not need to be global it will live as long as the object a is alive.

PS this is the reason python implements namespace.

PPS. also incidentally even if your using python you cant escape how maya works, its still mel even tough you write it oin python.

I know, that's what I like about python... that it combines things.

anyways: I executed this piece of script in the maya script editor:

CODE
import maya.cmds as cmds

def importImage( fileName, fileType):
   cmds.file( fileName, i=True );
   return 1

cmds.fileBrowserDialog( m=0, fc='importImage', ft='image', an='Import_Image', om='Import' )

gave this error:

CODE
# Error: line 1: Cannot find procedure "importImage". #

This script is actually taken from the maya help. So it should work but it doesn't.
So I think this command still doesn't work to well in Maya Python...

QUOTE
This script is actually taken from the maya help. So it should work but it doesn't.
So I think this command still doesn't work to well in Maya Python...

Yes but how do you execute it?

globality and locality is very important stuff to understand. otherwise it will never work out for you.

Anyway your most likely actually calling for the mel verion of import image here sho you should wrap that up into a python command call. ill look into it but i have a deadline to friday so...

PS. you don't happen to use a a external python interpreter by any chance?

Yeah I have python 2.5 installed on my system.
That shouldn't be a problem right?

I just copy & pasted the script directly into my command edittor and executed it.
I assume that everything that is executed in there should be global.

I tried this:

in mel:

CODE
global proc importImage(string $f, string $m){
    print "Woohoo";
}

in python:
CODE
import maya.cmds as cmds
cmds.fileBrowserDialog( m=0, fc='importImage', ft='image', an='Import_Image', om='Import' )

which prints out: woohoo.

now in python without the quotations:
CODE
import maya.cmds as cmds

def importImage( fileName, fileType):
   cmds.file( fileName, i=True );
   return 1

cmds.fileBrowserDialog( m=0, fc=importImage, ft='image', an='Import_Image', om='Import' )

Which returns an error:
# Error: "C:/Documents and Settings/maya/Desktop/bpw_test1.0275.iff" "image"; #
# Error: Line 1.1: Syntax error #

The command calls to the python procedure like this:
importImage "C:/Documents and Settings/maya/Desktop/bpw_test1.0275.iff" "image"
which is cool in MEL, but in python should be like this:
importImage("C:/Documents and Settings/maya/Desktop/bpw_test1.0275.iff", "image")

the last one works when I execute it manually in the editor.
Do you think there is a way past this syntax problem?

write a mel wrapper that calls the python command? Anyway that is indeed a oversight form makers of maya as mel should be called like this too:

importImage("C:/Documents and Settings/maya/Desktop/bpw_test1.0275.iff", "image")

mel just accepts shorthand. Anyway its not good idea to replace mel with python, use it instead for api development!

You can allways wearp your python as a api mel command instead.

QUOTE
Yeah I have python 2.5 installed on my system.

Well not unless you use it.

PS one oef the filebrowsers nify features is that its even hard to use in mel because its not really undocumented incorrectly by type.

Yeah that's maybe the best solution.
Thank you for your help!

Sjoerd

Some of the callbacks in Maya have been switched over to accept python or mel code and some have not.

If in a mel tab, you enter
[codebox]help fileBrowserDialog;[/codebox]
you'll get the quick help for the command, which lists all the flags and their associated types. Notice the -fc flag line which is giving you trouble.

-fc -fileCommand String

It's associated type is String, if it were Script then your code would work as expected.
The workaround is simple. It's expecting a mel command string, give it one.
In the form 'python("myFunction")'.

[codebox]import maya.cmds as cmds

def importImage( fileName, fileType):
cmds.file( fileName, i=True );
return 1

cmds.fileBrowserDialog( m=0, fc='python("importImage")', ft='image', an='Import_Image', om='Import' )[/codebox]

Interesting idea but that does not pass in the parameters!

because now maya executes this:

python("importImage") "...path..t/projection1-nurbsSphere1.iff" "image";

And that does not. So theres still some work for autodesk to do on this branch!

I'm confused, it seems to run without error, did you try executing the code?

What is the expected output?

Yes, i ran it in 8.5 sp1.

it does execute the file browser right, but that was never the question. It just never executed the callback right it did execute the python statement but even thet wasnt the question at hand. See its not just a procedure mayas also trying to execute its contents by passing parameters in and that is what we cant do.

see some few maya command dont just execure functions they requitre fuctions that get passed input values and thats not what we are able to do. Still leaves out the values for the function.

And yes it runs without erroring but that don't mean it does things right.

right now maya executes

python("importImage") "....path to selected file../filename" "image";

wich is obviously not the intent nor the result this thread is looking for. Your comment is right but its the wrong kind of callback your thinking of. theres just no way to get the parameters passed in at least not with that method

You are correct.

if we could correctly identify the mel syntax for positional arguments, we might be able to work around this.

Below is a workaround for this instance using fileDialog which is less os specific, but might not be useful in all cases.

[codebox]import maya.cmds as cmds

def importImage( fileName):
cmds.file( fileName, i=True );
return 1

fileName=cmds.fileDialog()
importImage(fileName)[/codebox]

Well you could just make a mel wrapper:

proc wrapMel2PythonimportImage(string $fileName,string $fileType){
python("importImage( "+$fileName+","+$fileType+")");
}

the synax is what i showed you, but python must have , between arguments and ) at last maya dont inject this so theres really no other way around. Or a more generic wrapper one

proc exectuePythoWith2Params(string $cmd,string $fileName,string $fileType){
python($cmd+"( "+$fileName+","+$fileType+")");
}

and wirte the command as :

cmds.fileBrowserDialog( m=0, fc='exectuePythoWith2Params "importImage" ', ft='image', an='Import_Image', om='Import' )

off course you could make a python api mel command with python that assembles this then you'd have possibility for any number of prams.

I like the idea of the scripted plugin for any number of params!

Here's what I put together with a mel proc inside the python script along the same lines as you suggested.

[codebox]import maya.cmds as cmds
import maya.mel as mel

def importImage( fileName, fileType):
print "made it"
print fileName
print "into python"
print fileType
cmds.file( fileName, i=True );
return 1

mel.eval('global proc importImageCallback( string $path, string $tp ) {python ("importImage(\'" + $path + "\', \'" + $tp + "\')\n");}')

cmds.fileBrowserDialog( m=0, fc='importImageCallback', ft='image', an='Import_Image', om='Import' )
[/codebox]

Thanks for your help!
It can work now !