PDA

View Full Version : itemFilter using byScript via python?


delarge
09-13-2010, 01:39 PM
Hi folks,
I'm trying to pass a python function to an itemFilter, which doesn't seem to work. The problem seems to be that a DAG nodename is passed implicitely to the function, which causes a syntax error when passed like this

cmds.itemFilter("myFilter", byScript = "myFilterProc()")

def myFilterProc(name):
return 1

filter = cmds.itemFilter("myFilter", byScript = "myFilterProc()")
cmds.outlinerEditor(name, edit = True, filter = filter)

# Result: #
// Error: myFilterProc() "persp"; //
// Error: Line 1.22: Syntax error //
// Error: myFilterProc() "perspShape"; //
// Error: Line 1.27: Syntax error //
...

In the above result that Maya script editor spits out you can see that
myFilterProc() "persp"

is called. "persp" is the argument for the preceding function name. Which means one cannot use a python function to pass to a -byScript flag for an itemfilter. Unfortunately the python itemFilter -byScript docs are the same as the mel docs.

Has anyone had any success with that before?

Cheers
R

ps. don't worry about the code above - this is just to illustrate the whole thing

NaughtyNathan
09-13-2010, 02:29 PM
you could try:

filter = cmds.itemFilter("myFilter", byScript = myFilterProc)


but it looks as though Autodesk didn't hook it up properly... I think there was a similar issue with scriptJob and fileBrowserDialogs on Maya 8.5

:nathaN

delarge
09-14-2010, 09:11 AM
this unfortunately doesn't work either, since python disallows calling a funtion without using brackets...

you're right, this pretty much sound like a bad hook. anyway. I've found a cheap and nasty way by just declaring a mel proc in the string passed to -byScript:


cmds.itemFilter("myFilter", byScript = 'global proc int myFilterProcMEL(string $name) {return `python("myFilterProcPYTHON(\'\" + $name + \"\')")}`; myFilterProcMEL')


this way I can use python - no need for messing around with mel.

R

NaughtyNathan
09-14-2010, 10:37 AM
this unfortunately doesn't work either, since python disallows calling a funtion without using brackets...
you are NOT calling a function here, you are referencing the function object.

for example:

mc.button( l='scatter', c='doScatter()' ) # poor
mc.button( l='scatter', c=doScatter ) # much better
While neither of those approaches is 100% perfect, the latter is generally better, especially if your function takes no args.

:nathaN

delarge
09-14-2010, 11:43 AM
you're right, that works if there's no implicit argument - like there is with this weird itemFilter -byScript method. again, with what you suggested itemFilter will give you

// Error: <function doScatter at 0xa702320> "persp"; //


times nodes in your scene. function is not an object, btw, but a type. that's why you can't run isinstance(doScatter, function), since a function is not an instance of a class or anything...

thanks for your suggestions though. wasn't aware you could pass a function name without brackets eg to a command flag of a control.

cheers
r

NaughtyNathan
09-14-2010, 12:24 PM
function is not an object, btw, but a type.

in python, everything is an object! :D

delarge
09-14-2010, 12:55 PM
if you're using python lingo, yes. and you're example illustrates that perfectly.

"Still, this begs the question. What is an object? Different programming languages define “object” in different ways. In some, it means that all objects must have attributes and methods; in others, it means that all objects are subclassable. In Python, the definition is looser; some objects have neither attributes nor methods, and not all objects are subclassable. But everything is an object in the sense that it can be assigned to a variable or passed as an argument to a function."

http://diveintopython.org/getting_to_know_python/everything_is_an_object.html#d0e4665

what I meant is class-object.

try this in a python shell:

In [1]: def myFunc(*args):
...: print "my function"
...:

In [2]: myFunc()
my function

In [3]: myFunc
Out[3]: <function myFunc at 0x1e76488>

In [4]: isinstance(myFunc, funtion)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython console> in <module>()
NameError: name 'funtion' is not defined

that is, because myFunc is NOT an instance of a class object, because there is no class myFunc.

CGTalk Moderation
09-14-2010, 12:55 PM
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.