Saturday, June 13, 2009

this is my studio script (using classes). i wrote some more helping scripts in the beginning.

in the maya scene should be a few locators (which are attraction and repulsion points)


import maya.cmds as cmds
import maya.mel as mm
import math
import sys
from random import*
from setColor import*
sys.setrecursionlimit(8000)

############################################################################################################################

def centerOfFace(facet):
#find the vertices that define that face.
vertex = cmds.polyListComponentConversion(facet, ff=1, tv=1)
cmds.select(vertex, r=1)
vertexFlat = cmds.ls(sl=1, fl=1)

#find out how many vertices define that face
vertCount = len(vertexFlat)
#print vertexFlat

#for each vertex go through and find it's world space position.
vertPositionSumX = 0.
vertPositionSumY = 0.
vertPositionSumZ = 0.
for a in range(0, vertCount, 1):
coordinate = cmds.pointPosition(vertexFlat[a], w=1)
vertPositionSumX += coordinate[0]
vertPositionSumY += coordinate[1]
vertPositionSumZ += coordinate[2]

centroidX = vertPositionSumX/float(vertCount)
centroidY = vertPositionSumY/float(vertCount)
centroidZ = vertPositionSumZ/float(vertCount)

return [centroidX, centroidY, centroidZ]

def getDistance(start, end):
#start and end must be lists xyz
v = [start[0]-end[0], start[1]-end[1], start[2]-end[2]] #list format x,y,z
vector = "<<" + str(v[0]) + "," + str(v[1]) + "," + str(v[2]) + ">>"
mag = mm.eval("mag " + vector + ";")

return mag

def closestLocator (locator, netOfLocs):
locatorPos = cmds.pointPosition(locator)
distance = 1000000000
closLocData = []
closestLocator = ""
for i in netOfLocs:
locPos = cmds.pointPosition(i)
currDist = getDistance(locatorPos, locPos)
if currDist < distance:
distance = currDist
closLocator = i
closLocPos = locPos
closLocDist = currDist
closLocData.append (closLocator)
closLocData.append (closLocPos)
closLocData.append (closLocDist)

return closLocData

def closestLocatorToPoint (Point, netOfLocs):

locatorPos = Point
distance = 1000000000
closLocData = []
closestLocator = ""
for i in netOfLocs:
locPos = cmds.pointPosition(i)
currDist = getDistance(locatorPos, locPos)
if currDist < distance:
distance = currDist
closLocator = i
closLocPos = locPos
closLocDist = currDist
closLocData.append (closLocator)
closLocData.append (closLocPos)
closLocData.append (closLocDist)

return closLocData

def midPoint (listPt1,listPt2):
x=0
y=0
z=0
n=0
for i in listPt1:
ptPos = cmds.pointPosition(i)
x=x+ptPos[0]
y=y+ptPos[1]
z=z+ptPos[2]
n=n+1
for i in listPt2:
ptPos = cmds.pointPosition(i)
x=x+ptPos[0]
y=y+ptPos[1]
z=z+ptPos[2]
n=n+1
midX = x/n
midY = y/n
midZ = z/n
midPt=[]
midPt.append (midX)
midPt.append (midY)
midPt.append (midZ)

return midPt

def midPointInPtLocs (listLoc1,listPt2):
x=0
y=0
z=0
n=0
for i in (1,len(listLoc1)-1,1):
ptPos = cmds.pointPosition(listLoc1[i])
x=x+ptPos[0]
y=y+ptPos[1]
z=z+ptPos[2]
n=n+1
ptPos = listPt2
x=x+(ptPos[0]*len(listLoc1))
y=y+(ptPos[1]*len(listLoc1))
z=z+(ptPos[2]*len(listLoc1))
n=n+len(listLoc1)
midX = x/n
midY = y/n
midZ = z/n
midPt=[]
midPt.append (midX)
midPt.append (midY)
midPt.append (midZ)

return midPt

def direction(a,b):
"Returns the direction vector going from a to b"
# Calculate direction vector
dire = [ a[0] - b[0], a[1] - b[1], a[2] - b[2] ] # a-b
return dire

############################################################################################################################

class graves:
def __init__(self):
print "Instance of class Graves created."

def getData(self):
locs = cmds.filterExpand(sm=22)
return locs

def getRepulsiveLocs (self):
locs1 = cmds.filterExpand(sm=22)
return locs1

def getAllLocators (self, AmountOfAttractors):
repulciveLocs = cmds.filterExpand(sm=22)

locs =[]
for i in range (AmountOfAttractors):
L = repulciveLocs[0]
repulciveLocs.remove (L)
locs.append (L)
return locs, repulciveLocs

def polyCubesInLocators (self, locators, size):
cubes = []
for i in locators:
position = cmds.pointPosition(i)
cube = cmds.polyCube (w =size ,h=size ,d=size )
cmds.move (position[0], position[1], position[2], cube)
cubes.append (cube)

