PDA

View Full Version : User Input


CadOp
12-06-2011, 04:50 AM
Hello,

I am having a hard time finding a way to prompt the user to select an object. Ive been using python in Rhino3D for a few months now and it has a set of functions like, getObject("Select Object") ... getInt("Enter Integer")... Does Maya have this? I have seen some references to OpenMaya, the ls function, and prompt. I have used the promptDialog for entering values, but do not see a way to have a user select objects. I have also found mc.draggerContext() which seems pretty cool but doesnt seem to do what I want either. The MObject looked like it was what I wanted but after reading the API it doesnt look like it anymore.

Can someone point me to a reference or help sort out all these for me?

Thanks!

WesHowe
12-06-2011, 06:59 AM
You should look for the promptDialog function.

All the Maya script commands are in the technical documentation section of the Maya help files. There are sections for MEL and for Python. MEL is the original Maya scripting language, it uses a C-like syntax. Python has access, for the most part, to all of the same commands, as well as being able to access the OpenMaya API, which otherwise requires C++.

<* Wes *>

uiron
12-06-2011, 07:19 AM
maya is not really designed to be used that way. even if you emulate something like this, it would not be intuitive to maya users. most commands in maya require selecting everything before command, where selection order matters - e.g., select joints, mesh and say "bind skin".

if you want a more complex workflow, I'd suggest going with UI window, where you can pick nodes into UI, and using "nameField" UI widget, specifically meant to represent a node selection.

Azrail
12-06-2011, 09:34 AM
You can use the scriptCtx, but it's kinda tricky since even that you call it from python you still need to supply the completion command as mel:

import maya.cmds as cmds
ctx = cmds.scriptCtx( title='Select Something', totalSelectionSets=1, fcs="select -r $Selection1; python(\"doSmth('\"+$Selection1[0]+\"')\");", cumulativeLists=False, expandSelectionList=True, setNoSelectionPrompt='Select Object', setSelectionPrompt='Never used', setDoneSelectionPrompt='Never used because setAutoComplete is set', setAutoToggleSelection=True, setSelectionCount=1, setAutoComplete=True)
cmds.setToolTo(ctx)

def doSmth(obj):
print "You selected", obj

but this way you can fire the cscriptCtx, and based on the result (do you have a selection or not) to run or not the rest of the script.

CadOp
12-06-2011, 07:01 PM
Thanks Azrail, that was what I needed. Its unfortunate that its so difficult to just get a user input.

Azrail, or anyone else, know of a common practice way to do this? Would it make sense for me to put the scriptctx in a separate python module and import it to consolidate the coding. Something like,

UI.py
import maya.cmds as mc
item='selection'
def User_Select():
mc.scriptCtx( title='Select Something', totalSelectionSets=1, fcs="select -r $Selection1; python(\"Sel_Func('\"+$Selection1[0]+\"')\");", cumulativeLists=False, expandSelectionList=True, setNoSelectionPrompt='Select Object', setSelectionPrompt='Never used', setDoneSelectionPrompt='Never used because setAutoComplete is set', setAutoToggleSelection=True, setSelectionCount=1, setAutoComplete=True)

return item

def Sel_Func(item):
return item


Curves.py
import maya.cmds as mc
import UI as ui
my_selection=ui.UserSelect()

This isnt working, besides for the global variable not changing, if i import the module the selection script doesnt run, no errors, just nothing happens. Any ideas?

Azrail
12-07-2011, 04:11 PM
You've missed to set the tool to your context:
mc.setToolTo(contextName)
your python should either do this, or return the new context so you can start it later on... check my example again - it's the last line

CadOp
01-24-2012, 08:01 PM
Its been awhile but bringing this up again because i never got it working. I tried it another way, this is currently how I am approaching it. With slightly different code i was able to call the User_Select() function from another module but it would automatically say nothing was selected, i.e. it still does the problem of automatically continuing in the code without waiting. In the end, i just need a function that will wait until i select an object, and i need to have that object assigned to the variable. I would like this in a separate module as i need to continually assign variables. I need to do things like "Select two curves", then the curves are selected, then i loft it.

Thanks for any help

#User Selection Prompt
import maya.cmds as mc


from maya.api import*





def User_Select():


ctx = mc.scriptCtx( title='Select Something', totalSelectionSets=1, fcs="select -r $Selection1; python(\"Sel_Func('\"+$Selection1[0]+\"')\");", cumulativeLists=False, expandSelectionList=True, setNoSelectionPrompt='Select Object', setSelectionPrompt='Never used', setDoneSelectionPrompt='Never used because setAutoComplete is set', setAutoToggleSelection=True, setSelectionCount=1, setAutoComplete=True)


mc.setToolTo(ctx)


return obj





def Sel_Func(obj):


MSelectionList()


if ( MGlobal.getActiveSelectionList(sel) == None):


User_Select()


else:


pass


return obj

#Selection Code
import maya.cmds as cmds
import UserSelect as us



obj= None

my_selection=us.Sel_Func(obj)



print "Your Selection is"

print my_selection

CGTalk Moderation
01-24-2012, 08:01 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.