1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import os
24 import slumMaya
25 import slum
26 import maya.cmds as m
27 from maya.mel import eval as meval
28 from maya.utils import executeInMainThreadWithResult
32 '''
33 this class defines all the static methods that shaderBase calls to correct implement
34 the 3delight support in slum nodes.
35 this class need to be registered in slumMaya.renderers, so shaderBase can know about it.
36 to register a new renderer class, just do :
37 slumMaya.renderers.append(class name)
38 '''
40 '''
41 initialize the delight class to be used in idle events, for example,
42 to render swatch using the _renderPreview method.
43 '''
44 self.node = node
45 @staticmethod
47 '''
48 This method is called by shaderBase when initializing parameters for a slum node.
49 slum is implemented in 3delight for maya as a rsl code node.
50 shadingParameters and shadingCode are the attributes that 3dfm looks for in an rsl code node.
51 we also set the attributes as "setInternal=True", which triggers maya to call our
52 getInternalValueInContext method everytime someone tries to read this parameters.
53 So, the only thing we do is hook some code into getInternalValueInContext to gather
54 parameters and code from the slum class and return to maya, dynamically.
55 '''
56 node['shadingParameters'] = ""
57 node.setHidden('shadingParameters', True)
58 node.setInternal('shadingParameters', True)
59 node.setStorable('shadingParameters', False)
60
61 node['shadingCode'] = ""
62 node.setHidden('shadingCode', True)
63 node.setInternal('shadingCode', True)
64 node.setStorable('shadingCode', False)
65
66 @staticmethod
67 - def setInternalValueInContext(plugName, node, dataHandle):
68 '''
69 This method is called by shaderBase when setting parameters of a slum node.
70 you can use this to automatically call the swatch method to render a swatch
71 when an attribute is changed.
72 '''
73 ret = False
74
75
76
77
78
79 return ret
80
81 @staticmethod
82 - def getInternalValueInContext(plugName, node, dataHandle):
83 '''
84 This method is called by shaderBase when querying parameters of a slum node.
85 slum is implemented in 3delight for maya as renderman shader code nodes. It
86 have a shadingParameters and shadingCode attributes that 3delight for maya
87 will query to get the rsl shader parameters and code for a shader.
88 This method grabs the parameters and code from the slum class and return to
89 3delight for maya dinamically.
90 '''
91 ret = False
92 dlParameters = ['shadingParameters', 'shadingCode']
93 if plugName in dlParameters:
94 import traceback
95 try:
96 delightShader = node.slum.delight( node )
97 except:
98 raise Exception('slum template error: \n%s' % traceback.format_exc() )
99 zcode = delightShader[ dlParameters.index(plugName) ]
100
101
102
103
104
105
106
107
108 code = '\n'.join( zcode ).replace('\t',' ')
109 dataHandle.setString( code )
110 ret = True
111 return ret
112
113 @staticmethod
115 '''
116 this method is called by slum node AETemplate to display a swatch images.
117 it should contain all the code to display swatch images inside slum node
118 AETemplate. Whith this method, its very easy to create diferent types of swatch
119 preview layouts, depending on the renderer.
120 '''
121 '''
122 m.scrollLayout(h=150)
123 m.gridLayout(cellWidthHeight=(128,150), nr=1)
124
125 for each in range(3):
126 m.image(w=128,h=128, enable=True, i='/tmp/xx.tif')
127
128 m.setParent('..')
129 m.setParent('..')
130 '''
131 m.rowLayout( numberOfColumns=3, adj=3, columnWidth3=((400-128)/2,128,1) )
132 m.text( label = ' ' )
133 m.image( delight._imageControlName(node), w=120,h=128, enable=True,
134 image = os.path.join(slumMaya.__path__[0],'images','previewImage.tif') )
135 m.setParent('..')
136 m.image( delight._imageControlName(node), e=True, w=128)
137
138 @staticmethod
140 return "__delightPreviewImageID_%s" % node.node
141
143 delete = []
144 type = 'surface'
145 sdl = '/tmp/%s_preview' % node
146 nodeSG = m.listConnections( node, t='shadingEngine' )
147 if nodeSG:
148 shadingGroup = nodeSG[0]
149 else:
150 shadingGroup = m.createNode('shadingEngine', name='%s_slumPreview' % node, ss=True)
151 m.connectAttr( '%s.outColor' % node, '%s.surfaceShader' % shadingGroup, f=True)
152 delete.append(shadingGroup)
153
154 if compile:
155 m.delightNodeWatch( f=True )
156 meval( 'buildShader("%s", {"%s"}, "%s", "","%s","%s", 1)' % (
157 os.path.basename(sdl),
158 shadingGroup,
159 type,
160 os.path.dirname(sdl),
161 os.path.dirname(sdl)
162 ))
163 else:
164 sdl=meval( 'DL_translateMayaToSl("%s", {"%s"}, "%s")' % (
165 os.path.basename(sdl),
166 shadingGroup,
167 type
168 ))
169
170 m.delightNodeWatch( f=True )
171 for each in delete:
172 m.delete(each)
173 return sdl
174
176 '''
177 this method is called by slum node AETemplate to render a swatch image.
178 everytime an attribute is changed, shaderBase class will call this method
179 to render a new swatch for the node.
180 '''
181 sdl = self._translateShader()
182
183 m.RiBegin(of=outputRib)
184 meval('RiOption -id false -n "rib" -p "format" "string" "ascii" ')
185 meval('RiOption -n "render" -p "bucketorder" "string" "spiral" ' )
186 meval('RiOption -n "limits" -p "bucketsize" "integer[2]" "16 16" ' )
187
188 m.RiPixelSamples( s=(3, 3) )
189 m.RiShadingRate( s=shadingRate )
190 m.RiDisplay( n = imagefile, t = display, m = "rgb")
191 m.RiFormat( r=resolution, pa=1 )
192 m.RiProjection( p=45 )
193 m.RiTranslate( 0, 0, 4)
194
195 m.RiWorldBegin()
196
197 m.RiTransformBegin()
198 m.RiArchiveRecord( m = "verbatim", t = "Translate 4 4 -4\n" )
199 meval('RiLightSource -n "pointlight" -p "intensity" "float" "1" ;' )
200 m.RiTransformEnd()
201
202 m.RiTransformBegin()
203 m.RiArchiveRecord( m = "verbatim", t = "Translate -4 4 -4\n" )
204 meval('RiLightSource -n "pointlight" -p "intensity" "float" "0.5" ;' )
205 m.RiTransformEnd()
206
207 m.RiTransformBegin()
208 m.RiArchiveRecord( m = "verbatim", t = "Translate 0 -4 -4\n" )
209 meval('RiLightSource -n "pointlight" -p "intensity" "float" "0.25" ;' )
210 m.RiTransformEnd()
211
212
213 meval('RiAttribute -n "visibility" -p "trace" "integer" 1 -p "photon" "integer" 1 -p "transmission" "string " "opaque" ;')
214
215 if type == 'surface':
216 m.RiSurface( n = sdl+'.sdl' )
217 elif type == 'displacement':
218 meval('RiAttribute -n "displacementbound" -p "sphere" "float" 2 -p "coordinatesystem" "string" "shader";')
219 m.RiDisplacement( n = sdl+'.sdl' )
220 m.RiSurface( n = "plastic" )
221
222 m.RiArchiveRecord( m = "verbatim", t = eval(model) )
223
224 m.RiArchiveRecord( m = "verbatim", t = "Translate 0 0 0 \n" )
225 m.RiArchiveRecord( m = "verbatim", t = groundRIB )
226
227 m.RiWorldEnd()
228 m.RiEnd()
229
230
231
232 slumMaya.renderers.append(delight)
233