如果语句在错误

时间:2016-09-25 23:59:08

标签: python nuke

我得到" Knob已经附加到一个节点"当我尝试添加一个旋钮

当我尝试从menu.py按钮运行我的代码时,我得到了这个...如果我从脚本编辑器运行脚本我没有收到错误..为什么会这样?

for i in nuke.allNodes():
    if not i.knob("tempMb"):
        if sum0 == 0:
            nuke.message("first tmp knob created")
            i.addKnob(t)
        elif sum0 != 0:
            nuke.message("second tmp knob created")
    else: 
        nuke.message("no nob created")     

即使我检查是否有一个名为tempMb的旋钮 ..它仍然执行它,好像没有,当有... 编辑:" t"早先定义为Int_Knob ...

谢谢!

3 个答案:

答案 0 :(得分:1)

首先,我要将elif更改为else,因为您的if条件已经在测试elif条件,而且我不知道这样做会有什么变化这段代码。

for i in nuke.allNodes():
    if not i.knob("tempMb"):
        if sum0 == 0:
            nuke.message("first tmp knob created")
            i.addKnob(t)
        else:
            nuke.message("second tmp knob created")
    else: 
        nuke.message("no nob created")

其次我假设i.knob(string)没有检查该名称是否存在旋钮,或者您的代码表现得更符合您的预期。因此,当我阅读the docs时,似乎可能会发生一些事情:

  1. 节点可能是也可能不是返回列表中的旋钮。如果你知道你只想要旋钮,你可以按类类型过滤。我认为这可能看起来像nuke.allNodes(nuke.Knob)
  2. 我不认为nuke.Knob.knob(str)是对其名称或标签的测试。我认为文档暗示您的测试应该是:if i.name != "tempMb":或可能if i.label != "tempMb"这取决于您创建t的方式。
  3. 继续前进,我认为这里可能存在逻辑错误。如果您有2个节点(如果您进行了上述更改,请假设它们都是旋钮),当您遍历所有节点时,第一个节点是tempMb,那么当您检查它赢得的第二个并且您将尝试添加t,我假设它也被命名为tempMb。这就是为什么它看起来好像负面情况总是在发生。

    您需要以两种方式之一进行重组:

    1. 在循环之前,设置一个假布尔值,在循环中设置为tempMb时将其设置为true;你可以在发生这种情况时尽快退出循环。在循环之后检查布尔值以查看是否可以安全地添加t
    2. 我看到一个可能的函数nuke.exists(s),告诉您是否有任何"项目"名为s
    3. 也许删除循环并写下以下内容:

       if not nuke.exists("tempMb"):
           # Add your knob. I'm actually not seeing `addKnob` in the docs.
      

答案 1 :(得分:1)

试试这个解决方案:

t = nuke.Int_Knob( 'tempMb', 'tempMb' )

for i in nuke.allNodes():
    if not i.knob("tempMb"):
        if nuke.exists("sum0"):
            nuke.message("First tmp knob created")
            i.addKnob(t)
        else:
            nuke.message("Second tmp knob created")
    else: 
        nuke.message("No knob created")

enter image description here

答案 2 :(得分:0)

nuke.exists("Name of Knob")将检查您的旋钮是否存在。尝试在if语句中使用它。

有关Nuke forum的更多详情。

另见Documentation on nuke.exists

相关问题