If statement executes when it's false
<p>I am getting "Knob already attached to a node" when i try to add a knob</p>

<p>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?</p>

<pre><code>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")     
</code></pre>

<p>Even though <strong>i check if there is a knob named tempMb</strong> .. it still executes it as if there was not, when there is..
edit: "t" is earlier defined as Int_Knob...</p>

<p>Thanks!</p>

<p>First I'm going to change the <code>elif</code> to just <code>else</code> because your if condition is already testing the elif condition and I don't see how that would be changing while in this code.</p>

<pre><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")
</code></pre>

<p>Second I'm assuming that <code>i.knob(string)</code> 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 <a href="https://docs.thefoundry.co.uk/nuke/80/pythonreference/nuke-module.html#allNodes" rel="nofollow">the docs</a> it seems like a couple of things may happen:</p>

<ol>
<li>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 <code>nuke.allNodes(nuke.Knob)</code>.</li>
<li>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: <code>if i.name != "tempMb":</code> or possibly <code>if i.label != "tempMb"</code> it depends on how you created <code>t</code>.</li>
</ol>

<p>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 <code>tempMb</code>, then when you check the second one it won't be that and you'll try to add <code>t</code>, which I assume is named <code>tempMb</code> too. So that's why it looks to you as though the negative condition is always occurring.</p>

<p>You need to restructure in one of two ways:</p>

<ol>
<li>Before the loop, set a false boolean, in the loop set it to true when the knob is <code>tempMb</code> 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 <code>t</code>.</li>
<li>I see a possible function <a href="https://docs.thefoundry.co.uk/nuke/80/pythonreference/nuke-module.html#exists" rel="nofollow"><code>nuke.exists(s)</code></a> which tells you if you have any "item" named <code>s</code>.</li>
</ol>

<p>Maybe remove the loop and write the following:</p>

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

python nuke