cwisbg

Seperate objects

Nov 15th, 2016
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.57 KB | None | 0 0
  1. # Seperate objects
  2. from pymel.core import*    
  3. import maya.api.OpenMaya as om
  4. import random as r
  5.  
  6. class defAttr:
  7.     type = "2d"# "3d"  Constrain to y - 0
  8.     stepSize = .1 # how much to move the object
  9.     itterAmnt  = 20 # how many itterations to run.
  10.     buffer = 1 # how much distance between objects + scale
  11. class thing():
  12.     def __init__(self, thingObject):
  13.         self.thingObj = thingObject
  14.         self.pos = om.MVector(thingObject.getTranslation())
  15.         self.lastPos = rVec(1)
  16.         self.dir = om.MVector.normalize(self.pos + rVec(.01))
  17.        
  18.         bounds = xform(thingObject, bbi=1, q=1)
  19.         boundsMin = om.MVector(bounds[0],bounds[1],bounds[2]) + self.pos
  20.         boundsMax = om.MVector(bounds[3],bounds[4],bounds[5]) + self.pos
  21.         generalRadius = om.MVector(boundsMin - boundsMax)
  22.         self.radius = abs((generalRadius[0]+generalRadius[1]+generalRadius[2])/3)
  23.        
  24. def getDist(p1,p2): # distance function
  25.     dist = math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)
  26.     return dist  
  27. def rVec(randAmnt): # generates a random vector
  28.     if defAttr.type == "2d":
  29.         rV = om.MVector(r.uniform(-randAmnt,randAmnt),0,r.uniform(-randAmnt,randAmnt))
  30.     else:
  31.         rV = om.MVector(r.uniform(-randAmnt,randAmnt),r.uniform(-randAmnt,randAmnt),r.uniform(-randAmnt,randAmnt))
  32.     return rV
  33.    
  34. def seperateObjects(list):
  35.     for i in range(defAttr.itterAmnt):
  36.         currentTime(i, u=1)
  37.         for self in list:
  38.             sepCount = -1 #seperation counter
  39.             sumS = om.MVector(0,0,0)
  40.             r.shuffle(list) # to get more random distribution
  41.             for thing in list:
  42.                  dist = getDist(self.pos, thing.pos)
  43.                  if dist >= 0:
  44.                      pass
  45.                  if dist < self.radius* defAttr.buffer:
  46.                      if defAttr.type == "2d":
  47.                          diff = om.MVector(self.pos[0],0,self.pos[2]) - om.MVector(thing.pos[0],0,thing.pos[2])
  48.                      else:
  49.                          diff = self.pos - thing.pos  
  50.                      om.MVector.normalize(diff)  
  51.                      sumS += diff
  52.                      sepCount += 1
  53.             if sepCount:    
  54.                 self.dir += om.MVector(sumS)
  55.                 om.MVector.normalize(self.dir)
  56.                 self.dir *= defAttr.stepSize
  57.                 self.pos += self.dir
  58.                 move(self.thingObj, self.pos)  
  59.                  
  60. sl = selected()
  61. slList = []
  62. for s in sl:
  63.     sepObj = thing(s)
  64.     slList.append(sepObj)
  65.    
  66. seperateObjects(slList)
Add Comment
Please, Sign In to add comment