Posted By

throb on 08/10/10


Tagged

python nuke flipbook


Versions (?)

flip.py with better support for Read nodes


 / Published in: Python
 

This is a fix to an old bug with the Foundry flip.py script

  1. # Copyright (c) 2009 The Foundry Visionmongers Ltd. All Rights Reserved.
  2.  
  3. import re
  4. import os.path
  5. import nuke
  6. import nukescripts
  7.  
  8. prev_inrange = ""
  9. prev_userrange = ""
  10.  
  11. def flipbook(command, node, framesAndViews = None):
  12. """Runs an arbitrary command on the images output by a node. This checks
  13. to see if the node is a Read or Write and calls the function directly
  14. otherwise it creates a temporary Write, executes it, and then calls
  15. the command on that temporary Write, then deletes it.
  16.  
  17. By writing your own function you can use this to launch your own
  18. flipbook or video-output programs.
  19.  
  20. Specify framesAndViews as a tuple if desired, like so: ("1,5", ["main"])
  21. This can be useful when running without a GUI."""
  22.  
  23. # Stop here if we're trying to Framecycle in PLE mode, as in some code paths
  24. # the execute is called before the Framecycler script could do this check
  25. if nuke.env['ple'] and command == nukescripts.framecycler_this:
  26. raise RuntimeError("Framecycler is not available in PLE mode.")
  27.  
  28. global prev_inrange
  29. global prev_userrange
  30.  
  31. if node is None or (node.Class() == "Viewer" and node.inputs() == 0): return
  32. if node.Class() == 'Read' :
  33. a = int( nuke.numvalue(node.name()+".first") )
  34. b = int( nuke.numvalue(node.name()+".last") )
  35.  
  36. else:
  37. a = int( nuke.numvalue("root.first_frame") )
  38. b = int( nuke.numvalue("root.last_frame") )
  39.  
  40.  
  41. try:
  42. inrange= str( nuke.FrameRange(a, b, 1) )
  43. except ValueError,e:
  44. # In this case we have to extract from the error message the
  45. # correct frame range format string representation.
  46. # I'm expecting to have a error like: "Frame Range invalid (-1722942,-1722942)"
  47.  
  48. msg = e. __str__()
  49. inrange = msg[ msg.index("(")+1: msg.index(")") ]
  50.  
  51. same_range = (inrange == prev_inrange)
  52. prev_inrange = inrange
  53.  
  54. if same_range:
  55. inrange = prev_userrange
  56.  
  57. if framesAndViews is None:
  58. r = nuke.getFramesAndViews(label = "Frames to flipbook:", default = inrange, \
  59. maxviews = (2 if nukescripts.framecycler_stereo_available() else 1))
  60. if r is None: return
  61. else:
  62. r = framesAndViews
  63.  
  64. range_input = r[0]
  65. views_input = r[1]
  66.  
  67. prev_userrange = range_input
  68.  
  69. f = nuke.FrameRange( range_input )
  70.  
  71. start =f.first()
  72. end = f.last()
  73. incr = f.increment()
  74.  
  75. if (start) < 0 or (end<0):
  76. raise RuntimeError("Flipbook cannot be executed, negative frame range not supported(%s)." % ( str(f),) )
  77.  
  78. proxy = nuke.toNode("root").knob("proxy").value()
  79.  
  80. if (node.Class() == "Read" or node.Class() == "Write") and not proxy:
  81. try:
  82. command(node, start, end, incr, views_input)
  83. except Exception, msg:
  84. nuke.message("Error running flipbook:\n%s" % (msg,))
  85. return
  86.  
  87. if node.Class() == "Viewer":
  88. input_num = int(nuke.knob(node.name()+".input_number"))
  89. input = node.input(input_num)
  90. if input is None: return
  91.  
  92. if (input.Class() == "Read" or input.Class() == "Write") and not proxy:
  93. try:
  94. command(input, start, end, incr, views_input)
  95. except Exception, msg:
  96. nuke.message("Error running flipbook:\n%s" % (msg,))
  97. return
  98.  
  99. # okay now we must execute it...
  100. flipbooktmp=""
  101. if flipbooktmp == "":
  102. try:
  103. flipbooktmp = os.environ["FC_DISK_CACHE"]
  104. except:
  105. try:
  106. flipbooktmp = os.environ["NUKE_DISK_CACHE"]
  107. except:
  108. flipbooktmp = nuke.value("preferences.DiskCachePath")
  109.  
  110. if len(views_input) > 1:
  111. flipbookFileNameTemp = "nuke_tmp_flip.%04d.%V.rgb"
  112. else:
  113. flipbookFileNameTemp = "nuke_tmp_flip.%04d.rgb"
  114. flipbooktmp = os.path.join(flipbooktmp, flipbookFileNameTemp)
  115.  
  116. if nuke.env['WIN32']:
  117. flipbooktmp = re.sub("\\\\", "/", str(flipbooktmp))
  118.  
  119. fieldname="file"
  120. if proxy:
  121. fieldname="proxy"
  122.  
  123. write = nuke.createNode("Write", fieldname+" {"+flipbooktmp+"} "+"tile_color 0xff000000", inpanel = False)
  124. #If called on a Viewer connect Write node to the one immediately above if exists.
  125. if node.Class() == "Viewer":
  126. write.setInput(0, node.input(int(nuke.knob(node.name()+".input_number"))))
  127.  
  128. try:
  129. # Throws exception on render failure
  130. nuke.executeMultiple((write,), ([start,end,incr], ), views_input)
  131. command(write, start, end, incr, views_input)
  132. except Exception, msg:
  133. nuke.message("Flipbook render failed:\n%s" % (msg,))
  134. nuke.delete(write)

Report this snippet  

You need to login to post a comment.