[英]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:因此,当我阅读文档时,似乎可能会发生一些事情:
nuke.allNodes(nuke.Knob)
.nuke.allNodes(nuke.Knob)
。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:您需要通过以下两种方式之一进行重组:
tempMb
is found;tempMb
时将其设置为 true; you may as well exit the loop as soon as this occurs.t
.t
是否安全。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.
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.