PDA

View Full Version : using hotkey to get user input


mradfo21
02-09-2011, 06:53 PM
hey guys, i'm messing with this today and coming up short.

i'm just trying to tell when the user has pressed the "n" key:

import maya.cmds as mc
def printTest():
print "pressed the n button"
mc.nameCommand("pressN", annotation = "presses the n key", sourceType ='python', command = printTest)
mc.hotkey(k = 'n', name = 'pressN')


and this sources, but when I go to run it, as you'd imagine i get

p, li { white-space: pre-wrap; } // Error: <function printTest at 0xe97c1b8>; //



which makes sense cause i'm just giving it the location of printTest, when what it wants is printTest()


however, if i change printTest to printTest() then i get an error when i compile:

p, li { white-space: pre-wrap; }
# Error: TypeError: Invalid arguments for flag 'command'. Expected string or function, got NoneType #





anyone have any experience getting this to work? alternatively, has anyone used another method of grabbing the user's keystrokes? tkinter just crashes maya for me.


Cheers!!!


- Matt Radford

dbsmith
02-09-2011, 07:07 PM
Your printTest() function needs to take arguments that come automatically from being called as a command.

Change it to:
def printTest(*args):

pritishd
02-09-2011, 09:57 PM
You need to pass the command arguement as a string. Also I think you needed to put a indentation when you start a new function/proc


import maya.cmds as mc

def printTest():
print "pressed the n button"
mc.nameCommand("pressN", annotation = "presses the n key", sourceType ='python', command = "printTest()")
mc.hotkey(k = 'n', name = 'pressN')

mradfo21
02-09-2011, 11:19 PM
thanks for the responses guys!

dbsmith i added the *args and no go. it will still only see the memory adress, not the actual function.

pritishd i tried calling it as a string aswell. odly no go. and sorry about the horribly formatted version of the code i pasted in.

again, thanks for the responses guys! I'll look into a way of getting user input without the nameCommand

Cheers!

pritishd
02-09-2011, 11:28 PM
pritishd i tried calling it as a string aswell. odly no go. and sorry about the horribly formatted version of the code i pasted in.
Cheers!

mmmm.....I just ran the code I gave you and worked without any hassle. Make sure you clear your named command and hotkey preferences and restart maya.

mradfo21
02-10-2011, 12:41 AM
weird.. after resetting hotkeys and maya, i decided to try just copying and pasting your code in and I get

p, li { white-space: pre-wrap; } // Error: Cannot find procedure "printTest". //



i did some testing earlier and when i re-wrote the function in mel, it all would work. it makes me think something isn't getting that i'm saying sourceType = "python".


i tried using the mel.createMelWrapper to wrap a python function i had written but no luck.


I'll give it a go when i get home. They've ripped maya to shreds here, maybe somethings getting lost in translation.


thanks for the support, i really appreciate it !

pritishd
02-10-2011, 12:57 AM
weird.. after resetting hotkeys and maya, i decided to try just copying and pasting your code in and I get

p, li { white-space: pre-wrap; } // Error: Cannot find procedure "printTest". //

I tried using the mel.createMelWrapper to wrap a python function i had written but no luck.


I'll give it a go when i get home. They've ripped maya to shreds here, maybe somethings getting lost in translation.


thanks for the support, i really appreciate it !


Ok, this time I deleted all my prefs and tried it on a fresh setup, and it worked. Just out of curiosity what version of maya are you using?

i did some testing earlier and when i re-wrote the function in mel, it all would work. it makes me think something isn't getting that i'm saying sourceType = "python".

Come to think of it. I do remember having issues with excecuting nameCommand/runTimeCommand in pure python.

I had to run the python via mel python command. Let's give this a try on a fresh setup. If it does not work, then there I can't think of anything else

import maya.cmds as mc
def printTest():
print "pressed the n button"
mc.nameCommand("pressN",
annotation = "presses the n key",
command ="python (\"printTest()\");",)
mc.hotkey(k = 'n', name = 'pressN')

mradfo21
02-10-2011, 04:15 PM
you sir are a genius !!

the python trick worked!

I didn't know this kind of python syntax existed. so you just specify:

python (\" your function \")?


Hey man i really appreciate the support. very kind of you.

looks like i might have some more coding time today so i'm gonna try and put some more functionality into this and post what i come up with.

pritishd
02-10-2011, 06:03 PM
you sir are a genius !!

No worries.

the python trick worked!

I didn't know this kind of python syntax existed. so you just specify:

python (\" your function \")?

Because it is not python. It's mel

(http://tinyurl.com/5wxgk6p) http://tinyurl.com/5wxgk6p

It's pretty much the mel equivalent of mayaMel.eval().

However since the python command needs to be quotes, you have to use the backslash before the quote so that the string is not closed. eg if you want to give a message with quotes you have to use the following syntax

print "Message with \"Quotes\""

mradfo21
02-11-2011, 04:59 PM
ok so here is some of the code I wrote yesterday. it makes a nice little function to make creating hotkeys that call python functions really easy.

for example, constructKey("pressFunction()","releaseFunction()", "n")

this will use the n key to call these functions.


p, li { white-space: pre-wrap; }
def constructKey(pressFunc, releaseFunc, key):
def makePress(Func,key1):
#slot 0 is pressName, 1 is releaseName, 2 is pressFuncCall
returnList = []
pressName = "press_" + key
releaseName = "release_" + key
concatPython = "python ("
concatParen = '"' + Func +'"' +");"
pressFuncCall = concatPython + concatParen
returnList.append(pressName)
returnList.append(releaseName)
returnList.append(pressFuncCall)
return returnList
# this returns an array that has all the info needed to make nameCommands
pressFuncInfo = makePress(pressFunc,key)
releaseFuncInfo = makePress(releaseFunc,key)
## makes the name commands using the info gathered from the makePress function
mc.nameCommand(pressFuncInfo[0], annotation = "when you press the "+key+" key", sourceType = 'python', command = pressFuncInfo[2] )
mc.nameCommand(pressFuncInfo[1], annotation = "when you release the "+key+" key", sourceType = 'python', command = releaseFuncInfo[2])
mc.hotkey(k = key, name = pressFuncInfo[0], releaseName = pressFuncInfo[1] )
return pressFuncInfo
## declaring the keys and their corresponding functions



thanks for your help with this!

I'm about to make another post, b/c i'm a bit stuck again with what i wanted to use this for

CGTalk Moderation
02-11-2011, 04:59 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.