import maya.cmds as cmds
import maya.mel as mel
def getTheNurbsCurve():
source_curve = cmds.ls(sl=True, dag=True)
if not source_curve:
cmds.warning('No curve was selected')
return
else:
source_curve = source_curve[0]
# Check if the curve has "attractionProfile"
if not cmds.objExists(source_curve + '.attractionProfile'):
cmds.warning('{} is missing "attractionProfile"'.format(source_curve))
source_curve = None
return source_curve
def currentKeyChanged(currentData):
currentIndex = cmds.gradientControlNoAttr('attractionProfile', q = True, currentKey = True)
if currentIndex == -1:
return
# Get the values for that key
currentData = cmds.gradientControlNoAttr('attractionProfile', q = True, asString = True)
if not currentData:
return
a = currentData.split(',')
posVal = float(a[currentIndex*3])
valVal = float(a[currentIndex*3+1])
interp = int(a[currentIndex*3+2])
# Update the two floatFields
cmds.floatFieldGrp('posFieldRadius', e = True, v =[posVal]*4)
cmds.floatFieldGrp('valFieldRadius', e = True, v =[valVal]*4)
interpStrings = [' None ',' Linear ',' Smooth ',' Spline ']
print interpStrings[interp]
cmds.optionMenuGrp('interpolationMenu', e=True, value = interpStrings[interp])
def posModified(val):
# Change the position of the selected key
currentIndex = cmds.gradientControlNoAttr('attractionProfile', q = True, currentKey = True)
if currentIndex == -1:
return
currentData = cmds.gradientControlNoAttr('attractionProfile', q = True, asString = True)
a = currentData.split(',')
a[currentIndex*3+1] = str(val)
newData = ','.join(a)
currentData = cmds.gradientControlNoAttr('attractionProfile', e = True, asString = newData)
def valModified(val):
# Change the position of the selected key
currentIndex = cmds.gradientControlNoAttr('attractionProfile', q = True, currentKey = True)
if currentIndex == -1:
return
currentData = cmds.gradientControlNoAttr('attractionProfile', q = True, asString = True)
a = currentData.split(',')
a[currentIndex*3] = str(val)
newData = ','.join(a)
currentData = cmds.gradientControlNoAttr('attractionProfile', e = True, asString = newData)
def interpModified(val):
# Change the position of the selected key
currentIndex = cmds.gradientControlNoAttr('attractionProfile', q = True, currentKey = True)
if currentIndex == -1:
return
currentData = cmds.gradientControlNoAttr('attractionProfile', q = True, asString = True)
a = currentData.split(',')
interpStrings = [' None ',' Linear ',' Smooth ',' Spline ']
val = interpStrings.index(val)
a[currentIndex*3+2] = str(val)
newData = ','.join(a)
currentData = cmds.gradientControlNoAttr('attractionProfile', e = True, asString = newData)
def setUpRamp():
# This function will make the ramp in the UI match the existing values of the ramp curve
source_curve = getTheNurbsCurve()
attributeData = []
for i in cmds.getAttr(source_curve + ".attractionProfile", mi = True) or []:
attributeData.append(cmds.getAttr("{}.attractionProfile[{}].attractionProfile_FloatValue".format(source_curve, i)))
attributeData.append(cmds.getAttr("{}.attractionProfile[{}].attractionProfile_Position".format(source_curve, i)))
attributeData.append(cmds.getAttr("{}.attractionProfile[{}].attractionProfile_Interp".format(source_curve, i)))
# Set the data
currentData = cmds.gradientControlNoAttr('attractionProfile', e = True, asString = str(attributeData))
def rampModified(gradient_data):
# Convert the gradient_data from a unicode (string) to a list
gradient_data = gradient_data.split(',')# Split the string up
# We will convert each element from a string to a float or int
data_list = []
for i,s in enumerate(gradient_data):
# If the elements index is a multiple of 3
# we want it to be an int
# since every third element represents the interpolation
# we check for this by using modular arithmetic (the % sysmbol)
if (i+1)%3 == 0:
data_list.append(float(s))
else:
data_list.append(float(s))
# Then we will split up the data into chunks of 3
# so instead of having 1 list of data, we have a list of lists
# where each element contains all the data for a specific cv
print (type(len(data_list)/3))
attraction_profile = []
for i in range(int(len(data_list)/3)):
attraction_profile.append(data_list[i*3:(i*3)+3])
source_curve=getTheNurbsCurve()
# Do whatever it is you were doing with that data
# THIS WILL AFFECT ALL THE CURVES NOT JUST THE ONE YOU
# SELECTED WHEN YOU LAUNCHED THE WINDOW
for eachCurve in (cmds.ls(sl=True, dag=True) or []) + [source_curve]:
for i in cmds.getAttr(eachCurve + ".attractionProfile", mi = True) or []:
cmds.removeMultiInstance("{}.attractionProfile[{}]".format(eachCurve, i), b=True)
for i, (pos, value, interp) in enumerate(attraction_profile):
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_Position".format(eachCurve, i), value)
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_FloatValue".format(eachCurve, i), pos)
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_Interp".format(eachCurve, i), interp)
def clear():
# To do the clear we need to edit the attribute not the ui
# since gradientControl keys can't be edited via command
source_curve=getTheNurbsCurve()
for eachCurve in (cmds.ls(sl=True, dag=True, typ="nurbsCurve") or []) + [source_curve]:
# Remove all the data
for i in cmds.getAttr(eachCurve + ".attractionProfile", mi = True) or []:
cmds.removeMultiInstance("{}.attractionProfile[{}]".format(eachCurve, i), b=True)
# Add the two standard keys back in with linear interpolation
attraction_profiled = [[0,1,1],[1,1,1]]
for i, (pos, value, interp) in enumerate(attraction_profile):
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_Position".format(eachCurve, i), value)
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_FloatValue".format(eachCurve, i), pos)
cmds.setAttr("{}.attractionProfile[{}].attractionProfile_Interp".format(eachCurve, i), interp)
def set__key():
source_curve = getTheNurbsCurve()
for i in cmds.getAttr(source_curve + '.attractionProfile', mi = True):
cmds.setKeyframe(source_curve + '.attractionProfile[{}].attractionProfile_Position'.format(i))
cmds.setKeyframe(source_curve + '.attractionProfile[{}].attractionProfile_FloatValue'.format(i))
cmds.setKeyframe(source_curve + '.attractionProfile[{}].attractionProfile_Interp'.format(i))
if cmds.window("mayaTool", exists=True):
cmds.deleteUI("mayaTool")
cmds.window("mayaTool", t="mayaTool",
w=460, h=500, sizeable=True, mnb=True, mxb=False ,menuBar=True)
###
cmds.scrollLayout('scrollLayout')
cmds.gradientControlNoAttr('attractionProfile', w=460, h = 120,ck=0,changeCommand = rampModified,)
cmds.rowLayout(nc=2)
cmds.button("reset", w=230, h=30, c="clear()", bgc=(0.4, 0.2, 0.3))
cmds.button("Set key", w=230, h=30, c="set__key()", bgc=(1, 0.05, 0))
cmds.setParent( '..' )
cmds.separator(h=5)
# Add the other widgets
cmds.rowLayout(nc=3)
cmds.floatFieldGrp('posFieldRadius', pre=3, l="Selected Position", nf=1, v1=0.000, s=1.0, cw=(1, 70), changeCommand = posModified)
cmds.floatFieldGrp('valFieldRadius', pre=3, l="Selected Value", nf=1, v1=1.000, s=1.0, cw=(1, 70), changeCommand = valModified)
cmds.gradientControlNoAttr('attractionProfile', e = True,currentKeyChanged = currentKeyChanged)
cmds.optionMenuGrp('interpolationMenu', cw=(1, 70), l="Interpolation",changeCommand = interpModified)
cmds.menuItem(l=" None ")
cmds.menuItem(l=" Linear ")
cmds.menuItem(l=" Smooth ")
cmds.menuItem(l=" Spline ")
cmds.setParent( '..' )
cmds.showWindow("mayaTool")
setUpRamp()
The interp works now, and I fixed the issue where you were no longer loading the existing ramp from the selected curve.