Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, April 26, 2011

Selection, Renaming, and Iteration.. Oh My!

Thought i would share some of the handy scripts i have made why making rigs for games. Some of these are short, but have saved me tons of time.

import pymel.core.general as pmg
import pymel.core.windows as pmw
import pymel.core.rendering as pmr
import pymel.core.modeling as pmm
import pymel.core.animation as pma
import pymel.core.system as pms
import maya.cmds as cmds
import maya.mel as mm

This one is handy, will rename some objects and make the last bone in the chain "Nub". This needs some adjustment to be able to handle number padding, but its a goood start.

def renameBones(boneName):
# need to add number padding as a function

numStart = 1
selection = pmg.ls(sl=True)
for obj in selection:
pmg.select(obj, hi=True, r=True)
nubTest = pmg.ls(sl=True)
# Check to see if node has children, if not name it 'Nub'
if len(nubTest) == 1:
nubArray.append(obj)
replaceName = boneName +'Nub'
pmg.rename(str(obj) , replaceName)
continue
replaceName = boneName + '0' + str(numStart)
pmg.rename(str(obj) , replaceName)
numStart += 1

I needed a quick way to select all the joints in a chain, and ignore all the other constraints and nonsense that can get added in there.

def sJoints():
pmg.select(hi=True)
origSelection = pmg.ls(sl=True)
joints = []
for obj in origSelection:
if pmg.nodeType(str(obj)) == 'joint':
joints.append(obj)
pmg.select(joints, r=True)

Iterator:

I found that when creating custom rigs I had to iterate alot through attributes here is a pretty quick solution for doing common things through attributes. Basically making an array out of each and running through them.

for attr in ['t', 'r','s']:
for axis in ['x','y','z']:
pmg.setAttr(str(objName)+'.' + attr + axis , lock=True, keyable=False)

Saturday, March 13, 2010

Renaming Files in a folder

i was reorganizing a bunch of my music and was thinking on some ways to make it go faster. I put together the beginnings of a renaming python script, right now it is searching a given directory and replacing whatever is passed in to it. Pretty simple at the moment but we will see where it goes.

I switched over to python 3.0 today as well, one of the things i noticed off the bat is that print command has changed. Instead of the old : print 'My Text' its now print('My Text')

Took me a little while to figure out why those commands weren't working but seems like its on track now.


import os
import os.path



def findReplace(find , replace, dir):
# dir = r'c:\Temp\\'
# find = '-'
# replace = ''


for file in os.listdir(dir):
print(file)
if find in file:
newName = file.replace( find , replace)
newName = newName.strip(' ')

print('New Name = %s' % newName)
print('File = %s' %file)
print('Directory + filename = %s' % (dir + '\\' +file))

if os.path.isfile(dir + file) == True:
os.rename(dir + file, dir + newName)
print('Full final File Name = ' + file)
def split(index , splitBy, dir):
for file in os.listdir(dir):
if splitBy in file:
splitList = file.split(splitBy)
print(splitList)
newName =splitList[index].lstrip(' ')
print(newName)
if os.path.isfile(dir + file) == True:
os.rename(dir + file, dir + newName)
print('Full final File Name = ' + file)
if __name__ == '__main__':
split(3 , '-' , r '')