-
Notifications
You must be signed in to change notification settings - Fork 1
/
proceduralCloudsApp.py
275 lines (193 loc) · 7.31 KB
/
proceduralCloudsApp.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import maya.cmds as cmds
import maya.mel as mel
class ProceduralClouds(object):
def __init__(self):
#load nearestPointOnMesh in case it isn't
self.loadPlugin()
self.cloudsGeo = self.getAllCloudsGeo()
self.emitters = []
def mainClouds(self):
print self.cloudsGeo
for cloud in self.cloudsGeo:
cloudShape = cmds.listRelatives(cloud, s=True, f=True)[0]
#get its BoundingBox
BB = self.getBB(cloud)
#create3DFluid
fluidShape = self.create3DFluid(BB, cloud)
#initialConfig
self.initialFluidsConfig(fluidShape)
#move Fluid to Geo
self.toPos(cloud, fluidShape)
#connect Closest Node to cloud
closNode = self.closestNode(cloudShape)
#fill
self.fillWVoxels(cloud, fluidShape, closNode)
#deleteClosNode
self.deleteClosestNode(closNode)
cmds.setAttr(cloudShape + '.visibility', 0)
def mainRain(self):
for cloud in self.cloudsGeo:
#duplicateMesh
dupCloud = cmds.duplicate(cloud, n=cloud + '_dupnParticles')[0]
#get rainyfaces
faces = self.getRainyFaces(dupCloud)
#delete others
self.deleteFaces(dupCloud, faces)
#createParticlezz
self.createParticles(dupCloud)
def loadPlugin(self):
if cmds.pluginInfo('nearestPointOnMesh.mll', q=True, l=True) == False:
cmds.loadPlugin('nearestPointOnMesh.mll')
def initialFluidsConfig(self, fluidShape):
'''
'''
cmds.setAttr(fluidShape + ".squareVoxels", 1)
cmds.setAttr(fluidShape + ".densityMethod", 2)
cmds.setAttr(fluidShape + ".selfShadowing", 1);
cmds.setAttr(fluidShape + ".velocityDamp", 0.3);
def closestNode(self, geo):
'''
'''
closestNode = cmds.shadingNode('nearestPointOnMesh', asUtility=True, n=geo + '_closest')
cmds.connectAttr(geo + '.worldMesh', closestNode + '.inMesh')
return closestNode
def deleteClosestNode(self, node):
cmds.delete(node)
def getAllCloudsGeo(self):
'''
'''
_cloudsGeo = []
for mesh in cmds.ls(et='mesh'):
isValidCloud = False
if mesh.lower().find('nube') != -1:
isValidCloud = True
if mesh.lower().find('_dupnParticles') != -1:
isValidCloud = False
meshParent = cmds.listRelatives(mesh, p=True)[0]
children = cmds.listRelatives(meshParent, c=True)
if type(children) == list:
for child in children:
if child.find('fluid') != -1:
isValidCloud = False
if isValidCloud == True:
cmds.setAttr(mesh + '.visibility', 1)
_cloudsGeo.append(meshParent)
return _cloudsGeo
def parentTo(src, dest):
parent = cmds.parent(src, dest)
return parent
def getBB(self, geo):
'''
'''
BBSum = []
BB = cmds.xform(geo, q=True, boundingBox = True)
for x in xrange(0, (len(BB) - 3)):
axisSum = BB[x + 3] - BB[x]
axisSum = axisSum * 1
axisSum = round(axisSum, 0)
BBSum.append(axisSum)
return BBSum
def toPos(self, geo, fluidShape):
'''
'''
fluidParent = cmds.listRelatives(fluidShape, p=True, f=True)
cons = cmds.pointConstraint(geo, fluidParent, mo=False)
cmds.delete(cons)
def create3DFluid(self, BB, geo):
multiplier = 1
BBRes = [i * multiplier for i in BB]
fluidShape = mel.eval('create3DFluid %s %s %s %s %s %s;' % (BBRes[0], BBRes[1], BBRes[2], BB[0], BB[1], BB[2]))
fluid = cmds.listRelatives(fluidShape, p=True)[0]
cmds.parent(fluid, geo)
return fluidShape
def fillWVoxels(self, geo, fluidShape, closNode):
resX, resY, resZ = cmds.getAttr(fluidShape + '.resolution')[0]
for x in range(resX):
for y in range(resY):
for z in range(resZ):
voxelCenter = cmds.fluidVoxelInfo(fluidShape, voxelCenter = True, xi=x, yi=y, zi=z)
cmds.setAttr(closNode + '.inPositionX', voxelCenter[0])
cmds.setAttr(closNode + '.inPositionY', voxelCenter[1])
cmds.setAttr(closNode + '.inPositionZ', voxelCenter[2])
closestPtX = cmds.getAttr(closNode + '.positionX')
closestPtY = cmds.getAttr(closNode + '.positionY')
closestPtZ = cmds.getAttr(closNode + '.positionZ')
closPos = [closestPtX, closestPtY, closestPtZ]
ClosPos = [a - b for a, b in zip(voxelCenter, closPos)]
normalX = cmds.getAttr(closNode + '.normalX')
normalY = cmds.getAttr(closNode + '.normalY')
normalZ = cmds.getAttr(closNode + '.normalZ')
normals = [normalX, normalY, normalZ]
dotProduct = sum(i*j for i,j in zip(normals, ClosPos))
if dotProduct <= 0:
#replace 0.5 with dotPr clamped
density = "setFluidAttr -at \"density\" -ad -fv " + str(0.3) + " -xi " + str(x) + " -yi " + str(y) + " -zi " + str(z) + " " + fluidShape + ";";
mel.eval(density)
cmds.select(fluidShape)
mel.eval('doSetFluidState 1 { "1", 1, 1, 1, 1, 1, 1 };')
def getRainyFaces(self, geo):
tolerance = 60
axis = (0, -1, 0)
cmds.select(geo + ".f[*]")
cmds.polySelectConstraint(mode = 3, type = 8, orient = 2, orientaxis = axis, orientbound = (0, tolerance))
cmds.polySelectConstraint(dis=True)
sel = cmds.ls(sl=True, fl=True)
BB = cmds.xform(geo, q=True, ws=True, bb=True)
threshold = .25
BBYaxis = (BB[4] - BB[1]) * threshold
BBThreshold = BBYaxis + BB[1]
faces = cmds.polyEvaluate(geo, f=True)
listFaces = []
listFacesInv = []
for face in sel:
if cmds.xform(face, q=True, ws=True, t=True)[1] < BBThreshold:
listFaces.append(face)
for f in xrange(0, faces):
stringFace = geo + '.f[' + str(f) + ']'
if stringFace not in listFaces:
listFacesInv.append(stringFace)
return listFacesInv
def deleteFaces(self, geo, listFaces):
cmds.delete(listFaces)
cmds.delete(geo, ch=True)
cmds.select(cl=True)
cmds.setAttr(geo + '.visibility', 0)
def createParticles(self, geo):
emitterVar = cmds.emitter(geo, type='surface', r=1)[0]
nParticleVar = cmds.nParticle()[0]
cmds.connectDynamic(nParticleVar, em=emitterVar)
nParticleShape = cmds.listRelatives(nParticleVar, s=True)[0]
self.tryAddingDefaultArgs(nParticleShape)
cmds.setAttr(nParticleShape + '.enableSPH', 1)
cmds.setAttr(nParticleShape + '.incompressibility', 1)
cmds.setAttr(nParticleShape + '.restDensity', 2)
cmds.setAttr(nParticleShape + '.radiusScaleSPH', 0)
cmds.setAttr(nParticleShape + '.viscosity', 0.1)
cmds.setAttr(nParticleShape + '.threshold', 0.001)
cmds.setAttr(nParticleShape + '.blobbyRadiusScale', 0.01)
cmds.setAttr(nParticleShape + '.motionStreak', 8)
cmds.setAttr(nParticleShape + '.meshTriangleSize', 0.001)
cmds.setAttr(nParticleShape + '.maxTriangleResolution', 300)
cmds.setAttr(nParticleShape + '.particleRenderType', 6)
cmds.setAttr(nParticleShape + '.tailSize', 9.5)
return emitterVar, nParticleVar
def tryAddingDefaultArgs(self, nParticleShape):
commands = ['addAttr -is true -ln "colorAccum" -at bool -dv false %s;'
,'addAttr -is true -ln "useLighting" -at bool -dv false %s;'
,'addAttr -is true -ln "lineWidth" -at long -min 1 -max 20 -dv 1 %s;'
,'addAttr -is true -ln "tailFade" -at "float" -min -1 -max 1 -dv 0 %s;'
,'addAttr -is true -ln "tailSize" -at "float" -min -100 -max 100 -dv 1 %s;'
,'addAttr -is true -ln "normalDir" -at long -min 1 -max 3 -dv 2 %s;'
]
for command in commands:
try:
mel.eval(command % nParticleShape)
except:
pass
def setHierarchy(self):
pass
def masterLoc(self):
loc = cmds.spaceLocator()
clouds = ProceduralClouds()
clouds.mainRain()
clouds.mainClouds()