生成一个Mel脚本

时间:2011-11-21 15:16:09

标签: python

关于我昨天问过哪个问题passing contents from multiple lists generated in different functions to a file我写了代码。部分代码如下,

def lower_lip_under_upper_teeth_bezier(x_n, p0, p3) :
    """ Calculating sampling points using rational bezier curve equation"""
    lower_lip_under_upper_teeth_p_u_list = []
    u = x_n
    p1 = p0
    p2 = p3

    lower_lip_under_upper_teeth_p_u = math.pow(1 - u, 3) * p0 + 3 * u * math.pow(1 - u, 2) * p1 \
                                 + 3 * (1 - u) * math.pow(u, 2) * p2 + math.pow(u, 3) * p3
    lower_lip_under_upper_teeth_p_u = lower_lip_under_upper_teeth_p_u * w
    d = math.pow(1 - u, 3) * w + 3 * u * w * math.pow(1 - u, 2) + 3 * (1 - u) * w * math.pow(u, 2) + math.pow(u, 3) * w
    lower_lip_under_upper_teeth_p_u = lower_lip_under_upper_teeth_p_u / d

    print "p(u): ", lower_lip_under_upper_teeth_p_u
    lower_lip_under_upper_teeth_p_u_list.append(lower_lip_under_upper_teeth_p_u)

    return lower_lip_under_upper_teeth_p_u_list

def mel_script() :
  """ Generating the mel script with the animation information """
    print "\n The mel script generated for the input speech with the chosen energy level" 
    with open("mel.txt", "w") as melFile :
        melFile.write('setKeyframe "BS_stickyLips_SL_recept.head_geo_stickyLips_wire";'
                      'setKeyframe "BS_stickyLips_baseSL_recept.head_geo";'
                      'setKeyframe "BS_stickyLips_wireSL_recept.head_geo";'
                      'setKeyframe "blend_shape.lip_round";'
                      'setKeyframe "blend_shape.jaw_open";'
                      'setKeyframe "blend_shape.lips_spread";'
                      'setKeyframe "blend_shape.lips_part";'
                      'setKeyframe "blend_shape.lower_lip_under_upper_teeth";')

    for p in lower_lip_under_upper_teeth_bezier :
        melFile.write('setAttr "blend_shape.jaw_open" %f ;' % p )
        melFile.write('setKeyframe -breakdown 0 -hierarchy none -controlPoints 0 -shape 0 {"blend_shape"};')

但我收到一个错误,

    for p in lower_lip_under_upper_teeth_bezier :
TypeError: 'function' object is not iterable

2 个答案:

答案 0 :(得分:3)

您没有调用此功能。要调用它,请使用

lower_lip_under_upper_teeth_bezier(x_n, p0, p3)

并替换参数的适当值。

答案 1 :(得分:1)

lower_lip_under_upper_teeth_bezier功能。错误信息清楚地告诉它,没有办法解决它。

您可能相信 lower_lip_under_upper_teeth_bezier是可迭代的,但事实并非如此。

现在,我只是一个猜测,但我相信你想要做的是:

def mel_script(lip_var):
    '''The mel script generated for the input speech with the chosen energy level'''
    with open("mel.txt", "w") as melFile :
        melFile.write('setKeyframe "BS_stickyLips_SL_recept.head_geo_stickyLips_wire";'
                      'setKeyframe "BS_stickyLips_baseSL_recept.head_geo";'
                      'setKeyframe "BS_stickyLips_wireSL_recept.head_geo";'
                      'setKeyframe "blend_shape.lip_round";'
                      'setKeyframe "blend_shape.jaw_open";'
                      'setKeyframe "blend_shape.lips_spread";'
                      'setKeyframe "blend_shape.lips_part";'
                      'setKeyframe "blend_shape.lower_lip_under_upper_teeth";')
    for p in lip_var:    # Check this out!!!
        melFile.write('setAttr "blend_shape.jaw_open" %f ;' % p )

编辑(参见评论):我认为对python的工作原理有一些基本的误解...在python中(就像在大多数编程语言中一样!)当你声明一个函数时,你声明其名称和它期望的参数

如果您尝试将5个列表传递给mel_script,当您声明mel_script时,您应该这样说:

def mel_script(list1, list2, list3, list4, list5):
    # Your code here should work with list1, list2, etc...

然后,在调用mel_script时,您需要将这些列表传递给它。如果此类列表由函数生成(例如f1f2f3 ...)。您可以使用以下命令完成所有操作:

mel_script(f1(), f2(), f3(), f4(), f5())

否则您必须将列表存储在临时变量中并将其传递给mel_scirpt

tmp1 = f1()
tmp2 = f2()
tmp3 = f3()
tmp4 = f4()
tmp5 = f5()
mel_script(tmp1, tmp2, tmp3, tmp4, tmp5)

在上面的示例中,请注意f1()有圆括号表示您调用这个名为f1的函数。如果你省略那些,那么你将传递函数本身,而不是它的结果

实际上有更聪明的方法来实现这一点(比如传递可变数量的参数,或者使用闭包,但从你的问题我明白你不是(还是!)精通python,所以最好坚持下去现在使用这种方法!:)

HTH!