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

Source Code for Module nukescripts.applymaterial

 1  """Functions used by the ApplyMaterial node""" 
 2   
 3  import nuke 
 4  import nukescripts.panels 
 5   
 6   
7 -class ObjectNameChooserDialog(nukescripts.panels.PythonPanel):
8 """A dialog box with a SceneView_Knob for choosing from a tree of names. 9 10 The easiest way to use this is via the chooseObjectName function from this 11 module, e.g.: 12 13 chooseObjectName(["/root/foo", "/root/bar", "/root/bar/baz"]) 14 15 This will create and display the dialog as a modal popup and give you the 16 selected name, or None if cancel was pressed. 17 """ 18
19 - def __init__(self, names):
20 nukescripts.panels.PythonPanel.__init__( self, "Object Name Chooser", "uk.co.thefoundry.ObjectNameChooserDialog" ) 21 self.sceneView = nuke.SceneView_Knob("sceneView", "object name", names) 22 self.addKnob(self.sceneView) 23 self.setMinimumSize( 420, 50 )
24
25 - def selectedName(self):
26 """Get the name selected in the SceneView_Knob.""" 27 return self.sceneView.getHighlightedItem()
28 29
30 -def chooseObjectName(amfNode):
31 """Given an ApplyMaterial node, show a modal ObjectNameChooserDialog 32 for it then set the filter_name knob to whatever name was selected in the 33 dialog. 34 """ 35 if amfNode.Class() != "ApplyMaterial": 36 raise Exception("%s is not an ApplyMaterial node" % amfNode.name()) 37 38 geoList = amfNode['geo_select'].getGeometry() 39 names = [_getName(geo) for geo in geoList] 40 dlg = ObjectNameChooserDialog(names) 41 result = dlg.showModalDialog() 42 if result: 43 name = dlg.selectedName() 44 name = _workaroundForBug36107(name, names) # Work around for Bug 36107 - SceneView_Knob trims leading '/' from strings. Remove this when that bug is fixed. 45 amfNode['filter_name'].setValue(name)
46 47
48 -def _getName(geo):
49 """Given a GeoInfo object, return the value of its 'name' attribute. If there is no such attribute, return None.""" 50 attrCtx = geo.attribContext('name', 3, 7) # 3 = per-object attribute, 7 = std::string type 51 if attrCtx is None: 52 attrCtx = geo.attribContext('name', 3, 6) # 3 = per-object attribute, 6 = c-string (char*) type 53 if attrCtx is not None: 54 attr = attrCtx.attribute 55 if attr is not None and len(attr) > 0: 56 return attr[0] 57 return None
58 59
60 -def _workaroundForBug36107(name, names):
61 if name is not None and name not in names: 62 fixedName = '/' + name 63 if fixedName in names: 64 return fixedName 65 return name
66