Monday, September 10, 2012

cdReorderAttributes

0 comments
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()

Thursday, September 6, 2012

Sliding Joints

0 comments
Here's a demo of python script I wrote to make joints slide on surfaces. On the demo, there's a hidden nurbs sphere that the joints are sliding on. It does keep the orientation of the joint, but I just didn't orient my joints when I did the demo.

Wednesday, September 5, 2012

sceneFileContents

0 comments
     I had the idea to write a python script that exported all your file contents neatly into a text file. I wrote some of it, but the important parts were written by old chocolate milk Dan Bradham. Just replace the directory with where you want the text file to be written.

import maya.cmds as cmds
import os

def sceneFileContents(filePath):
    '''
    Creates a text file that lists all your nodes in order of node types.
    It puts it in your shot maya directory as a text file.
    '''
    fileName = cmds.file(query = True, sn = True) 
    nodes = cmds.ls(showType = True)
    size = len(nodes)/2
    sceneContents = ["%s:\n\nThere are %i objects in this scene.\n" % (fileName,size)]

    #Build a Dictionary
    node_dictionary = {}
    for i in range(0, len(nodes), 2):
          
        node = nodes[i]
        nodeType = nodes[i+1]
        
        if not nodeType in node_dictionary:
            #If nodeType is not in dictionary, assign node as a list item
            node_dictionary[nodeType] = [node]
        
        else:
            #If nodeType is in dictionary, append node to the existing list
            node_dictionary[nodeType].append(node)

    for k in node_dictionary:
        sceneContents.append ('\nNode Type : %s (%s Nodes)\n' %(k,len(node_dictionary[k])))
        for i, each in enumerate(node_dictionary[k]):
            sceneContents.append  ('    %d:%s\n' % (i, each))

    sceneTxt = open(r'%ssceneContent.txt'%filePath, 'w')    
    sceneTxt.writelines(sceneContents)
    sceneTxt.close()

if __name__ == '__main__':
    sceneFileContents('/USERS/YourName/Desktop/')