Package nukescripts :: Module version
[hide private]
[frames] | no frames]

Source Code for Module nukescripts.version

  1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved. 
  2   
  3  import nuke 
  4  import os.path 
  5  import re 
  6   
  7  __NODES_FOR_VERSION = ( "Read", 
  8                          "Write", 
  9                          "Precomp", 
 10                          "DeepRead", 
 11                          "DeepWrite", 
 12                          "LiveGroup", 
 13                        ) 
 14   
15 -def version_get(string, prefix, suffix = None):
16 """Extract version information from filenames used by DD (and Weta, apparently) 17 These are _v# or /v# or .v# where v is a prefix string, in our case 18 we use "v" for render version and "c" for camera track version. 19 See the version.py and camera.py plugins for usage.""" 20 21 if string is None: 22 raise ValueError, "Empty version string - no match" 23 24 regex = "[/_.]"+prefix+"\d+" 25 matches = re.findall(regex, string, re.IGNORECASE) 26 if not len(matches): 27 msg = "No \"_"+prefix+"#\" found in \""+string+"\"" 28 raise ValueError, msg 29 return (matches[-1:][0][1], re.search("\d+", matches[-1:][0]).group())
30
31 -def version_set(string, prefix, oldintval, newintval):
32 """Changes version information from filenames used by DD (and Weta, apparently) 33 These are _v# or /v# or .v# where v is a prefix string, in our case 34 we use "v" for render version and "c" for camera track version. 35 See the version.py and camera.py plugins for usage.""" 36 37 regex = "[/_.]"+prefix+"\d+" 38 matches = re.findall(regex, string, re.IGNORECASE) 39 if not len(matches): 40 return "" 41 42 # Filter to retain only version strings with matching numbers 43 matches = filter(lambda s: int(s[2:]) == oldintval, matches) 44 45 # Replace all version strings with matching numbers 46 for match in matches: 47 # use expression instead of expr so 0 prefix does not make octal 48 fmt = "%%(#)0%dd" % (len(match) - 2) 49 newfullvalue = match[0] + prefix + str(fmt % {"#": newintval}) 50 string = re.sub(match, newfullvalue, string) 51 return string
52
53 -def version_up():
54 """All new version_up that uses the version_get/set functions. 55 This script takes the render version up one in selected iread/writes.""" 56 57 n = nuke.selectedNodes() 58 for i in n: 59 _class = i.Class() 60 # check to make sure this is a read or write op 61 if _class in __NODES_FOR_VERSION: 62 fileKnob = i['file'] 63 proxyKnob = i.knob('proxy') 64 try: 65 (prefix, v) = version_get(fileKnob.value(), 'v') 66 v = int(v) 67 fileKnob.setValue(version_set(fileKnob.value(), prefix, v, v + 1)) 68 except ValueError: 69 # We land here if there was no version number in the file knob. 70 # If there's none in the proxy knob either, just show the exception to the user. 71 # Otherwise just update the proxy knob 72 if proxyKnob and proxyKnob.value(): 73 (prefix, v) = version_get(proxyKnob.value(), 'v') 74 v = int(v) 75 76 if proxyKnob and proxyKnob.value(): 77 proxyKnob.setValue(version_set(proxyKnob.value(), prefix, v, v + 1)) 78 79 nuke.root().setModified(True)
80
81 -def version_down():
82 """All new version_down that uses the version_get/set functions. 83 This script takes the render version up one in selected iread/writes.""" 84 85 n = nuke.selectedNodes() 86 for i in n: 87 _class = i.Class() 88 # check to make sure this is a read or write op 89 if _class in __NODES_FOR_VERSION: 90 fileKnob = i['file'] 91 proxyKnob = i.knob('proxy') 92 try: 93 (prefix, v) = version_get(fileKnob.value(), 'v') 94 v = int(v) 95 fileKnob.setValue(version_set(fileKnob.value(), prefix, v, v - 1)) 96 except ValueError: 97 # We land here if there was no version number in the file knob. 98 # If there's none in the proxy knob either, just show the exception to the user. 99 # Otherwise just update the proxy knob 100 if proxyKnob and proxyKnob.value(): 101 (prefix, v) = version_get(proxyKnob.value(), 'v') 102 v = int(v) 103 if proxyKnob and proxyKnob.value(): 104 proxyKnob.setValue(version_set(proxyKnob.value(), prefix, v, v - 1)) 105 nuke.root().setModified(True)
106
107 -def version_latest():
108 """Like version_up, but only goes up to the highest numbered version 109 that exists. 110 111 Works on all selected Read nodes, or all Read nodes if nothing is 112 selected. 113 114 Does not modify Write nodes.""" 115 116 class __KnobValueReplacer(object): 117 def loop(self, knob): 118 while True: 119 oVersion = knob.value() 120 try: 121 (prefix, v) = version_get(oVersion, 'v') 122 v = int(v) 123 nVersion = version_set(oVersion, prefix, v, v + 1) 124 knob.setValue(nVersion) 125 if not os.path.exists(knob.evaluate()): 126 knob.setValue(oVersion) 127 return 128 nuke.root().setModified(True) 129 except ValueError: 130 return
131 132 nodes = nuke.selectedNodes() 133 if not nodes: nodes = nuke.allNodes() 134 n = [i for i in nodes if i.Class() == "Read"] 135 for i in n: 136 __KnobValueReplacer().loop(i['file']) 137 __KnobValueReplacer().loop(i['proxy']) 138