【笔记】【Maya工具】Python For Maya(Cmds)(三)The Animation Tweener

一、UI library

  • QT

    • maya的UI就是用QT建立的
  • PySide

image-20220630000053014

选取某一帧进行百分比插值

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
from maya import cmds

def tween(percentage, obj = None, attrs = None, selection = True):
if not obj and not selection:
raise ValueError("No object given to tween")
if not obj:
obj = cmds.ls(selection = True)[0]
if not attrs:
attrs = cmds.listAttr(obj,keyable = True)
currentTime = cmds.currentTime(query = True)

for attr in attrs:
# construct the full name of the attr with its obj
attrFull = '{}.{}'.format(obj,attr)
# get the keyframes of the attr on this obj
keyframes = cmds.keyframe(attrFull, query = True)

if not keyframes:
continue
previousKeyframes = [frame for frame in keyframes if frame < currentTime]

laterKeyframes = [frame for frame in keyframes if frame> currentTime]
if not previousKeyframes and not laterKeyframes:
continue


if previousKeyframes:
previousFrame = max(previousKeyframes)
else:
previousFrame = None
nextFrame = min(laterKeyframes) if laterKeyframes else None

if not previousFrame or not nextFrame:
continue
previousValue = cmds.getAttr(attrFull, time = previousFrame)
nextValue = cmds.getAttr(attrFull, time = nextFrame)

difference = nextValue - previousValue
weightedDifference = (difference*percentage) / 100.0
currentValue = previousValue + weightedDifference
print(previousValue)
print(nextValue)
print(currentValue)
cmds.setKeyframe(attrFull, time = currentTime, value = currentValue)

tween(50)



二、创建窗口

1
2
3
4
5
6
7
8
9
10
11
import maya.cmds as cmds

# Make a new window
#
window = cmds.window( title="Long Name", iconName='Short Name', widthHeight=(200, 55) )
cmds.columnLayout( adjustableColumn=True )
cmds.button( label='Do Nothing' )
cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )
cmds.setParent( '..' )
cmds.showWindow( window )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class TweenWindow(object):
windowName = "TweenerWindow"
def show(self):
if cmds.window(self.windowName, query = True, exists = True):
cmds.deleteUI(self.windowName)
cmds.window(self.windowName)
self.buildUI()
cmds.showWindow()
def buildUI(self):
column = cmds.colunmLayout()
cmds.text(label = "Use this slider to set the tween amout")
row = cmds.rowLayout(numberOfColumns = 2)
self.slider = cmds.floatSlider(min = 0, max = 100, value = 50,step = 1,changeCommand = tween)
cmds.button(labe = "Reset",command = self.reset)

cmds.setParent(column)
cmds.button(label = "Close", command = self.close)
def reset(self,*args):#button默认传递参数,但不重要
print("resetting UI")
cmds.floatSlider(self.slider, edit = True, value = 50)
def close(self,*args):
cmds.deleteUI(self.windowName)

三、Base UI for different function

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
import tween
import Gear
from maya import cmds
class BaseWindow(object):
windowName = "BaseWindow"
def show(self):
if cmds.window(self.windowName, query = True, exists = True):
cmds.deleteUI(self.windowName)
cmds.window(self.windowName)
self.buildUI()
cmds.showWindow()
def buildUI(self):
pass
def reset(self,*args):#button默认传递参数,但不重要
pass
def close(self,*args):
cmds.deleteUI(self.windowName)

class TweenerUI(BaseWindow):#继承basewindow基类
windowName = "TweenerWindow"
def buildUI(self):
column = cmds.colunmLayout()
cmds.text(label = "Use this slider to set the tween amout")
row = cmds.rowLayout(numberOfColumns = 2)
self.slider = cmds.floatSlider(min = 0, max = 100, value = 50,step = 1,changeCommand = tween)
cmds.button(labe = "Reset",command = self.reset)

cmds.setParent(column)
cmds.button(label = "Close", command = self.close)
def reset(self,*args):#button默认传递参数,但不重要
print("resetting UI")
cmds.floatSlider(self.slider, edit = True, value = 50)

class GearUI(BaseWindow):
windowName = "GearWindow"
def __init__(self):
self.gear = None
def buildUI(self):
column = cmds.columnLayout()
cmds.text(label = "Use the silider to modify the gear")
cmds.rowLayout(numberOfColumns = 4)
self.label = cmds.text(label = "10")
self.slider = cmds.intSlider(min = 5, max = 30, value = 10,step = 1, dragCommand = self.modifyGear)
cmds.button(label = "Make Gear", command = self.makeGear)
cmds.button(label = "Reset",command = self.reset)
cmds.setParent(column)
cmds.button(label = "Close",command = self.close)
def makeGear(self,*args):
teeth = cmds.intSlider(self.slider,query = True, value = True)
self.gear = Gear()
self.gear.createGear(teeth = teeth)

def modifyGear(self,teeth):
if self.gear:
self.gear.changeTeeth(teeth = teeth)
cmds.text(self.label,edit = True, label = teeth
def reset(self,*args):
self.gear = None
cmds.intSlider(self.slider,edit = True,value = 10)
cmds.text(self.label,eidt = True,label = '10')
GearUI().show()

四、Add script to maya shelf

image-20220701001501311

直接选中脚本代码,然后拖到上面,就出现了一个按钮,一点按钮,自动运行,右键可以修改