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

Source Code for Module nukescripts.modelbuilder

  1  """Functions used by the ModelBuilder node""" 
  2   
  3  import nuke 
  4  import nukescripts 
  5   
  6   
  7  # 
  8  # Initialisation 
  9  ## 
 10   
11 -def populateBakeMenu(mb):
12 """Populate the bake menu on a ModelBuilder node.""" 13 entries = [ 14 # Label # Script to execute when the bake button is pressed. 15 ( 'Selected geometry', 'nukescripts.modelbuilder.bakeSceneSelection(nuke.thisNode())' ), 16 ( 'Projection', 'nukescripts.modelbuilder.bakeProjection(nuke.thisNode())' ), 17 ( 'Apply texture', 'nukescripts.modelbuilder.bakeApplyTexture(nuke.thisNode())' ) 18 # Add your own entries here, if desired. 19 ] 20 k = mb['bakeMenu'] 21 k.setValues([ "%s\t%s" % (script, label) for label, script in entries ])
22 23 24 # 25 # Bake functions 26 # 27
28 -def bakeSceneSelection(mb):
29 """Create a ModelBuilderGeo node containing a copy of the currently selected geometry.""" 30 if mb.Class() != "ModelBuilder": 31 return 32 33 # Note that if the "expand/collapse panels to match selection" setting is 34 # true, Nuke will collapse the ModelBuilder panel when createNode is called 35 # below. That seems to have the side effect of making the ModelBuilder think 36 # nothing is selected in its tree knob, which means it'll come back with an 37 # empty string when we execute the serializeScene knob (the cause of bugzilla 38 # bug #32592). So it's important to do the serialisation and save the results 39 # *before* calling createNode. 40 41 mb['serializeScene'].execute() 42 serializeStr = mb['serializeStr'].getValue() 43 serializeName = mb['serializeName'].getValue() 44 mb['serializeName'].setValue('') 45 mb['serializeStr'].setValue('') 46 47 geo = nuke.createNode('ModelBuilderGeo','',False) 48 geo.setXYpos( int(mb.xpos() + mb.screenWidth() * 1.5), int(mb.ypos()) ) 49 geo['scene'].fromScript( serializeStr ) 50 geo['label'].setValue( serializeName )
51 52
53 -def bakeProjection(mb):
54 """Create a projection setup for the current frame.""" 55 if mb.Class() != "ModelBuilder": 56 return 57 58 clearNodeSelection() 59 srcCam = mb.input(1) 60 if not srcCam: 61 return 62 w = mb.screenWidth() 63 h = mb.screenHeight() 64 src = mb.input(0) 65 frame = nuke.frame() 66 if (mb['textureType'].getValue() > 0): 67 frame = mb['textureFrame'].getValue() 68 label = '(frame ' + str(frame) + ')' 69 70 srcdot = nuke.createNode('Dot','',False) 71 srcdot.setInput(0, src) 72 srchold = nuke.createNode('FrameHold','',False) 73 stamp = nuke.createNode('PostageStamp','',False) 74 project = nuke.createNode('Project3D','',False) 75 apply = nuke.createNode('ApplyMaterial','',False) 76 77 cam = copyCamera(srcCam) 78 79 camhold = nuke.clone(srchold,'',False) 80 camdot = nuke.createNode('Dot','',False) 81 project.setInput(1, camdot) 82 83 srcdot.setXYpos( int(mb.xpos() + w * 3.5), int(mb.ypos()) ) 84 srchold.setXYpos( int(srchold.xpos()), int(srcdot.ypos() + w) ) 85 stamp.setXYpos( int(stamp.xpos()), int(stamp.ypos() + srchold.screenHeight() + h * 2) ) 86 cam.setXYpos( int(srchold.xpos() - w * 1.5), int(srchold.ypos() + srchold.screenHeight() / 2 - cam.screenHeight() / 2) ) 87 camhold.setXYpos( int(cam.xpos() + cam.screenWidth() / 2 - srchold.screenWidth() / 2), int(stamp.ypos() + h) ) 88 project.setXYpos( int(stamp.xpos()), int(stamp.ypos() + stamp.screenHeight() + h * 2) ) 89 camdot.setXYpos( int(camdot.xpos()), int(project.ypos() + project.screenHeight() / 2 - camdot.screenHeight() / 2) ) 90 apply.setXYpos( int(stamp.xpos()), int(apply.ypos() + w) ) 91 92 srchold.knob('first_frame').setValue( frame ) 93 srcdot.setSelected( True ) 94 srchold.setSelected( True ) 95 stamp.setSelected( True ) 96 project.setSelected( True ) 97 apply.setSelected( True ) 98 cam.setSelected( True ) 99 camhold.setSelected( True ) 100 camdot.setSelected( True ) 101 bd = nukescripts.autoBackdrop() 102 bd['label'].setValue('Frame %d' % frame)
103 104
105 -def bakeApplyTexture(mb):
106 if mb.Class() != "ModelBuilder": 107 return 108 109 texture = mb.input(3) 110 if not texture: 111 return 112 113 clearNodeSelection() 114 mb.setSelected(True) 115 116 amat = nuke.createNode('ApplyMaterial') 117 amat.setInput(1, texture) 118 amat['filter_type'].setValue('name') 119 amat['filter_name_match'].setValue('equals') 120 amat['filter_refresh'].execute()
121 122 123 # 124 # Helpers 125 # 126
127 -def clearNodeSelection():
128 for n in nuke.selectedNodes(): 129 n.setSelected(False)
130 131
132 -def copyCamera(srcCam):
133 while srcCam is not None and srcCam.Class() == 'Dot': 134 srcCam = srcCam.input(0) 135 if srcCam.Class() == 'Camera': 136 newCam = nuke.createNode('Camera') 137 newCam.readKnobs(srcCam.writeKnobs(nuke.TO_SCRIPT)) 138 else: 139 clearNodeSelection() 140 newCam = nuke.clone(srcCam, '', False) 141 newCam.setSelected(True) 142 return newCam
143 144 145 # 146 # Callbacks 147 # 148
149 -def modelbuilderCreateCallback():
150 populateBakeMenu(nuke.thisNode())
151 152 153 assist = nuke.env['assist'] 154 if not assist: 155 nuke.addOnCreate(modelbuilderCreateCallback, nodeClass='ModelBuilder') 156