简体   繁体   English

if 语句为 false 时执行

[英]If statement executes when it's false

I am getting "Knob already attached to a node" when i try to add a knob当我尝试添加旋钮时,我收到“旋钮已连接到节点”

i get this when i try to run my code from menu.py button.. if i run the script from the script editor i don't get the error.. why is that?当我尝试从 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")     

Even though i check if there is a knob named tempMb .. it still executes it as if there was not, when there is.. edit: "t" is earlier defined as Int_Knob...即使我检查是否有一个名为 tempMb 的旋钮.. 它仍然像没有一样执行它,当有时 .. 编辑:“t”早先被定义为 Int_Knob ......

Thanks!谢谢!

First I'm going to change the elif to just else because your if condition is already testing the elif condition and I don't see how that would be changing while in this code.首先,我要将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")

Second I'm assuming that i.knob(string) doesn't check for the existence of a knob by that name, or your code would behave more as you expected.其次,我假设i.knob(string)不检查该名称的旋钮是否存在,否则您的代码会表现得更像您预期的那样。 So when I read the docs it seems like a couple of things may happen:因此,当我阅读文档时,似乎可能会发生一些事情:

  1. The nodes might or might not be knobs in the list returned.节点可能是也可能不是返回的列表中的旋钮。 If you know you only want knobs you could filter by class type.如果你知道你只想要旋钮,你可以按类类型过滤。 I think that might look like nuke.allNodes(nuke.Knob) .我认为这可能看起来像nuke.allNodes(nuke.Knob)
  2. I don't think a nuke.Knob.knob(str) is a test for its name or label.我不认为 nuke.Knob.knob(str) 是对其名称或标签的测试。 I read the docs as implying that your test should be: if i.name != "tempMb": or possibly if i.label != "tempMb" it depends on how you created t .我阅读文档时暗示您的测试应该是: if i.name != "tempMb":或者if i.label != "tempMb"这取决于您如何创建t

Moving on though, I think there may be a logical error here.尽管如此,我认为这里可能存在逻辑错误。 If you have 2 nodes (and if you make the above changes, let's assume they're both knobs), and as you loop over all nodes the first one is the tempMb , then when you check the second one it won't be that and you'll try to add t , which I assume is named tempMb too.如果您有 2 个节点(并且如果您进行了上述更改,我们假设它们都是旋钮),并且当您遍历所有节点时,第一个节点是tempMb ,那么当您检查第二个节点时,它不会是那个你会尝试添加t ,我假设它也被命名为tempMb So that's why it looks to you as though the negative condition is always occurring.所以这就是为什么在你看来,负面情况似乎总是在发生。

You need to restructure in one of two ways:您需要通过以下两种方式之一进行重组:

  1. Before the loop, set a false boolean, in the loop set it to true when the knob is tempMb is found;在循环之前,设置一个 false 布尔值,在循环中,当发现旋钮为tempMb时将其设置为 true; you may as well exit the loop as soon as this occurs.一旦发生这种情况,您最好立即退出循环。 After the loop check the boolean to see if it's safe to add t .在循环之后检查布尔值以查看添加t是否安全。
  2. I see a possible function nuke.exists(s) which tells you if you have any "item" named s .我看到一个可能的函数nuke.exists(s)它告诉你是否有任何名为s “项目”。

Maybe remove the loop and write the following:也许删除循环并编写以下内容:

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

Try this solution:试试这个解决方案:

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")

在此处输入图片说明

nuke.exists("Name of Knob") will check if your knob exists. nuke.exists("Name of Knob")将检查您的旋钮是否存在。 Try using that in your if statement.尝试在您的 if 语句中使用它。

More details on Nuke forum .更多详情请访问Nuke 论坛

See also Documentation on nuke.exists另请参阅有关 nuke.exists 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM