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)

问题是玛雅不承认"点str"作为曲线命令的标志,当我打印点str时,它确实给了我我想要的,但在如何实际使这个工作上挣扎!

Python解释器在调用函数之前不会展开你的字符串(你可以使用eval实现这一点,但这通常被认为是不好的做法——参见SO的这篇文章)。

在将参数作为关键字字典传递时应该可以工作。在这里查找:

    在Python官方教程中定义函数的更多内容
  • PEP 3102仅关键字参数
  • 5.3.4 Python参考手册中的调用

所以不是:

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中会有不同的计算。python .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()

我想这就是你所想的:

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  

现在您可以稍后更改螺旋参数,实时。您可以向用户公开参数半径、长度和线圈,这样它们就可以被动画化。参见Maya工厂脚本。

最新更新