forked from TianD/TianD_arnold_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateRenderLayers.py
226 lines (198 loc) · 6.59 KB
/
CreateRenderLayers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#coding:utf-8
'''
Created on 2015年10月26日 下午3:32:42
@author: TianD
@E-mail: tiandao_dunjian@sina.cn
@Q Q: 298081132
@Description: create render layer
'''
import pymel.core as pm
from lightRenderData import ProjNameMatch
import kxTool
import aov_tool as at
import CreateNodeGroup as CNG
#创建显示层
#包括: chr_color, bg_color, IDP, shadow, RGBlight, FOGlight
LAYER_FILENAME = {'chr_color':'CHRcolor',
'night_chr_color':'CHRcolor',
'bg_color':'BGcolor',
'chr_idp1':'CHRidp1',
'chr_idp2':'CHRidp2',
'chr_idp3':'CHRidp3',
'bg_idp1':'BGidp1',
'bg_idp2':'BGidp2',
'bg_idp3':'BGidp3',
'shadow':'shadow',
'RGBlight':'RGBlight',
'FOGlight':'FOGlight'}
def saveFile(describe):
#按照描述保存文件
#
#参数describe: 需要输入描述字符串, 如: 'CHRcolor'等, 保存文件时加入到文件名中.
#
pnm = ProjNameMatch()
fileName = pm.Env().sceneName().namebase
dir = pnm.setFileName(fileName)
dir = pm.Path(pnm.getProjDirectorys()[-1]+pnm.getProjDirectorys()[0]+'/scenes')
project_name = pnm.getResults('project_name')
episode_number = pnm.getResults('episode_number')
session_number = pnm.getResults('session_number')
scene_number = pnm.getResults('scene_number')
scene_describe = LAYER_FILENAME[describe]
process_name = 'lr'
version_number = 'c001'
newFileName = '_'.join([project_name, episode_number, session_number, scene_number, scene_describe, process_name, version_number])
filePrefix = '_'.join([project_name, episode_number, session_number, scene_number, scene_describe, process_name])
versions = []
for f in dir.files():
subpnm = ProjNameMatch()
if filePrefix in f.name:
subpnm.setFileName(f.name)
versions.append(subpnm.getResults('version_number'))
if versions:
versions.sort()
version_number = 'c'+ str(int(versions[-1][1:])+1).zfill(3)
newFileName = '_'.join([project_name, episode_number, session_number, scene_number, scene_describe, process_name, version_number])
try:
pm.saveAs("{0}/{1}.mb".format(dir, newFileName))
return newFileName
except:
raise "save this file failure!!!"
def createRenderLayer(name):
#创建渲染层
#
#参数name: 需要输入渲染层的名字
#
#获取所有的模型对象
allgeos = getallGeos()
topgrp = list(set([i.getParent(-1) for i in allgeos]))
members = getallTops()
#切换渲染器
try:
defaultLayer = pm.nodetypes.RenderLayer.defaultRenderLayer()
defaultLayer.setCurrent()
except:
pass
#删除所有的AOV
at.delAOV()
#删除所有的渲染层
renderLayers = pm.ls(type = 'renderLayer')
for rl in renderLayers:
if 'defaultRenderLayer' not in rl.name():
pm.delete(rl)
#创建渲染层
if name == "bg_color":
for key in ['AO', 'NOM', 'Fre', 'Z']:
at.createAOV(key)
members = getsetTops() + getlightTops()
elif name == "chr_color":
for key in ['AO', 'NOM', 'Fre', 'sss']:
at.createAOV(key)
im = importLight()
members = getchrandpropTops() + im
elif name == "night_chr_color":
for key in ['AO', 'NOM', 'Fre', 'sss']:
at.createAOV(key)
im = importLight()
members = getchrandpropTops() + im
elif name in ["chr_idp1", "chr_idp2", "chr_idp3", "bg_idp1", "bg_idp2", "bg_idp3"]:
delAllShaders(topgrp)
shader, sg = CNG.createIDPNode('Maeet')
CNG.assignShader(topgrp, shader, sg)
elif name == "shadow":
delAllShaders(topgrp)
at.createAOV('COCC')
shader, sg = CNG.createShadow()
CNG.assignShader(topgrp, shader, sg)
elif name == "RGBlight":
delAllShaders(topgrp)
shader, sg = CNG.createRGBlight()
CNG.assignShader(topgrp, shader, sg)
elif name == "FOGlight":
delAllShaders(topgrp)
shader, sg = CNG.createFOGlight()
CNG.assignShader(topgrp, shader, sg)
else :
pass
if name == 'night_chr_color':
layerName = 'chr_color'
else :
layerName = name
try:
layer = pm.nodetypes.RenderLayer.findLayerByName(layerName)
except:
layer = pm.createRenderLayer(name = layerName, e=1)
try:
defaultLayer = pm.nodetypes.RenderLayer.defaultRenderLayer()
defaultLayer.renderable.set(0)
except:
pass
layer.setCurrent()
if members:
layer.addMembers(members)
try:
saveFile(name)
except:
pass
def importLight(path = "Z:/Proj/SENBA/Senba_link/Render/chr_light/chr_light.mb"):
#chr_color层需要导入默认灯光
#
#参数path: 要导入的文件的路径
#
#返回值是导入的对象列表
kx = kxTool.KXTool()
kx.getSceneName()
kx.analyzeSceneName()
if kx.projectName:
path = kx.chr_light_pathDic[kx.projectName]
importNode = pm.importFile(path, returnNewNodes = 1)
return importNode
def getallTops():
return pm.ls(assemblies = 1)
def getchrandpropTops():
#获取角色道具组
return [i for i in pm.ls(assemblies = 1) if 'charRigGrp' in i.name() or 'CharRigGrp' in i.name() or 'PropRigGrp' in i.name()]
def getsetTops():
#获取场景组
return pm.ls(regex = '*:*SetRigGrp')
def getlightTops():
#获取灯光组
return list(set([l.getParent(-1) for l in pm.ls(lights = 1)]))
def getallGeos():
#获取模型对象
geos = [i for i in pm.ls(type = "mesh") + pm.ls(type = "nurbsSurface") if not i.isIntermediateObject()]
return geos
def delAllShaders(geos):
#删除所有材质球
#
#参数geos: 模型对象
try:
defaultShader = pm.PyNode("lambert1")
defaultSG = pm.PyNode("initialShadingGroup")
except:
defaultShader, defaultSG = pm.createSurfaceShader('lambert', name = 'lambert1')
pm.waitCursor(state=1)
pm.sets(defaultSG, forceElement = geos)
pm.mel.MLdeleteUnused()
pm.waitCursor(state=0)
def setObjAttr(obj, attr, value):
#设置对象属性
#
#参数obj: 模型对象
#
#参数attr: 属性名
#
#参数value: 属性值
try:
obj.attr(attr).set(value)
except:
print "cannot set {0}.{1} value".format(obj, attr)
return True
def getObjAttr(obj, attr):
#获取对象属性
#
#参数obj: 模型对象
#
#参数attr: 属性名
#
return obj.attr(attr).get()