简体   繁体   中英

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?

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...

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.

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. 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) .
  2. I don't think a nuke.Knob.knob(str) is a test for its name or label. 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 .

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. 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; 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 .
  2. I see a possible function nuke.exists(s) which tells you if you have any "item" named 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. Try using that in your if statement.

More details on Nuke forum .

See also Documentation on nuke.exists

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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