-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaya Bakenator
226 lines (186 loc) · 8.27 KB
/
maya Bakenator
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
import maya.cmds as cmds
import maya.mel as mel
#ESC Tool
gMainProgressBar = mel.eval('$tmp = $gMainProgressBar');
cmds.progressBar( gMainProgressBar,
edit=True,
beginProgress=True,
isInterruptable=True,
status='"I AM BAKING...',
maxValue=5000)
minF = cmds.playbackOptions(q=True,minTime=True)
maxF = cmds.playbackOptions(q=True,maxTime=True)
#cmds.playbackOptions( by=2 )
rangeF = maxF-minF
def bake(*args):
#cmds.progressBar(progressControl2, edit=True, endProgress=True)
#get seletions
Psel = cmds.ls( selection=True )
count = cmds.getAttr(Psel[0]+'.count')
print "particlecount:"
print count
inst = cmds.listConnections( Psel[0]+'.instanceData', d=True, s=True )
print "instancer:"
print inst[0]
baked = cmds.spaceLocator(name="instancerbake")
print baked
minF = cmds.playbackOptions(q=True,minTime=True)
maxF = cmds.playbackOptions(q=True,maxTime=True)
rangeF = maxF-minF
curFrame = minF -1
#get jump frame
framejump = cmds.textFieldGrp( 'jump',query=True, text=True)
framejump = int(framejump)
#collect bake Geo setup
DoSimplify = cmds.checkBoxGrp( 'simple',query=True,value1=True)
simpleGeo = []
#----------------------------------------------------------------------------------------------------------
#walking through frames
#----------------------------------------------------------------------------------------------------------
for f in range(rangeF):
#look for ESC
if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
break
#----------------
#setup jump frames
#---------------
curFrame +=framejump
print "baking frame: "+str(curFrame)
cmds.currentTime( curFrame, edit=True )
#update progressBAR
cmds.progressBar(gMainProgressBar, edit=True, step=1)
#check particles
count = cmds.getAttr(Psel[0]+'.count')
print "particlecount: " + str(count)
#start vbake if particles there
#-------------------------------
if count > 0:
#look for ESC
if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
break
#----------------------------------------------------------------------------------------------------------
#bake current frame
#----------------------------------------------------------------------------------------------------------
realIDs = cmds.getAttr(Psel[0]+'.particleId')
for i in range(len(realIDs)):
#look for ESC
if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
break
#get particle Attr
meshIDAttr = cmds.textFieldGrp( 'meshid',query=True, text=True)
sizePPAttr = cmds.textFieldGrp( 'size',query=True, text=True)
rotationPPAttr = cmds.textFieldGrp( 'rotation',query=True, text=True)
inn = int(i)
pid = int(realIDs[inn])
positionPP = cmds.nParticle(Psel[0],attribute='worldPosition',query=True,particleId=pid)
doscale = 1
try:
sizePP = cmds.nParticle(Psel[0],attribute=sizePPAttr,query=True,particleId=pid)
except RuntimeError :
print "No size Attr found :" + str(pid)
doscale = 0
dorotate = 1
try:
rotationPP = cmds.nParticle(Psel[0],attribute=rotationPPAttr,query=True,particleId=pid)
except RuntimeError :
print "No rotation Attr found :" + str(pid)
dorotate = 0
#get meshes
listmesh = cmds.listConnections(inst[0]+'.inputHierarchy')
checkAttr = cmds.objExists(Psel[0]+"."+meshIDAttr)
meshID = [0]
instName = ''
# paricle ID and mupltiple meshes
#--------------------------------
if checkAttr == True:
try:
meshID = cmds.nParticle(Psel[0],attribute=meshIDAttr,query=True,particleId=pid)
meshID = [int(z) for z in meshID]
except RuntimeError:
print " Emty ID Attr"
print "using meshID 0 instead"
meshID = [0]
mesh = meshID[0]
try:
instName = listmesh[mesh]+"_"+str(pid)
checkGeo = cmds.objExists(instName)
#-----------------------------------------------------------------------
#duplicate
#-----------------------------------------------------------------------
if checkGeo== False:
print "duplicate"
instGeo = cmds.duplicate(listmesh[mesh],name=instName,renameChildren=True )
cmds.setAttr(instGeo[0]+".visibility", 1)
cmds.parent( instGeo[0], baked )
except IndexError :
print "No Mesh for particleID found:" + str(pid)
print "using meshID 0 instead"
instName = listmesh[0]+"_"+str(pid)
checkGeo = cmds.objExists(instName)
#duplictae
if checkGeo== False:
print "Duplicate 2:"
instGeo = cmds.duplicate(listmesh[0],name=instName,renameChildren=True )
cmds.setAttr(instGeo[0]+".visibility", 1)
cmds.parent( instGeo[0], baked )
#collect simple Geo
#-----------------------------------------------------------------------
if DoSimplify == True:
simpleGeo.insert(0,instName)
#-----------------------------------------------------------------------
#set keyframes
#-----------------------------------------------------------------------
cmds.setKeyframe(instName, v=positionPP[0], at='translateX', time=curFrame)
cmds.setKeyframe(instName, v=positionPP[1], at='translateY', time=curFrame)
cmds.setKeyframe(instName, v=positionPP[2], at='translateZ', time=curFrame)
if doscale == 1:
cmds.setKeyframe(instName, v=sizePP[0], at='scaleX', time=curFrame)
try:
cmds.setKeyframe(instName, v=sizePP[1], at='scaleY', time=curFrame)
cmds.setKeyframe(instName, v=sizePP[2], at='scaleZ', time=curFrame)
except:
#a scalar value was used, not a vector
cmds.setKeyframe(instName, v=sizePP[0], at='scaleY', time=curFrame)
cmds.setKeyframe(instName, v=sizePP[0], at='scaleZ', time=curFrame)
if dorotate == 1:
cmds.setKeyframe(instName, v=rotationPP[0], at='rotateX', t=curFrame)
cmds.setKeyframe(instName, v=rotationPP[1], at='rotateY', t=curFrame)
cmds.setKeyframe(instName, v=rotationPP[2], at='rotateZ', t=curFrame)
#sets visibility
cmds.setKeyframe(instName, f=1, at='visibility', t=curFrame)
#sets visibility to zero before particle birth
firstkey = cmds.keyframe(instName+'.visibility',index=(0,0),query=True)
turnoff = firstkey[0]-1
cmds.setKeyframe(instName, f=0, at='visibility', t=turnoff)
cmds.keyframe(instName+'.visibility',edit=True,index=(0,0),timeChange=turnoff,valueChange=0)
cmds.clearCache( all=True )
cmds.flushUndo()
if cmds.progressBar(gMainProgressBar, query=True, isCancelled=True ) :
break
#update progressBAR
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
for s in range(len(simpleGeo)):
cmds.simplify( simpleGeo[s] )
cmds.clearCache( all=True )
cmds.flushUndo()
#update progressBAR
cmds.progressBar(gMainProgressBar, edit=True, step=1)
#update progressBAR
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)
#----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------
#window creation
#----------------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------------
window = cmds.window('Bakenator3')
cmds.columnLayout()
gMainProgressBar = cmds.progressBar(maxValue=rangeF,isInterruptable=True,width=550)
cmds.textFieldGrp( 'meshid',label='MeshID int Attr:', text="meshID",editable=True)
cmds.textFieldGrp( 'rotation',label='rotation vector Attr:', text="rotationPP",editable=True)
cmds.textFieldGrp( 'size',label='size vector Attr:', text="sizePP",editable=True)
cmds.textFieldGrp( 'jump',label='keyframe every (frame):', text=1,editable=True)
cmds.checkBoxGrp( 'simple', numberOfCheckBoxes=1, label='', labelArray3=['Simplify Animation Curve', '', ''] )
cmds.button( label='BAKE Instance NOW !!', command=bake )
cmds.showWindow( window )
#update progressBAR
cmds.progressBar(gMainProgressBar, edit=True, endProgress=True)