zainea13
zainea13

Reputation: 21

Using python to set Autolabel, but retain usual label functionality

I'm writing some python to create a more dynamic cornerpin node. I have seen this done in other pipelines before, and I like it and it seemed like a good project to continue to learn python for nuke. (or python at all lol)

I have set the "autolabel" to show the node as "stabilize" or "matchmove", but when I set the autolabel, I cannot type in tcl into the label knob and have it work. I've tried a couple things to get this to work, but i still can't quite get it. Not sure what is the issue, and I'm not sure how to solve it.

I've tried setting the autolabel, and then appending the "label" on to it, I've even tried the nuke.tcl() to interpret the [value ref_frame] in the label and show it, which works, but it just spits out the value when the node is created instead of putting that text into the label and allowing it to be interpreted as tcl.

Here is the code:

def customCornerPin():

    n = nuke.thisNode()
    n.addKnob(nuke.Int_Knob('ref_frame',"reference frame"))
    n.addKnob(nuke.PyScript_Knob('setFrame', "set to current frame", "{nuke.thisNode()['ref_frame'].setValue(nuke.frame())}"))
    n.addKnob(nuke.Boolean_Knob('stabilize',"stabilize"))

    n["stabilize"].setFlag(nuke.STARTLINE)

    n["from1"].setExpression("to1(ref_frame)")
    n["from2"].setExpression("to2(ref_frame)")
    n["from3"].setExpression("to3(ref_frame)")
    n["from4"].setExpression("to4(ref_frame)")
    n["invert"].setExpression("stabilize")


    n["ref_frame"].setValue(1001)
    n["label"].setValue("[value ref_frame]")

    def cornerPinLabel():
        
        n = nuke.thisNode()
        customAutoLabel = n.name() + '\n' + "(" + str('stabilize' if n['stabilize'].value()==1 else 'matchmove') + ")"

        if n['label'].value():
            customAutoLabel = customAutoLabel + '\n' + nuke.tcl("expr", "[value ref_frame]") + n['label'].value()
        return customAutoLabel
    nuke.addAutolabel(cornerPinLabel, nodeClass="CornerPin2D")

nuke.addOnCreate(customCornerPin, nodeClass="CornerPin2D")

I could just ignore the autolabel bit, and put the stabilize/matchmove if/else statement in the label using the setValue(), but I'd prefer to keep the label box as clean as possible.

I've also tried this version of the customAutoLabel = line: customAutoLabel = customAutoLabel + '\n' + n['label'].value(), which allows you to type anything into the label of the node, but it doesn't evaluate it as tcl if i were to type in [value to1] or whatever, it doesn't actually do it, it just displays it as a string.

I'm trying to do this in the most efficient way and not slow nuke down. From what I understand doing it via python is best, but maybe I'm wrong on that, and I should just do the tcl.

Upvotes: 2

Views: 61

Answers (1)

tk421storm
tk421storm

Reputation: 373

I haven't used autolabel myself, but the docs suggest that it works like the other callback functions, in that it expects the first argument to be a function like so:

def AutoLabelFunction(*args, **kwargs):
  return "text for label"

nuke.addAutoLabel(AutoLabelFunction, nodeClass="CornerPin2D")

You can use the optional args, kwargs arguments in "addAutoLabel" to pass information to the function.

Upvotes: 0

Related Questions