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

Source Code for Module nukescripts.readviewscheck

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ Check for multiple views in files when a read node is created, by 
  4  looking for a VIEW_NAMES entry in the node's metadata. 
  5  Most file types won't specify views but EXR/SXR and potentially other 
  6  files may do. 
  7  Registers for callbacks when a Read node is created, or its 'file' knob 
  8  is changed.  If there are multiple views, and they do not exist on the 
  9  root node, the user is asked if they should be created. 
 10  """ 
 11   
 12  import nuke 
 13  from PySide2.QtCore import QTimer 
 14  from PySide2.QtWidgets import QApplication, QMessageBox 
 15   
 16  # Global for storing when the user says no 
 17  UserAlreadySaidNo = False 
 18   
19 -def createViews(views):
20 """ Add the views in a list to the root node. """ 21 for view in views: 22 # Set "left" and "right" colours to match "setup views for stereo" behaviour 23 if view == "left": 24 nuke.root().addView(view, "#FF0000") 25 elif view == "right": 26 nuke.root().addView(view, "#00FF00") 27 else: 28 nuke.root().addView(view)
29 30
31 -def getViews(read):
32 """ Attempt to get the views from a read node's metadata. Returns an 33 empty list if no views were found or if the node is disabled. """ 34 35 # disabled nodes return None when calling metadata() 36 if read['disable'].value(): 37 return [] 38 39 readMetadata = read.metadata() 40 if type(readMetadata) is not dict: 41 return [] 42 43 # Try to read the expected key. Catch KeyError and return an empty list if 44 # not present 45 try: 46 # If present, the metadata value is a list of view names separated by 47 # newlines. 48 views = readMetadata[nuke.VIEW_NAMES].split('\n') 49 50 # Now exclude any views that are wrapped in [], which indicate they're generic views rather 51 # than explicitly specified named views (e.g. we're reading from a multitrack QuickTime file 52 # and hence they're just some placeholder names generated by the reader). 53 # rick: This shouldn't be necessary when we support view-to-track mapping for QuickTimes, etc. 54 trueViews = [] 55 for view in views: 56 if not (view.startswith('[') and view.endswith(']')): 57 trueViews += [view] 58 return trueViews 59 except KeyError: 60 return []
61 62
63 -def checkReadNodeViews(read):
64 """ Check if a read has multiple views, and if so, ask the user if they 65 want to add them to the root node. """ 66 global UserAlreadySaidNo 67 if not UserAlreadySaidNo: 68 views = getViews(read) 69 if views: 70 # Find the views in the read that do not exist on the Root node and if 71 # there are any, ask the user what to do. 72 rootViews = nuke.views() 73 missingViews = [ view for view in views if view not in rootViews ] 74 75 if missingViews: 76 userChoice = nuke.showCreateViewsDialog(missingViews) 77 if userChoice is nuke.DONT_CREATE_VIEWS: 78 UserAlreadySaidNo = True 79 elif userChoice is nuke.REPLACE_VIEWS: 80 viewsToDelete = [ view for view in rootViews if view not in views ] 81 # Create all views in the clip 82 createViews(views) 83 # Remove the views that are not in views 84 for existingView in viewsToDelete: 85 nuke.root().deleteView(existingView) 86 elif userChoice is nuke.ADD_VIEWS: 87 # Create only the missing views 88 createViews(missingViews)
89 90
91 -def onReadNodeCreated():
92 """ Callback when a Read node is created. Note that the knob values 93 don't seem to be set when this callback occurs. Defer the check with 94 a QTimer, which will cause the views check to be done when the Qt event 95 loop next sends events. 96 """ 97 read = nuke.thisNode() 98 QTimer.singleShot(0, lambda: checkReadNodeViews(read))
99 100
101 -def onReadNodeKnobChanged():
102 """ Callback when a Read node knob changes. If it's the file knob, 103 check for multi views. 104 """ 105 if nuke.thisKnob().name() == "file": 106 checkReadNodeViews(nuke.thisNode())
107 108 109 # Register the callbacks 110 nuke.addKnobChanged(onReadNodeKnobChanged, nodeClass="Read") 111 nuke.addOnUserCreate(onReadNodeCreated, nodeClass="Read") 112