def _create_particle(self,value,*argv): global n_particle_create pmel.emitter( r=value,dx=1, dy=0, dz=0, sp=0, nsp=0, n='myRandomEmitter' ,type='surface') n_particle_create = pmel.nParticle( n='emittedParticles' ) n_particle_create[1].maxCount.set(value) nucleus = pmel.listConnections(n_particle_create,t='nucleus') nucleus[0].gravity.set(0) pmel.connectDynamic( 'emittedParticles', em='myRandomEmitter') self.list_emitter_delete.append('myRandomEmitter') self.list_emitter_delete.append(n_particle_create) self.list_emitter_delete.append(nucleus)
def _initialize(self,args): #Initialize partTarget = self.inputBar.getText() partNameBase = self.inputName.getText() if partTarget=='' or partNameBase=='': pm.error("Must input both fields to continue...") partName = partNameBase + '_NPARTICLE' partNucleus = partNameBase + '_NUCLEUS' partEmitter = partNameBase + '_EMITTER' #First thing we will do is Emit from Surface with an Emiter self.emitter = pm.emitter(partTarget, type='surface',name=partEmitter,r=5000,sro=0,nuv=0,cye='none',cyi=1,spd=0,srn=0,nsp=0,tsp=0,mxd=0,mnd=0,dx=1,dy=0,dz=0,sp=0) emitter=self.emitter self.nparticle = pm.nParticle(n=partName) npart=self.nparticle if '2012' in str(pm.versions.current()): import maya.cmds as cmds self.nucleus = cmds.listConnections(self.nparticle[1].name(), type='nucleus')[0] cmds.setAttr((self.nucleus+'.gravity'), 0) cmds.setAttr((self.nucleus+'.startFrame'), pm.playbackOptions(q=True,ast=True)) cmds.rename(self.nucleus, partNucleus) self.nucleus=partNucleus elif '2015' in str(pm.versions.current()) or '2014' in str(pm.versions.current()): self.nucleus=pm.PyNode(set(npart[1].listConnections(type='nucleus')).pop()) nucleus=self.nucleus nucleus.rename(partNucleus) nucleus.gravity.set(0) nucleus.startFrame.set(pm.playbackOptions(q=True,ast=True)) else: pm.error("This maya version is unsupported by this script") #Set the Nucleus to no gravity + start time to first frame pm.currentTime( pm.playbackOptions(q=True,ast=True), e=True ) pm.connectDynamic(npart, em=emitter) #Set up the nParticle: #add per particle attributes vattrs = ['posPP', 'rotPP', 'scalePP'] for vattr in vattrs: npart[1].addAttr(vattr, k=1, dt='vectorArray') npart[1].addAttr(vattr+'0', dt='vectorArray') fattrs = ['indexPP'] for fattr in fattrs: npart[1].addAttr(fattr, k=1, dt='doubleArray') npart[1].addAttr(fattr+'0', k=1, dt='doubleArray') #add dynamic expressions ala Vicky Osborne self._dynExpr() #Change shading type to points npart[1].particleRenderType.set(3)
def createParticleEmitter(self, meshEmitter, collider): # Force nParticle balls at creation pm.optionVar(sv=("NParticleStyle", "Balls")) self.particle, self.partShape = pm.nParticle(n=str(meshEmitter) + "_particle") # Add attribute in particleShape pm.addAttr(self.partShape, ln="indexPP", dt="doubleArray") pm.addAttr(self.partShape, ln="rotatePP", dt="vectorArray") pm.addAttr(self.partShape, ln="scalePP", dt="vectorArray") pm.addAttr(self.partShape, ln="rgbPP", dt="vectorArray") pm.addAttr(self.partShape, ln="fixPosPP", dt="vectorArray") pm.addAttr(self.partShape, ln="opacityPP", dt="doubleArray") pm.addAttr(self.partShape, ln="typePP", dt="doubleArray") self.nameEmitter = str(meshEmitter) + "_emitter" pm.emitter(meshEmitter, type="surface", name=self.nameEmitter, r=float(self.ui.simulationEmit_le.text())) pm.connectDynamic(self.partShape, em=self.nameEmitter) # Used maya command because pymel crash when find nucleus node self.nucleusName = mel.eval('listConnections -type "nucleus" ' + self.partShape + ";")[0] pm.parent(self.partShape, self.worldParent) pm.parent(self.nameEmitter, self.worldParent) pm.parent(self.nucleusName, self.worldParent) self.setParamaters(self.partShape, self.particleParameter) self.setParamaters(self.nameEmitter, self.emitterParameter) self.setParamaters(self.nucleusName, self.nucleusParameter) pm.addAttr(self.partShape, ln="radiusPP", dt="doubleArray") # Create Rigid pm.select(collider, r=1) pm.runtime.nClothMakeCollide(collider) self.nrigid = pm.listConnections(collider.listRelatives(s=1, c=1)[0], type="nRigid")[0] self.setParamaters(self.nrigid.listRelatives(s=1, c=1)[0], self.rigidParameter) pm.parent(self.nrigid, self.worldParent) self.nrigid.setAttr("v", 0) # Create instancer self.instancer = pm.particleInstancer( self.partShape, a=True, object=self.listInstance, n=str(meshEmitter) + "_instancer", cycle="sequential", age="indexPP", rotation="rotatePP", scale="scalePP", visibility="opacityPP", ) pm.parent(self.instancer, self.worldParent) # Create proc Colision expression = """ global proc forestGeneratorEvent(string $particleObject,int $particleId, string $geometryObject) { vector $rgb = `nParticle -attribute rgbPP -id $particleId -q $particleObject`; if ($rgb != << 1,1,1 >>) { nParticle -e -attribute rgbPP -id $particleId -vv 0 1 0 $particleObject; } vector $pos = `nParticle -attribute position -id $particleId -q $particleObject`; vector $lastPos = `nParticle -attribute lastPosition -id $particleId -q $particleObject`; nParticle -e -attribute opacityPP -id $particleId -fv 1 $particleObject; if (mag($pos - $lastPos) >= 10 && $rgb != << 1,1,1 >>){ nParticle -e -attribute lifespanPP -id $particleId -fv 0 $particleObject; } }""" pm.expression(s=expression, n="forestGenerator_exp") # Create Colision event pm.event(self.partShape, die=0, count=0, proc="forestGeneratorEvent")
def updateRadius(self): if self.ui.updateRadiusBy_le.text() == "": return reduce = float(self.ui.updateRadiusBy_le.text()) for x in self.partShape.getAttr("id"): t = pm.nParticle(self.partShape, q=1, at="typePP", id=x)[0] r = pm.nParticle(self.partShape, q=1, at="radiusPP", id=x)[0] pos = pm.nParticle(self.partShape, q=1, at="position", id=x) pm.nParticle(self.partShape, e=1, at="fixPosPP", vv=(pos[0], pos[1], pos[2]), id=x) p = pm.nParticle(self.partShape, q=1, at="fixPosPP", id=x) if t == self.ui.updateRadiusType_cb.currentIndex(): if self.ui.updateRadiusOp_cb.currentText() == "Divide": nR = r / reduce elif self.ui.updateRadiusOp_cb.currentText() == "Multiply": nR = r * reduce pm.nParticle(self.partShape, e=1, at="fixPosPP", vv=(p[0], p[1] - (r - nR), p[2]), id=x) pm.nParticle(self.partShape, e=1, at="radiusPP", fv=nR, id=x) print p, pm.nParticle(self.partShape, q=1, at="fixPosPP", id=x)
# # Model of E. coli # # Spatial Scale: 1 micron # Simulation Engine: Nucleus # import pymel.core as pm import maya.cmds as mc import maya.mel as mel pm_parts = pm.nParticle( p=[(0, 0, -6), (0, 0, 6)], n="parts") # protects against multiple objects called "parts" parts_nm = pm_parts[1].name() p_shape = mc.select(parts_nm) # returns name (string) mc.addAttr(ln="radiusPP", dt="doubleArray", w=True, r=True) mc.setAttr("%s.selfCollide"%parts_nm,1) #blobby surface mc.setAttr("%s.particleRenderType"%parts_nm,7) #make plane mc.select("nucleus1") mc.setAttr("nucleus1.usePlane",1) mc.select(parts_nm) mc.addAttr(ln="rgbPP", dt="vectorArray", w=True, r=True)
def createParticleEmitter(self, meshEmitter, collider): # Force nParticle balls at creation pm.optionVar(sv=("NParticleStyle", "Balls")) self.particle, self.partShape = pm.nParticle(n=str(meshEmitter) + "_particle") #Add attribute in particleShape pm.addAttr(self.partShape, ln="indexPP", dt="doubleArray") pm.addAttr(self.partShape, ln="rotatePP", dt="vectorArray") pm.addAttr(self.partShape, ln="scalePP", dt="vectorArray") pm.addAttr(self.partShape, ln="rgbPP", dt="vectorArray") pm.addAttr(self.partShape, ln="fixPosPP", dt="vectorArray") pm.addAttr(self.partShape, ln="opacityPP", dt="doubleArray") pm.addAttr(self.partShape, ln="typePP", dt="doubleArray") self.nameEmitter = str(meshEmitter) + "_emitter" pm.emitter(meshEmitter, type="surface", name=self.nameEmitter, r=float(self.ui.simulationEmit_le.text())) pm.connectDynamic(self.partShape, em=self.nameEmitter) #Used maya command because pymel crash when find nucleus node self.nucleusName = mel.eval("listConnections -type \"nucleus\" " + self.partShape + ";")[0] pm.parent(self.partShape, self.worldParent) pm.parent(self.nameEmitter, self.worldParent) pm.parent(self.nucleusName, self.worldParent) self.setParamaters(self.partShape, self.particleParameter) self.setParamaters(self.nameEmitter, self.emitterParameter) self.setParamaters(self.nucleusName, self.nucleusParameter) pm.addAttr(self.partShape, ln="radiusPP", dt="doubleArray") #Create Rigid pm.select(collider, r=1) pm.runtime.nClothMakeCollide(collider) self.nrigid = pm.listConnections(collider.listRelatives(s=1, c=1)[0], type='nRigid')[0] self.setParamaters( self.nrigid.listRelatives(s=1, c=1)[0], self.rigidParameter) pm.parent(self.nrigid, self.worldParent) self.nrigid.setAttr("v", 0) #Create instancer self.instancer = pm.particleInstancer(self.partShape, a=True, object=self.listInstance, n=str(meshEmitter) + "_instancer", cycle="sequential", age="indexPP", rotation="rotatePP", scale="scalePP", visibility="opacityPP") pm.parent(self.instancer, self.worldParent) #Create proc Colision expression = """ global proc forestGeneratorEvent(string $particleObject,int $particleId, string $geometryObject) { vector $rgb = `nParticle -attribute rgbPP -id $particleId -q $particleObject`; if ($rgb != << 1,1,1 >>) { nParticle -e -attribute rgbPP -id $particleId -vv 0 1 0 $particleObject; } vector $pos = `nParticle -attribute position -id $particleId -q $particleObject`; vector $lastPos = `nParticle -attribute lastPosition -id $particleId -q $particleObject`; nParticle -e -attribute opacityPP -id $particleId -fv 1 $particleObject; if (mag($pos - $lastPos) >= 10 && $rgb != << 1,1,1 >>){ nParticle -e -attribute lifespanPP -id $particleId -fv 0 $particleObject; } }""" pm.expression(s=expression, n="forestGenerator_exp") #Create Colision event pm.event(self.partShape, die=0, count=0, proc="forestGeneratorEvent")