Thursday, March 21, 2013

test string type

hi

i have not found a way to create a field in a maya window, that will read the contents as something other than text.
when querying a field it will be returned as unicode, therefore here is a simple way to check if that text can be interpreted as a string, float, integer, or some type of list.

this can be useful for windows or many other cases (python only):

def testStringType(string):
    
    '''
    given a string, test if it can be interpreted as a differen type.
    useful for reading text.
    
    @string = str: string to be tested

    @return = list['this', 'is', 'a', 'tuple']: if string type = this, is, a, tuple
    @return = float(1.0): if string type = 1.0
    @return = int(5): if string type = 5
    @return = str(a123): if string type = a123
    '''
    
    #var is not a list, test type
    def testType(var):
        
        #var contains numbers, test type
        def numTest(n):
            try:
                print 'i am a float!'
                return float(n)
            except:
                print 'no! i am actually a str!'
                return str(n)
        
        #test type                        
        if var.isdigit():
            print 'i am an integer!'
            return int(var)
        elif  entry == 'True' or entry == 'False':
            print 'i am a bool!'
            return bool(entry)  
        elif var.isalpha():
            print 'i am a str!'
            return str(var)
        else:
            print 'i look complicated...'
            return numTest(var)
    
    #test if var is a type of list
    cleanList=[]
    if ',' in var:
        print 'i am a some sort of list!'
        clean = var.replace(' ', '')
        cleanList = clean.split(',')
        return cleanList
    else:
        print 'let\'s see what am i...'
        single = testType(var)
        return single     

because we are assuming that we are reading text, the function checks if the text contains comas ',' and if so it will be converted into a list. lists shall be presented in the form: a,b,c,d,1,2,3. additionally we could remove '(', ')', '[', ']' if necessary.

otherwise, we would use the 'types.ListType' and  'types.TupleType', or 'is list' and 'is tuple', but both would return false whilst reading text.

now in maya, used in a practical example, we can create a window, that given a function will create a UI for it. this function needs to return the exact type read from the window and with the above method it is tested and returned correctly.

import maya.cmds as mc

def jc_makeDefWin(myDef):

    '''
    creates a window to run given function
    enter function name and run
    
    @myDef = function: 
    '''
    
    # inspect my function, get arguments and name    
    funArgs = inspect.getargspec(myDef)
    myArgs = funArgs[0]
    name = myDef.__name__+'_win'
    
    # create window  
    window = mc.window(title=name, iconName=name, widthHeight=(200, 200))
    mc.columnLayout(adjustableColumn=True)
    slots = []
    
    # create slots for function arguments    
    for i in range(len(myArgs)):
        slot = mc.textFieldGrp(label='arg: '+myArgs[i])
        slots.append(slot)
    
    # read input text and run function
    def run(*args):
        
        entries = []    
        
        #read entries
        for i in range(len(slots)):
            entry = mc.textFieldGrp(slots[i], q=True, text=True)
            
            # func test data type and pass correct (str, int, float)
            def testType(entry):
                            
                def numTest(n):
                    try:
                        return float(n)                        
                    except:
                        return str(n)
                                
                if entry.isdigit():
                    return int(entry)
                elif  entry == 'True' or entry == 'False':
                    return bool(entry)
                elif entry.isalpha():
                    return str(entry)
                else:
                   return numTest(entry)
            
            #test if entry is a tuple, add as tuple or type
            if ',' in entry:
                clean = entry.replace(' ', '')
                cleanList = clean.split(',')
                entries.append(cleanList)
            else:
                entry = testType(entry)  
                entries.append(entry)
 
        myDef(*entries)
                
    # run button, calls run def    
    mc.button(label='Run', command=run)
    mc.button(label='Close', command=('maya.cmds.deleteUI(\"' + window + '\", window=True)'))
    mc.setParent('..')
    mc.showWindow(window)

    mm.eval("print \"type jc_makeDefWin(your function!)\"")

python variables into mel.eval

hi

this is rather simple, but not necessarily easy to find.

when calling mel commands you might want to pass variables declared in python, without having to do so in the mel environment first.

the documentation suggests that you might be working on both and might to communicate in-between them (and you can do so only via global variables). in activities such as mine, this is rarely the case.
using the eval function is 99% of the time given by the need to call a mel only command or script not available in python.

to solve this one can use the python() function when opening a "mel statement" via eval, one can call python again.
 
import maya.cmds as mc
import maya.mel as mm   
collideMainGrp = mc.group(n= 'ColisMain_grp', em=1)  
kONode, kOShape, kODrivenGrp = mm.eval('cMuscle_rigKeepOut(python("collideMainGrp"))')

easy! in this case cMuscle_rigKeepOut uses a python variable, instead of having to do stuff like using selection based commands such as cMuscle_rigKeepOutSel whilst selecting stuff in your script, which is a rather unclean alternative.

unfortunately because of the explained above, mel will not read the variable unless it is global. therefore if one where to pass it to a function: 

loc = 'locator1'
makeKeepOut(loc)

def makeKeepOut(obj):
    global keepOut
    keepOut = obj
    kONode, kOShape, kODrivenGrp = mm.eval('cMuscle_rigKeepOut(python("keepOut"))')
    del keepOut
    return kONode, kOShape, kODrivenGrp

this as you can see is not the cleanest way to call mel commands... but! thanks to python formatting we can avoid the variable smuggling and we can just pass stuff as strings. this will not only make for a cleaner and more pythonic code, but shorter too.

we can either use the old %s formatting or str.format(), like so:

collideMainGrp = mc.group(n= 'ColisMain_grp', em=1)

mm.eval('cMuscle_rigKeepOut("{0}")'.format(collideMainGrp))
# or 
#mm.eval('cMuscle_rigKeepOut("%s")' % collideMainGrp )

when in a function:

def makeKeepOut(obj):

    kONode, kOShape, kODrivenGrp = mm.eval('cMuscle_rigKeepOut("%s")' % obj)
    # or
    #kONode, kOShape, kODrivenGrp = mm.eval('cMuscle_rigKeepOut("{0}")'.format(obj))

    return kONode, kOShape, kODrivenGrp

no need for globals or other fluff... neat and easier to read. (cheers to jakub for suggesting a couple of things)

cMuscle_rigKeepOut, by the way is a maya muscle un-documented command. you can find it in /***/autodesk/maya2012sp1/scripts/muscle/cMuscle.mel , it creates a collision node with a collision vector. needs a muscle object to interact with. handy stuff!

for more information on python formatting and the old %.

regards