Maya变量串联

2024-06-06 15:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在插入由循环生成的一堆标志时遇到了一些问题:

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时,它确实给了我想要的,但却在如何真正使其工作上苦苦挣扎!在


Tags: 标志floatlengthstrradiusspiralnumpointsyloc
2条回答

我想这就是你想要的:

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,使用节点来构建你的东西。下面是一个不必要的评论和冗长的版本:

^{pr2}$

现在你可以在以后更改螺旋参数了。您可以为用户公开参数radius、length和coils,以便对它们进行动画设置。例如,请参见Maya factory脚本。在

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

当作为关键字的dict传递参数时,它应该起作用。 在这里查一下:

所以不是:

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

你应该这么做

^{pr2}$

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

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

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

声明floatint常量,只需将它们写入小数点或不带小数点。不需要在对float()int()的调用中包装它们

相关问题 更多 >