Thursday, March 21, 2013

test string type


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):
                print 'i am a float!'
                return float(n)
                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)
            print 'i look complicated...'
            return numTest(var)
    #test if var is a type of list
    if ',' in var:
        print 'i am a some sort of list!'
        clean = var.replace(' ', '')
        cleanList = clean.split(',')
        return cleanList
        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))
    slots = []
    # create slots for function arguments    
    for i in range(len(myArgs)):
        slot = mc.textFieldGrp(label='arg: '+myArgs[i])
    # 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):
                        return float(n)                        
                        return str(n)
                if entry.isdigit():
                    return int(entry)
                elif  entry == 'True' or entry == 'False':
                    return bool(entry)
                elif entry.isalpha():
                    return str(entry)
                   return numTest(entry)
            #test if entry is a tuple, add as tuple or type
            if ',' in entry:
                clean = entry.replace(' ', '')
                cleanList = clean.split(',')
                entry = testType(entry)  
    # run button, calls run def    
    mc.button(label='Run', command=run)
    mc.button(label='Close', command=('maya.cmds.deleteUI(\"' + window + '\", window=True)'))

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

No comments: