esotericDisciple

sphere packing in Maya optimized

Feb 10th, 2012
745
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pymel.all import *
  2. import maya.OpenMaya as OpenMaya
  3. import maya.mel as mel
  4. import maya.cmds as cmds
  5. import time
  6.  
  7. step = 100
  8. ######################################
  9. def mag(p1,p2):
  10.     return ((p1[0]-p2[0])**2+(p1[1]-p2[1])**2+(p1[2]-p2[2])**2)**.5
  11.  
  12. def fillOptimized():
  13.     if(len(ls(sl=1))):
  14.         obj=cmds.ls(sl=1)[0]
  15.         objList = OpenMaya.MSelectionList()
  16.         objList.add(obj)
  17.         path = OpenMaya.MDagPath()
  18.         objList.getDagPath(0,path)
  19.         path.extendToShape()
  20.        
  21.         closest = OpenMaya.MMeshIntersector()
  22.        
  23.         closest.create(path.node(),path.inclusiveMatrix())
  24.        
  25.         try:
  26.             particleFill(rs=step,maxX=1,maxY=1,maxZ=1,minX=0,minY=0,minZ=0,pd=1,cp=1)
  27.         except:
  28.             pass
  29.         part = cmds.ls(sl=1)[0]
  30.         partShape = cmds.listRelatives(part,shapes=1)[0]
  31.        
  32.         nplist = OpenMaya.MSelectionList()
  33.         nplist.add(partShape)
  34.         npobj = OpenMaya.MObject()
  35.         nplist.getDependNode(0,npobj)
  36.                
  37.         npnode = OpenMaya.MFnDependencyNode(npobj)
  38.        
  39.         nplug = npnode.findPlug("position")
  40.        
  41.         nhdl = nplug.asMDataHandle()
  42.        
  43.         ndata = OpenMaya.MFnVectorArrayData(nhdl.data())
  44.        
  45.         points = OpenMaya.MVectorArray()
  46.        
  47.         points.copy(ndata.array())
  48.      
  49.         delete(part)
  50.      
  51.         rayons=OpenMaya.MDoubleArray()
  52.         for p in range(points.length()):
  53.             mpom = OpenMaya.MPointOnMesh()
  54.             closest.getClosestPoint(OpenMaya.MPoint(points[p]),mpom)
  55.             closestPoint = mpom.getPoint()
  56.             rayons.append(mag(points[p],closestPoint))
  57.      
  58.         newPoints,newRayons = OpenMaya.MVectorArray(),OpenMaya.MDoubleArray()
  59.         while points.length()>0:
  60.             print "%s problem points still left..." % points.length()
  61.             action,p,max,maxId = 0,OpenMaya.MVector(),-1.0,-1
  62.             for i in range(points.length()):
  63.                 if rayons[i]>max:
  64.                     max,p,maxId=rayons[i],points[i],i
  65.             points.remove(maxId)
  66.             rayons.remove(maxId)
  67.             newPoints.append(p)
  68.             newRayons.append(max*1.2)
  69.             i=0
  70.             while i<points.length():
  71.                 if mag(p,points[i])<rayons[i]+max:
  72.                     points.remove(i)
  73.                     rayons.remove(i)
  74.                 else:
  75.                     i=i+1
  76.                    
  77.         #note: the default nParticle creation mode should be set to "points"
  78.                    
  79.         part = cmds.nParticle()[1]
  80.        
  81.         plist = OpenMaya.MSelectionList()
  82.        
  83.         plist.add(part)
  84.        
  85.         newNPObj = OpenMaya.MObject()
  86.        
  87.         plist.getDependNode(0,newNPObj)
  88.        
  89.         newNPNode = OpenMaya.MFnDependencyNode(newNPObj)
  90.        
  91.         newPosPlug = newNPNode.findPlug("position")
  92.        
  93.         newNPHdl = newPosPlug.asMDataHandle()
  94.        
  95.         newposdata = OpenMaya.MFnVectorArrayData(newNPHdl.data())
  96.        
  97.         newposdata.set(newPoints)
  98.        
  99.         newPosPlug.setMObject(newposdata.object())
  100.        
  101.         prt = newNPNode.findPlug("particleRenderType")
  102.        
  103.         prt.setInt(4)
  104.        
  105.         isg = newNPNode.findPlug("ignoreSolverGravity")
  106.        
  107.         isg.setInt(1)
  108.        
  109.         tattr = OpenMaya.MFnTypedAttribute()
  110.        
  111.         rpp = tattr.create("radiusPP","rpp",OpenMaya.MFnData.kDoubleArray)
  112.        
  113.         newNPNode.addAttribute(rpp)
  114.        
  115.         rppPlug = newNPNode.findPlug("radiusPP")
  116.        
  117.         rpphdl = rppPlug.asMDataHandle()
  118.        
  119.         rppdata = OpenMaya.MFnDoubleArrayData(rpphdl.data())
  120.        
  121.         rppdata.set(newRayons)
  122.        
  123.         rppPlug.setMObject(rppdata.object())
  124.        
  125.         print "finished."
  126.     else:
  127.         print("no mesh selected !")
RAW Paste Data