Maya变量连接

时间:2013-01-13 11:03:57

标签: python maya

基本上插入一堆由循环生成的标志有一些问题:

def drawHelix(radius, length, coils): 
    numPoints = int(8)
    degrees = float((360 / numPoints))
    centerX = float(0)
    centerZ = float(0)
    xLoc = float(0)
    zLoc  = float(0)
    yLoc  = float(0)
    yOffset = float(((length / coils) / numPoints))
    vectorIndex = int(0)
    pointStr = ""
    knotStr = ""
    for i in range(1, (360 * coils), 20):
        t = i + degrees
        xLoc = centerX + (math.cos(t) * radius)
        zLoc = centerZ - (math.sin(t) * radius)
        pointStr = (pointStr + " p=(" + str(xLoc) + "," +  str(yLoc) + "," +  str(zLoc) + "),")
        knotStr = (knotStr + "k=(" + str(vectorIndex) + ")")
        vectorIndex = i + 1
        yLoc = yLoc + yOffset
    print pointStr        
    spiral = cmds.curve(d=float(1.0), pointStr, knotStr)
    cmds.rebuildCurve (spiral, ch=1, rpo=1, rt=0, end=1, kr=1, kcp=0, kep=0, kt=0, s=0, d=3, tol=0.001)
    return spiral

然后我运行:drawHelix (2.00, 3.00, 5.00)

问题在于Maya不会将“pointStr”识别为curve命令的标志,当我打印pointStr时它确实给了我我想要的东西,但却在努力实现如何实现这个功能!

2 个答案:

答案 0 :(得分:2)

Python解释器在调用函数之前不会扩展你的字符串(你可以使用eval来实现这一点,但这通常被认为是不好的做法 - 请参阅SO上的this post

将参数作为关键字词典传递时应该有效。 在这里查看:

所以而不是:

pointStr = (pointStr + " p=(" + str(xLoc) + "," +  str(yLoc) + "," +  str(zLoc) + "),")
knotStr = (knotStr + "k=(" + str(vectorIndex) + ")")

你应该做

kwargs['d'] = 1.0
kwargs['p'] = []
for i in range(1, (360 * coils), 20):
    ...
    kwargs['p'].append((xloc, yloc, zloc))
    kwargs['k'].append(vectorIndex)

spiral = cmds.curve(**kwargs) 

除此之外,您的代码中还有一些其他问题:

float((360 / numPoints))将在Python2.x和Python3.x中进行不同的评估。这就是2.x:

中发生的情况
In [5]: float(7 / 6)
Out[5]: 1.0

In [6]: 7. / 6
Out[6]: 1.1666666666666667

In如果您想确保在您的案例中执行浮点除法,请使用degrees = 360. / numPoints。 这段代码中的潜在影响更严重:yOffset = float(((length / coils) / numPoints))

您可以通过使用或不使用小数点来编写floatint常量。无需将它们打包到float()int()

答案 1 :(得分:1)

我认为这就是你的想法:

from maya import cmds
import math
def drawHelix(radius, length, coils): 
    numPoints = int(8)
    degrees = float((360 / numPoints))
    centerX = float(0)
    centerZ = float(0)
    xLoc = float(0)
    zLoc  = float(0)
    yLoc  = float(0)
    yOffset = float(((length / float(coils)) / float(numPoints)))
    vectorIndex = int(0)
    pointStr = []
    knotStr = []
    yLoc = 0
    for i in range(1, (360 * coils), 20):
        t = i + degrees
        xLoc = centerX + (math.cos(t) * radius)
        zLoc = centerZ - (math.sin(t) * radius)
        pointStr.append((xLoc, yLoc,zLoc))
        knotStr.append(vectorIndex)
        vectorIndex = i + 1
        yLoc = yLoc + yOffset
    print pointStr        
    spiral = cmds.curve(p= pointStr, k=knotStr,d=float(1.0))
    cmds.rebuildCurve (spiral, ch=1, rpo=1, 
                       rt=0, end=1, kr=1, kcp=0, kep=0, 
                       kt=0, s=0, d=3, tol=0.001)
    return spiral

有一种更好的方法可以做到这一点。这就是你应该如何使用Maya,使用节点来构建你的东西​​。所以这里是一个不必要的评论和冗长的版本:

from maya import cmds

def getHistoryShape(name):
    history = cmds.listHistory(name)
    filteredShape = cmds.ls(history, shapes=1)[0]
    return filteredShape 

def drawHelix(radius, length, coils): 
    cyl = cmds.cylinder( ch=True, radius=radius, ax=(0,1,0),
                         hr=float(length)/float(radius) )

    # build a curve on the cylinders surface
    crv = cmds.curveOnSurface(cyl[0], d=1, 
                              uv=[(0,0),(length, coils*4)],
                              k=[0,1]) 
    # make a duplicate that is visible 
    crv = cmds.duplicateCurve(ch=1, rn=0, local=1)

    # tell maya to ignore the cylinder as a piece of geometry
    shape = getHistoryShape(cyl[0])
    cmds.setAttr(shape+'.intermediateObject', 1)

    cmds.rename(cyl[0],'helix1')
    return crv  

现在您可以稍后更改螺旋参数,直播。您可以为用户显示参数radius,length和coil,以便对它们进行动画处理。例如,请参阅Maya工厂脚本。