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