Question

So, I'm trying to get python to print a string that had a tcl variable in it, with the result of that variable. I'm using nuke, in case that matters for my code.

[set THIS image] 
Ln = "/Test/still/testing/$THIS\_clean_v001"
print(Ln) # this prints exactly the above

G = nuke.tcl('[puts "$THIS"] ') 

Would print(G) return the word image or no?

I could then sub it in. The problem is the text field I'm entering that tcl in to solves it inside the program fine, but then as soon as I send it to process it takes the $THIS literally.

Was it helpful?

Solution

Two things to note:

  1. Tcl puts does not return a value. It sounds like you want Tcl set, which does.

  2. The docs for nuke.tcl at http://www.nukepedia.com/reference/Python indicate:

    tcl(command, arg, arg, ...)

    Run a tcl command. The arguments must be strings and passed to the command. If no arguments are given and the command has whitespace in it then it is instead interpreted as a tcl program (this is depreciated)

Rather than invoking it as a full script, it seems you could both use the non-deprecated version and get the behavior you want by using:

G = nuke.tcl("set", "THIS")

Whic should reutrn the value of the THIS variable.

OTHER TIPS

You can also do a one-shot expansion of all TCL variables in a string using the TCL subst command.

nuke.tcl('set', 'THIS', 'image')
p = '/Test/still/testing/$THIS\_clean_v001'
print nuke.tcl('subst', p)

The result of this will be the properly-expanded string: '/Test/still/testing/image_clean_v001'

Documentation for subst can be found here: http://www.tcl.tk/man/tcl8.4/TclCmd/subst.htm

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top