Skip to content

Instantly share code, notes, and snippets.

@johnpneumann
Created April 5, 2015 03:07
Show Gist options
  • Save johnpneumann/0e2c0de92ea46a5a1194 to your computer and use it in GitHub Desktop.
Save johnpneumann/0e2c0de92ea46a5a1194 to your computer and use it in GitHub Desktop.
Control Curve Deconstructor
"""
Control Curve Deconstructor
Original file creation date: 2010-09-07
Concept and scripting by John P. Neumann (john@animateshmanimate.com) http://animateshmanimate.com
This work is licensed under the 3-clause BSD ("New BSD License") license.
This is one of the most permissive licenses available, next to the MIT license.
In other words: Use it, Share it, Make it better, Give credit where it's due. Done.
Changelog:
0.3
* Optimization of code (with stuff being pointed out by Eric E. Lenerville {http://eeltechart.com})
* Now you only need to pass the curve name and it'll get all the necessary data
* Now closes the curve properly because of how we're getting the data
0.2
* Public Release
0.1
* Initial Creation
"""
import maya.cmds as cmds
import os
def getCVList(crvName):
# get the home directory
homeDir = os.getenv('USERPROFILE') or os.getenv('HOME')
# make the homeDir variable sane on windows
homeDir = homeDir.replace('\\', '/')
# output it to the desktop
homeDir = homeDir + '/Desktop/'
# open our curveData temporary file
curveDataTemp = open(homeDir+'curveData.txt', 'w')
# open our outputFile
outFile = open(homeDir+crvName+'.py', 'w')
# go through the cv's and get the data
cvData = cmds.getAttr(crvName+'.cv[*]')
print >> curveDataTemp, cvData
# close our temp file
curveDataTemp.close()
# get the shape node
shapeNode = cmds.listRelatives(crvName, s=True)
shapeNode = shapeNode[0]
# get the spans and the degree
spans = cmds.getAttr(shapeNode + '.spans')
degree = cmds.getAttr(shapeNode + '.degree')
# get the number of keys to pass to the next part
numKeys = spans / degree + 2
keyDataTemp = open(homeDir+'keyData.txt', 'w')
# define a new list
newList = []
# iterate through it and print it out so it's nice and close the file
for i in range(numKeys):
# check the degree to add to the list properly
if degree == 1:
newList.append(i)
elif degree == 2:
newList.append(i)
newList.append(i)
elif degree == 3:
newList.append(i)
newList.append(i)
newList.append(i)
# output the list
print >> keyDataTemp, newList
keyDataTemp.close()
# open the curveData file and read it into memory - this could be bad for large files though
curveDataTemp = open(homeDir+'curveData.txt', 'r')
curveData = curveDataTemp.read()
curveDataTemp.close()
# import maya cmds in the file just so it's easier
outFile.write('import maya.cmds as cmds\n')
# write the beginning of the curve creation
outFile.write('cmds.curve(d='+str(degree)+', p=')
# get rid of whitespace at the end
curveData = curveData.rstrip()
# write it all back out to the final file
outFile.write(curveData)
# open the key data and read it into memory
keyDataTemp = open(homeDir+'keyData.txt', 'r')
keyData = keyDataTemp.read()
keyDataTemp.close()
# append the key part
outFile.write(', k=')
# get rid of whitespace at the end
keyData = keyData.rstrip()
outFile.write(keyData)
# and name the curve
outFile.write(', name=\''+crvName+'_new\')')
# close all of our files now
keyDataTemp.close()
curveDataTemp.close()
outFile.close()
# and delete our temporary files
os.remove(homeDir+'curveData.txt')
os.remove(homeDir+'keyData.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment