PDA

View Full Version : draggerContext in python


walfridson
11-15-2010, 11:12 AM
I can't get draggerContext to work in python. Works great in MEL.
In my own code and in the sample from the docs I get the following error:
# Error: name 'SampleContextPress' is not defined

import pymel.core as pm

# Procedure called on press
def SampleContextPress():
pressPosition = pm.draggerContext( 'sampleContext', query=True, anchorPoint=True)
print ("Press: " + str(pressPosition))

# Procedure called on drag
def SampleContextDrag():
dragPosition = pm.draggerContext( 'sampleContext', query=True, dragPoint=True)
button = pm.draggerContext( 'sampleContext', query=True, button=True)
modifier = pm.draggerContext( 'sampleContext', query=True, modifier=True)
print ("Drag: " + str(dragPosition) + " Button is " + str(button) + " Modifier is " + modifier + "\n")
message = str(dragPosition[0]) + ", " + str(dragPosition[1])
pm.draggerContext( 'sampleContext', edit=True, drawString=message)

# Define draggerContext with press and drag procedures
pm.draggerContext( 'sampleContext', pressCommand='SampleContextPress()', dragCommand='SampleContextDrag()', cursor='hand' );

# Set the tool to the sample context created
# Results can be observed by dragging mouse around main window
pm.setToolTo('sampleContext')

NaughtyNathan
11-15-2010, 01:30 PM
I don't use pymel Johan, but just to test your code I changed the first line (import pymel.core as pm) to:

import maya.cmds as pm

and all your code worked fine with no errors, so I can only assume it's something to do with how you are using pymel..?
:nathaN

walfridson
11-15-2010, 02:23 PM
Oh I tried maya's old python as well. Using 2009 here.
Both one gives errors. Maybe it has been fixed in later version.

NaughtyNathan
11-15-2010, 02:52 PM
I doubt Autodesk have done any work on draggerContext since it was first introduced :(

your example code (with my single import line changed) works in Maya 8.5, 2008ext2 and 2010. ironically Maya 2009 is the only version I don't have access to :D I use draggerContext all the time in Python and never seen any issues in any Maya version.

There must be something else wrong on your end..?
:nathaN

walfridson
11-15-2010, 04:56 PM
Thanks for testing!
Tried it in 2011 and works fine.

Strange! Will test more...

walfridson
11-15-2010, 05:14 PM
Hm seems like there is something with my shelf button.
import draggercode
reload(draggercode)

pasting it in script editor works fine however..

NaughtyNathan
11-15-2010, 05:42 PM
are you sure it's a python shelf button and not a MEL one? :D ;)

NateH
11-15-2010, 06:40 PM
Hm seems like there is something with my shelf button.
import draggercode
reload(draggercode)

pasting it in script editor works fine however..
That's because your code is looking for "SampleContextPress" in the __main__ namespace. (Any code executed as a string for a python command is executed in __main__ globals() and locals().)
You need to change how it finds your code, theres a few ways to do this:
A) Use "draggercode.SampleContextPress" and "draggercode.SampleContextDrag" for your callback.
B) In your draggercode.py file add something like this to the end to copy your code into __main__:
import __main__
__main__.SampleContextPress = SampleContextPress
__main__.SampleContextDrag = SampleContextDrag
C) Use "from x import *" in your shelf button:
from draggercode import *
pm.setToolTo('sampleContext')
D) Pass a function as the command, rather than a string:
pm.draggerContext( 'sampleContext', pressCommand=SampleContextPress, dragCommand=SampleContextDrag, cursor='hand' )


Generally passing the function rather than a string is the best way to set it up, that way you don't have to deal with namespaces at all. However some UI commands don't allow you to do this, in which case you have to work around it.

meuH
11-15-2010, 09:22 PM
I doubt Autodesk have done any work on draggerContext since it was first introduced :(


sadly I wish they did.

and on a side note as it's not related to the OP problem:
after having had to beat my head with weird problems for a while, the switch over to Qt with 2011 had some nasty side effects with how the events are now handled. (not sure if that has been adressed with any of the fixes, but nothing was reported in the release notes though).
It seems the UI has the priority over the draggerContext commands, and the events actually renter themselves before their code is finished. Leading to all sort of nasty stuff if you are using the context to test with data from a previous position for example.
I had to protect all the commands I was using for the dragger context to prevent rentry if the code of the previous drag command wasn't finished.

walfridson
11-16-2010, 09:06 AM
Ah! Awesome Nathan :)

D doesn't seem to work though. No errors, but neither press or drag function gets called

import maya.cmds as cmds

def SampleContextPress():
pressPosition = cmds.draggerContext( 'sampleContext', query=True, anchorPoint=True)
print ("Press: " + str(pressPosition))


def SampleContextDrag():
dragPosition = cmds.draggerContext( 'sampleContext', query=True, dragPoint=True)
button = cmds.draggerContext( 'sampleContext', query=True, button=True)
modifier = cmds.draggerContext( 'sampleContext', query=True, modifier=True)
print ("Drag: " + str(dragPosition) + " Button is " + str(button) + " Modifier is " + modifier + "\n")
message = str(dragPosition[0]) + ", " + str(dragPosition[1])
cmds.draggerContext( 'sampleContext', edit=True, drawString=message)


cmds.draggerContext( 'sampleContext', pressCommand='sample.SampleContextPress', dragCommand='sample.SampleContextDrag', cursor='hand' );

cmds.setToolTo('sampleContext')

NateH
11-16-2010, 05:33 PM
Ah! Awesome Nathan :)

D doesn't seem to work though. No errors, but neither press or drag function gets called


Yeah, some commands just wont take a function as an argument. unfortunately they don't include this in the documentation, so you just have to try it and see. If they don't, then one of the other 3 solutions should work just fine.

CGTalk Moderation
11-16-2010, 05:33 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.