我正在尝试使用以下代码将xsz值从csv文件导入maya中的某些对象:
import maya.cmds as cmds
def getDataByFrame(fileName):
# Open and read lines from some csv file
data = fileName
f = open(data)
line = f.readline()
dataPerFrame = []
original, object, x, y, z = line.split(",")
while line:
frame, object, x, y, z = line.split(",")
if frame != original:
print dataPerFrame
return dataPerFrame
#print frame
#print x
#print y
#print z
else:
dataPerFrame.append(line)
line = f.readline()
f.close()
def animate(data):
for i in range (len(data)):
print "dada"
frame, objectName, x, y, z = data[i].split(",")
print frame
print objectName
print x
print y
print z
frame = float(frame)
x = float(x)
y = float(y)
z = float(z)
**cmds.currentTime(frame)
cmds.setAttr (objectName+".tx", x)
cmds.setKeyframe( v=x, at='translateX' )
cmds.setAttr (objectName+".ty", y)
cmds.setKeyframe( v=y, at='translateY' )
cmds.setAttr (objectName+".tz", z)
cmds.setKeyframe( v=z, at='translateZ' )**
data = getDataByFrame("C:/Users/User/Desktop/Stickman/testing.csv")
animate(data)
问题是我收到错误:"错误检索默认参数" 突出显示的代码是从我在官方maya帮助网站上找到的mel代码翻译而来的:
global proc getAnim(string $fileName, string $objectName)
{
//open the file for reading
$fileId=`fopen $fileName "r"`;
//get the first line of text
string $nextLine = `fgetline $fileId`;
//while $nextline is not emtpy(end of file) do the following
while ( size( $nextLine ) > 0 ) {
//tokenize(split) line into separate elements of an array
string $rawAnimArray[];
tokenize ($nextLine, " ",$rawAnimArray);
//place each element of the array into separate variables
print $rawAnimArray;
float $frame=$rawAnimArray[0];
float $x=$rawAnimArray[1];
float $y=$rawAnimArray[2];
float $z=$rawAnimArray[3];
//change the currentTime and set keys for tx, ty, tz
currentTime $frame ;
setAttr ($objectName+".tx") $x;
setKeyframe ($objectName+".tx");
setAttr ($objectName+".ty") $y;
setKeyframe ($objectName+".ty");
setAttr ($objectName+".tz") $z;
setKeyframe ($objectName+".tz");
//get the next line in the ascii file.
$nextLine = `fgetline $fileId`;
}
//close file
fclose $fileId;
}
我不确定是不是因为我没有将这行代码正确地翻译成Python,或者是因为其他原因。
答案 0 :(得分:0)
只看你的代码,这里有两个错误:
第47行:**cmds.currentTime(frame)
- > cmds.currentTime(frame)
第53行:cmds.setKeyframe( v=z, at='translateZ' )**
- > cmds.setKeyframe(objectName, v=z, at='translateZ' )
对于动画,我会做这样的事情。如果要在csv中添加旋转和缩放,则第一个attr和attr轴就是示例。
def animate(data):
for d in data:
visibility = []
frame, objectName = d.split(",")[:2]
values = [float(i) for i in d.split(",")[2:]]
# just for the fun to see if there is a visibility value
guess = [len(values)/3, len(values)%3
if guess[1]:
visibility = ('v', values[0])
values = values[1:]
attr = ['t','r', 's'][:guess[0]/3]
attr_axes = [i+j for i in attr for j in ['x', 'y', 'z']]
feed = zip(attr_axes, values)
if visibility:
feed += visibility
for d in feed:
cmds.setKeyframe(objectName, v=d[1], at=d[0], t=frame)
data = ["10, pSphere1, 1,2,3", "20, pSphere1, 5,6,7"]
# EXAMPLE : data = [ "10, pSphere1, 1, 1, 2, 3, 4,5 , 6, 1.1, 1.2, 1.3"]
# EXAMPLE : data = [frame, obj, visibility, tx, ty, tz, rx, ry, rz, sx, sy, sz]
animate(data)
如果您只想要必要的代码:
def animate(data):
for d in data:
frame, objectName = d.split(",")[:2]
values = [float(i) for i in d.split(",")[2:]]
attr = ['t']
attr_axes = [i+j for i in attr for j in ['x', 'y', 'z']]
feed = zip(attr_axes, values) # + visibility
for d in feed:
cmds.setKeyframe(objectName, v=d[1], at=d[0], t=frame)
data = ["10, pSphere1, 1,2,3", "20, pSphere1, 5,6,7"]
animate(data)
--- --- EDIT
最后的提示,如果你想编辑大量的对象,我会用相同的值重新组合对象和属性,因为它可能非常慢,否则