return cubes

def makeNet (self, locators, numRow, numCell):
allLocs = []
startLocs = []
x = randint (98,102)
x = x/100.0
for i in range (numRow):
for j in range (numCell):
newloc= cmds.spaceLocator( p=(j*10*x, i*10*x, 0) )
if i == 0:
startLocs.append (newloc)
elif i == (numRow-1):
startLocs.append (newloc)
else:
allLocs.append (newloc)
cmds.refresh()
return startLocs, allLocs

def changeNetBecauseRepulsive (self, allLocs, repulsiveLocs, repSize, pointsAmount = 4, N=0):
Empty =[]
LOCS = allLocs
for i in allLocs:
Empty.append (i)
C = pointsAmount

for k in repulsiveLocs:
for i in range (repSize):
L=(repSize/5)*(i+1)
for j in range (L):
Kpos = cmds.pointPosition(k)
closLoc = closestLocatorToPoint(Kpos, LOCS)
LOCpos = closLoc[1]
clLoc = closLoc[0]
direc = direction(LOCpos, Kpos)
newX = ((direc[0]*6)/((i+1)*(i+1)))
newY = ((direc[1]*6)/((i+1)*(i+1)))
newZ = ((direc[2]*6)/((i+1)*(i+1)))
cmds.move (newX, newY, newZ, clLoc)
LOCS.remove (clLoc)

cmds.refresh()
return Empty

def subdivStartLocs (self, startLocs, locs):
subStartLocs = []
lenLocs = len(locs)
lenStertLocs = len(startLocs)
locsPerUnit = lenStertLocs/lenLocs
for i in range (lenLocs):
groupOfLocs = []
groupOfLocs.append (locs[i])
for j in range (locsPerUnit):
numOfLoc = i*locsPerUnit+j
loccc = startLocs [numOfLoc]
loccc = loccc[0]
groupOfLocs.append (loccc)
subStartLocs.append (groupOfLocs)

return subStartLocs

def looping1 (self, subdivStartLocs, allLocs, locs, size, number, cubes):
#cmds.pause( sec=0.5 )
cmds.refresh()
size *= 1.03
color = 255/(size*10)
number = number + 1
print "number"
print number
temploryList = []
midPoints =[]
N=0
for i in subdivStartLocs:
if number == 1:
Pt1 = len(i)/2
midP = i[Pt1]
midP = cmds.pointPosition(midP)
remLocs = []
remLocs.append (i[0])
x = randint (2,len(i)+7)
else:
midP = i
remLocs = []
#remLocs.append (i)
x = randint (2,len(subdivStartLocs)+7)
n = 0
if len(allLocs)>x:
for g in range (x):
closLocData = closestLocatorToPoint (midP, allLocs)
closLocator = closLocData[0]
closLocPos = closLocData[1]
closLocDist = closLocData[2]

newSphere = cmds.polySphere (r=size, sx=4, sy=4)
cmds.move (closLocPos[0], closLocPos[1], closLocPos[2], newSphere)
remLocs.append (closLocator)
allLocs.remove (closLocator)
if len(allLocs)<2:
return "Done."

temploryList.append (remLocs)

#################################################################
if number == 1:
Pt1 = len(i)/2
midPt1 = i[Pt1]
midPt1 = cmds.pointPosition(midPt1)
ptPos = cmds.pointPosition(i[0])
midPt = midPoint (remLocs,i)
else:
ptPos = cmds.pointPosition(locs[N])
midPt = midPointInPtLocs (remLocs,i)

midX = midPt[0] - midP[0]
midY = midPt[1] - midP[1]
midX = ptPos[0] + (midX/2)
midY = ptPos[1] + (midY/2)
cmds.move (midX, midY, 0, cubes[N])
cmds.move (midX, midY, 0, locs[N])
midP = midPt
N = N+1
midPoints.append (midP)
##################################################################

subdivStartLocs = midPoints

if len(allLocs)>0:
self.looping1 (subdivStartLocs, allLocs, locs, size, number, cubes)
else:
return "Done."
this part is running the script in way like on the video above

g= graves()

locs, repulsiveLocs = g.getAllLocators (8)
cubes = g.polyCubesInLocators ( locs, 6)
cubes1 = g.polyCubesInLocators ( repulsiveLocs, 10)

startLocs, allLocs = g.makeNet(locs,48,48)
cubes2 = g.polyCubesInLocators ( startLocs, 1)
allLocsPPP = allLocs


ALLLOCS = g.changeNetBecauseRepulsive (allLocsPPP, repulsiveLocs, 13)


subd = g.subdivStartLocs(startLocs,locs)
g.looping1 (subd, ALLLOCS, locs, 1.2, 0, cubes)

No comments:

Post a Comment