views:

357

answers:

2

I'm using the TreeView control in Maya 2009 but I'm getting a syntax error on drag and drop. My code is as follows (simplified for brevity):

 class View(event.Dispatcher):
    def __init__(self):
     self.window = cmds.window()
     tree_view = cmds.treeView(
      numberOfButtons=1,
      allowReparenting=True,
      dragAndDropCommand=self.tree_view_onDrag
     )
     cmds.showWindow(self.window)

    def tree_view_onDrag(self, dropped_items, old_parents, old_indexes, new_parent, new_indexes, item_before, item_after, *args):
     print "worked"

When I drag and drop and element I get the following command is executed in the console:

<bound method View.tree_view_onDrag of  {"layer 3"} {""} {1} "layer 1"  {0} "" "layer 2";

And get the following error:

// Error: <bound method View.tree_view_onDrag of  {"layer 3"} {""} {1} "layer 1"  {0}€ // 
// Error: Line 1.1: Syntax error //

EDIT: It turns out that the issues I was having were due to the treeView still implementing MEL function calls on most of its event callbacks. The errors above are being thrown by the MEL interpreter as it attempts to feed arguments to a command name.

+1  A: 

See http://download.autodesk.com/us/maya/2009help/CommandsPython/treeView.html: dragAndDropCommand is a STRING -- you're passing a bound method, Maya's using its repr. I'm not sure, but I suspect that string should name a top-level (module-level) function, not a bound method.

Alex Martelli
Aha! Good catch! What's funny is I've had this happen before now that I think about it. But the error is so ambiguous that I never think to check the type.
Soviut
A: 

As of Maya 2010 the treeView widget appears to still require a string name of a mel procedure to be used for some of its callbacks, but not for others. For example, the dragCallback and dropCallback do work as expected, but the selectCommand and others don't. Many other widgets do accept a python function for their callbacks. Even though the docs list the arguments for some treeView callbacks as strings, it isn't stated that the string must be a mel procedure name, and it is certainly inconsistent.

kb