def draw(self, agent):
if len(agent.neighbors) > 0:
for neighbor in agent.neighbors:
if self.shouldIMakeIt(agent.point, neighbor):
self.lineIDs.append(rs.AddCurve([agent.point, neighbor], 1))
self.startPts.append( agent.point )
self.endPts.append( neighbor )
"""for line in self.lineIDs:
self.pipeIDs.append( rs.AddPipe(line, 0, .08 ))"""
for line in self.lineIDs:
rs.ObjectColor(line, [(rs.CurveLength(line)*2),(rs.CurveLength(line)*1.5),(rs.CurveLength(line)*3)])
def shouldIMakeIt(self, startPt, endPt):
for i in range(len(self.lineIDs)):
otherLineStartPt = self.startPts[i]
otherLineEndPt = self.endPts[i]
if otherLineStartPt == endPt and otherLineEndPt == startPt :
return False
return True
"""initialize the class - pass it the point coordinates (POINT),
and the initial distance to reach out to neighbors(THRESHOLD)"""
def __init__(self, POINT, THRESHOLD, RESETTHRESHOLD, INDEX, GUID, STRENGTH):
self.point = POINT
self.positions = []
self.threshold = THRESHOLD
self.startThres = THRESHOLD
self.resetthreshold = RESETTHRESHOLD
self.strength = STRENGTH
self.resetStrength = STRENGTH
self.neighborstrength = []
#create empty list within object to store
self.myIndex = INDEX
self.id = GUID
self.neighbors = []
self.neighborLine = "empty"
self.neighborLines = []
self.neighborIndexes = []
self.moveVecs = []
self.MyTrail = "empty"
for neighbor in self.neighbors:
NewVec = rs.VectorCreate(self.point, neighbor)
Dist = rs.Distance(self.point, neighbor)
if Dist < self.resetthreshold:
#unitize the vector so we just have direction
NewVec = rs.VectorUnitize(NewVec)
#scale the vector based on the neighbor's strength
if NewVec == None:
NewVec = [0,0,0]
NewVec = rs.VectorScale(NewVec,((Dist * (self.neighborstrength[self.neighbors.index(neighbor)]))-Dist))
#start toataling (to get average) of the vectors and the dists
MoveVec = rs.VectorAdd(MoveVec, NewVec)
#########################################################################
#MAIN FUNCTION
#########################################################################
def Main(thres, resetthresh):
myMuscleLines = MuscleLines()
#get starting points
POINTS=rs.GetLayer("select layer")
listOfPoints = rs.ObjectsByLayer(POINTS)
ATTRACTORS= rs.GetLayer("select layer")
listOfAttractors = rs.ObjectsByLayer(ATTRACTORS )
#create empty list for all agents
listOfAgents = []
#ASSIGN EACH POINT AS AN INSTANCE OF MUSCLE CLASS
for i in range(len(listOfPoints)):
myMuscle = Muscle(rs.PointCoordinates(listOfPoints[i]), thres, resetthresh, i, listOfPoints[i], 1 )
#append that instance to list of agents
listOfAgents.append(myMuscle)
#ASSIGN ATTRACTORS
for attractor in listOfAttractors:
for myAgent in listOfAgents:
SDist = rs.Distance(myAgent.point, rs.PointCoordinates(attractor))
if SDist < (thres*6):
myAgent.strength = myAgent.strength - ((thres/SDist) * .10)
Iter = 20
for y in range(Iter):
rs.EnableRedraw(False)
myMuscleLines.cleanUp()
for myAgent in listOfAgents:
myAgent.cleanUp()
listOfPosNeighbors = listOfAgents
for otherAgent in listOfPosNeighbors:
if myAgent.myIndex != otherAgent.myIndex:
if dist < myAgent.threshold:
myAgent.neighbors.append(otherAgent.point)
myAgent.neighborstrength.append(otherAgent.strength)
myAgent.neighborIndexes.append(listOfPosNeighbors.index(otherAgent))
myMuscleLines.draw(myAgent)
myAgent.Trail()
if y != (Iter-1):
for myAgent in listOfAgents:
myAgent.MovePoint(listOfAgents)
myAgent.positions.append(myAgent.point)
rs.EnableRedraw(True)
#Run it with the 1. lower threshold(how far we reach out) and upper threshold (when new agent are created)
Main(22,29)
错误:Message: Could not convert None to a list of points 作者: Jorin 时间: 2013-10-23 13:05
将两组点放到不同图层执行楼主这段代码,发现以下问题:
第161行 rs.Distance要对比两组点,但是没办法把点和空值进行对比,所以出现了Could not convert None to a list of points
原因是rs.PointCoordinates(attractor)返回了空值。
我在这句前面加一句if rs.IsPoint(attractor):print attractor 测试了一下,print attractor没有执行,说明attractor不是点物件,当然PointCoordinates(attractor)就没有返回值了。作者: 陈志伟o0 时间: 2013-10-23 21:07