我试图使用以下代码将XYZ值从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,或者是因为其他内容。
仅查看您的代码,这里有两个错误:
行47:**cmds.currentTime(frame)
-> cmds.currentTime(frame)
第53行:cmds.setKeyframe( v=z, at='translateZ' )**
-> cmds.setKeyframe(objectName, v=z, at='translateZ' )
对于动画,我会做这样的事情。如果要添加旋转并扩展到CSV。
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)
---编辑---
最后提示,如果您想编辑很多对象,我将重新组合对象和具有相同值的属性,因为它确实可以很慢,否则