Monday, September 10, 2012

cdReorderAttributes

I wrote this script today just because maya has never implemented the feature of moving your user defined attributes. Basically you can do this by hand by deleting an attribute and undoing it. Do make sure you have undo's enabled and have the object selected/have an object with user defined attributes. Just run the script in the python script editor and move attributes in the list in the order you want.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import maya.cmds as mc

def cdReorderAttributes():
    if mc.window('cdReOrderAttributes', exists = True):
     mc.deleteUI('cdReOrderAttributes')
    
    #Get the selected nurbs surface and joints, otherwise print an error. 
    try:
     obj = mc.ls(selection= True)[0]
    except:
     raise RuntimeError('Please select an object to load attributes.')         
    
    attrs = mc.listAttr(obj, userDefined = True)
    
    if attrs:
        if len(attrs) < 2:
            raise RuntimeError('Not enough attributes to reorder.')   
    
    else:
        raise RuntimeError('Not enough attributes to reorder.')   
         
    win = mc.window('cdReOrderAttributes', title= 'cdReOrderAttributes', sizeable = True)
    
    mc.columnLayout('attrLayout', adjustableColumn = True, rowSpacing = 5)
    mc.rowColumnLayout('attrRowLayout', numberOfColumns = 2, columnWidth = [(1, 120), (2,50)])
    
    mc.columnLayout('attrLayout', adjustableColumn = True, rowSpacing = 5)
    mc.textScrollList('attrScroll', allowMultiSelection=True, append = attrs, selectItem= attrs[0])    
    mc.setParent('..')    
    
    mc.columnLayout('buttonLayout', adjustableColumn = True, rowSpacing = 5)
    mc.symbolButton(image = 'trackUp.png',  height =  90, command = 'moveAttrUp()')
    mc.symbolButton(image =  'trackDown.png', height =  90, command = 'moveAttrDown()')
    mc.setParent('..')    
    mc.setParent('..')    
    
    mc.columnLayout('buttonLayout', adjustableColumn = True, rowSpacing = 5)
    mc.button('Reorder Attributes', command = 'reorderAttr("%s")' %obj, backgroundColor = (0,.2,.2))
    mc.setParent('..')    
    
    mc.window(win, edit= True, width = 100, height = 200)
    mc.showWindow(win)


def moveAttrUp():
    attrs = mc.textScrollList( 'attrScroll', query = True, ai = True)
    attrSel = mc.textScrollList( 'attrScroll', query = True, selectItem = True)[0]
    
    a = attrs.index(attrSel)
    attrs[a-1], attrs[a] = attrs[a], attrs[a-1]
            
    mc.textScrollList('attrScroll', e = True, ra = True)
    mc.textScrollList('attrScroll', e = True, append = attrs, selectItem = attrSel)
            
def moveAttrDown():
    
    attrs = mc.textScrollList( 'attrScroll', query = True, ai = True)
    attrSel = mc.textScrollList( 'attrScroll', query = True, selectItem = True)[0]
    
    a = attrs.index(attrSel)
    
    if a == (len(attrs) - 1):
        attrs[0], attrs[a] = attrs[a], attrs[0]    
        
    else:        
        attrs[a+1], attrs[a] = attrs[a], attrs[a+1]
            
    mc.textScrollList('attrScroll', e = True, ra = True)
    mc.textScrollList('attrScroll', e = True, append = attrs, selectItem = attrSel)    

def reorderAttr(obj):
    attrs = mc.textScrollList( 'attrScroll', query = True, ai = True)
    for attr in attrs:
        mc.undo(mc.deleteAttr(obj, attribute = attr))
 
       
cdReorderAttributes()

0 comments:

Post a Comment