Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Seperate objects v2
- from pymel.core import*
- import maya.api.OpenMaya as om
- import random as r
- import math
- class defAttr:
- type = "2d"
- stepSize = .01
- itterAmnt = 100
- buffer = .5
- smallInCenter = 1
- 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
- sumS = om.MVector(0,0,0)
- r.shuffle(list)
- for thing in list:
- dist = getDist(self.pos, thing.pos)
- #print dist
- if dist == 0:
- pass
- if dist < thing.radius+self.radius * defAttr.buffer:
- diff = self.pos - thing.pos
- om.MVector.normalize(diff)
- sumS += diff
- sepCount += 1
- if defAttr.type == "2d":
- diff = om.MVector(self.pos[0],0, self.pos[2]) - om.MVector(thing.pos[0],0,thing.pos[2])
- if sepCount:
- self.dir += om.MVector(sumS)#*thing.radius
- om.MVector.normalize(self.dir)
- self.dir *= defAttr.stepSize # * abs(self.radius)
- #self.dir /= thing.radius
- self.pos += self.dir
- move(self.thingObj, self.pos)
- sl = selected()
- slList = []
- for s in sl:
- sepObj = thing(s)
- slList.append(sepObj)
- seperateObjects(slList)
Advertisement
Add Comment
Please, Sign In to add comment