0

I'm a novice programmer and currently working on a script as a little task for myself. I'm stuck with a part of a script. I need to display the selected object name in a text field

(https://i.stack.imgur.com/wbuIS.jpg)

With the first button, you are supposed to select the object, and as soon as you do that the name of the object should be displayed in the text field below.

Don't know how to write it.

from maya import cmds
import pymel.core as pm

############################

#          UI             #

############################
class drawUI(object):

    def __init__(self):
        myWindow = pm.window(title="Weapon Position Fix")
        pm.columnLayout("basicTools", adjustableColumn=True)
        
        pm.gridLayout("CreateGrid", numberOfRowsColumns=(1, 1), cellWidthHeight=(180, 30), parent="basicTools")
        
        pm.separator(style='in')
        pm.text(label="Select object")       
        self.ObjClass = pm.textField(text = 'Press Set Character' ,editable = False)
        pm.button(c='nameSp()', label='Set Character', parent="CreateGrid")
        pm.separator(style='in')
        pm.button(c='weaponCor(side = R)', label='Right Hand Weapon', parent="CreateGrid")
        pm.separator(style='in')
        pm.button(c='noRWeapon(side = R)', label='Right Hand No Weapon', parent="CreateGrid")
        pm.separator(style='in')
        pm.button(c='weaponCor(side = L)', label='Left Hand Weapon', parent="CreateGrid")
        pm.separator(style='in')
        pm.button(c='noWeapon(side = L)', label='Left Hand No Weapon', parent="CreateGrid")
        pm.separator(style='in')
        
        pm.showWindow(myWindow)

    L = 'L'
    R = 'R'
    nameS = []
    
    
    def nameSp():
        nameSpace = ''
        nameS = []
        CH = pm.ls(sl=True, fl=True)
        if pm.referenceQuery(CH, isNodeReferenced=True):
            nameSpace = CH[0].split(':')[0] + ':'
            nameS.append(nameSpace)
            print nameS[0]
        return nameSp


    def update_UI(self, *_, **__):
        sel = pm.ls(selection=True)[0] or []
        if sel:
            pm.textField(self.ObjClass, e=True, text = CH)


############################
    CH = pm.ls(sl=True, fl=True)

    
    
    def weaponCor(side):
        Target = pm.ls(sl=True, fl=True)[0]
        for name in nameS:
            name = nameS[0]
            print name
            Weapon = name + side + '_Weapon_ctrl'
            print Weapon
    
            # pm.select(clear=True)
            pm.cutKey(Weapon)
            par = pm.parentConstraint([Target] + [Weapon], mo=False)
    
            startTime = pm.playbackOptions(query=True, minTime=True)
            endTime = pm.playbackOptions(query=True, maxTime=True)
    
            pm.bakeResults(Weapon, simulation=True, t=(startTime, endTime))
    
            pm.showHidden(above=True)
    
            pm.delete(par)
    
            return weaponCor
    
    
    def noWeapon(side):
        pm.select(clear=True)
        for name in nameS:
            name = nameS[0]
            src = name + 'Wrist_' + side
            trg = name + side + '_Weapon_ctrl'
    
            print
            src
    
            pm.cutKey(trg)
    
            loc = pm.spaceLocator(n='reference')[0]
    
            pm.parent(loc, src)
            pm.xform(loc, t=(-0.08, 0, 0.035), ro=(0, 90, 180))
    
            # pm.ls('ctrl')
            par = pm.parentConstraint([loc] + [trg], mo=0)
    
            startTime = pm.playbackOptions(query=True, minTime=True)
            endTime = pm.playbackOptions(query=True, maxTime=True)
    
            pm.bakeResults(trg, simulation=True, t=(startTime, endTime))
    
            pm.showHidden(above=True)
    
            pm.delete(par, loc)
    
            return noWeapon
    
    def noRWeapon(side):
        pm.select(clear=True)
        for name in nameS:
            name = nameS[0]
            src = name + 'Wrist_' + side
            trg = name + side + '_Weapon_ctrl'
    
            print
            src
    
            pm.cutKey(trg)
    
            loc = pm.spaceLocator(n='reference')[0]
    
            pm.parent(loc, src)
            pm.xform(loc, t=(0.08, 0, -0.035), ro=(0, 90, 180))
    
            # pm.ls('ctrl')
            par = pm.parentConstraint([loc] + [trg], mo=0)
    
            startTime = pm.playbackOptions(query=True, minTime=True)
            endTime = pm.playbackOptions(query=True, maxTime=True)
    
            pm.bakeResults(trg, simulation=True, t=(startTime, endTime))
    
            pm.showHidden(above=True)
    
            pm.delete(par, loc)
    
            return noWeapon
test = drawUI()

Ashutosh Yadav
  • 333
  • 1
  • 12

2 Answers2

0

figured it out ^^'

    def __init__(self):
    myWindow = pm.window(title="Weapon Position Fix")
    pm.columnLayout("basicTools", adjustableColumn=True)
    
    pm.gridLayout("CreateGrid", numberOfRowsColumns=(1, 1), cellWidthHeight=(180, 30), parent="basicTools")
    
    pm.separator(style='in')
    pm.text(label="Select object")       

    textFld=pm.textField('tFld', editable = False)
    but = pm.button(c='setSel()', l='Set Character', parent="CreateGrid")
    
    pm.separator(style='in')
    pm.button(c='weaponCor(side = R)', label='Right Hand Weapon', parent="CreateGrid")
    pm.separator(style='in')
    pm.button(c='noRWeapon(side = R)', label='Right Hand No Weapon', parent="CreateGrid")
    pm.separator(style='in')
    pm.button(c='weaponCor(side = L)', label='Left Hand Weapon', parent="CreateGrid")
    pm.separator(style='in')
    pm.button(c='noWeapon(side = L)', label='Left Hand No Weapon', parent="CreateGrid")
    pm.separator(style='in')
    
    pm.showWindow(myWindow)

L = 'L'
R = 'R'
nameS = []

def addFirstSel(*args):
 sel = pm.ls(sl=True)
 add = pm.textField('tFld', edit=True, text=sel[0])
 return addFirstSel



def nameSp():
    nameSpace = ''
    nameS = []
    CH = pm.ls(sl=True, fl=True)
    if pm.referenceQuery(CH, isNodeReferenced=True):
        nameSpace = CH[0].split(':')[0] + ':'
        nameS.append(nameSpace)
        print nameS[0]
    return nameSp

def setSel():
    nameSp()
    addFirstSel()
    return setSel
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '23 at 17:37
0

I wonder it works at all. To build ui in maya you should follow a few rules. First rule: Never rely on UI names. You create a gridLayout and name it CreateGrid. In maya you cannot be sure that these names are really used, it may be named CreateGrid1 in some cases. Always use the return value like you have done in the first version with the self.ObjClass class variable. Second rule: Do not use strings as function calls in the command definitions of the buttons. If you use strings these functions have to be globally defined what is not a good idea. Always use class methods. And it seems that you heavily mix class and non class methods what is not a good idea either.It should look more like this (that's not complete as you can see, it's only an example):

class drawUI(object):

    def __init__(self):
        self.names = []
        self.myWindow = pm.window(title="Weapon Position Fix")
        self.cl = pm.columnLayout("basicTools", adjustableColumn=True)
        self.cg = pm.gridLayout("CreateGrid", numberOfRowsColumns=(1, 1), cellWidthHeight=(180, 30), parent=self.cl)        
        pm.separator(style='in')
        pm.text(label="Select object")       
        self.ObjClass = pm.textField(text = 'Press Set Character' ,editable = False)
        
        pm.button(c=self.nameSp , label='Set Character', parent=self.cg)
        pm.separator(style='in')
        pm.button(c = lambda *args: self.weaponCor(side=R), label='Right Hand Weapon', parent=self.cg)
        pm.separator(style='in')
        pm.button(lambda *args: self.noRWeapon(side=R), label='Right Hand No Weapon', parent=self.cg)
        pm.separator(style='in')
        pm.button(c=lambda *args: self.weaponCor(side=L), label='Left Hand Weapon', parent=self.cg)
        pm.separator(style='in')
        pm.button(lambda *args: self.noRWeapon(side=L), label='Left Hand No Weapon', parent=self.cg)
        pm.separator(style='in')        
        pm.showWindow(self.myWindow)

    def nameSp(self):
        nameSpace = ''
        self.names = []
        CH = pm.ls(sl=True, fl=True)
        if pm.referenceQuery(CH, isNodeReferenced=True):
            nameSpace = CH[0].split(':')[0] + ':'
            self.names.append(nameSpace)
            print(nameS[0])
        return nameSp    

    def update_UI(self, *_, **__):
        sel = pm.ls(selection=True)[0] or []
        if sel:
            pm.textField(self.ObjClass, e=True, text = CH)
            
    def weaponCor(self, side):
        Target = pm.ls(sl=True, fl=True)[0]
        ....


    def noWeapon(self, side):
        ....
haggi krey
  • 1,885
  • 1
  • 7
  • 9