Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Seperate objects
- from pymel.core import*
- import maya.api.OpenMaya as om
- import random as r
- class defAttr:
- type = "2d"# "3d" Constrain to y - 0
- stepSize = .1 # how much to move the object
- itterAmnt = 20 # how many itterations to run.
- buffer = 1 # how much distance between objects + scale
- class thing():
- def __init__(self, thingObject):
- self.thingObj = thingObject
- self.pos = om.MVector(thingObject.getTranslation())
- self.lastPos = rVec(1)
- self.dir = om.MVector.normalize(self.pos + rVec(.01))
- bounds = xform(thingObject, bbi=1, q=1)
- boundsMin = om.MVector(bounds[0],bounds[1],bounds[2]) + self.pos
- boundsMax = om.MVector(bounds[3],bounds[4],bounds[5]) + self.pos
- generalRadius = om.MVector(boundsMin - boundsMax)
- self.radius = abs((generalRadius[0]+generalRadius[1]+generalRadius[2])/3)
- def getDist(p1,p2): # distance function
- dist = math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 + (p1[2] - p2[2])**2)
- return dist
- def rVec(randAmnt): # generates a random vector
- if defAttr.type == "2d":
- rV = om.MVector(r.uniform(-randAmnt,randAmnt),0,r.uniform(-randAmnt,randAmnt))
- else:
- rV = om.MVector(r.uniform(-randAmnt,randAmnt),r.uniform(-randAmnt,randAmnt),r.uniform(-randAmnt,randAmnt))
- return rV
- def seperateObjects(list):
- for i in range(defAttr.itterAmnt):
- currentTime(i, u=1)
- for self in list:
- sepCount = -1 #seperation counter
- sumS = om.MVector(0,0,0)
- r.shuffle(list) # to get more random distribution
- for thing in list:
- dist = getDist(self.pos, thing.pos)
- if dist >= 0:
- pass
- if dist < self.radius* defAttr.buffer:
- if defAttr.type == "2d":
- diff = om.MVector(self.pos[0],0,self.pos[2]) - om.MVector(thing.pos[0],0,thing.pos[2])
- else:
- diff = self.pos - thing.pos
- om.MVector.normalize(diff)
- sumS += diff
- sepCount += 1
- if sepCount:
- self.dir += om.MVector(sumS)
- om.MVector.normalize(self.dir)
- self.dir *= defAttr.stepSize
- self.pos += self.dir
- move(self.thingObj, self.pos)
- sl = selected()
- slList = []
- for s in sl:
- sepObj = thing(s)
- slList.append(sepObj)
- seperateObjects(slList)
Add Comment
Please, Sign In to add comment