讨论 --- PYthon 的一个小问题

[复制链接]
跳转到指定楼层
21822 大美在前方 发表于 2013-10-23 05:15:23 楼主
代码如下,但是老出现如下错误(代码后面),不知道怎么解决了。。。求高手解决下。。老师周四就过来检查了可惜没事运行不了。。。蛋疼
import rhinoscriptsyntax as rs
import random
import math
#########################################################################
#LINES
#########################################################################
class MuscleLines():
   
    def __init__(self):
        self.lineIDs = []
        self.pipeIDs = []
        self.startPts = []
        self.endPts = []
        
        
    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
        
    def cleanUp(self):
        rs.DeleteObjects(self.lineIDs)
        self.lineIDs = []
        rs.DeleteObjects(self.pipeIDs)
        self.pipeIDs = []
        self.startPts = []
        self.endPts = []
#########################################################################
#MUSCLES
#########################################################################
class Muscle():
   
    """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"
        
        
      
        
            
    def MovePoint(self, listOfAgents):
        MoveVec = [0,0,0]
        TotalDist = 0
        OverallDist = 0
        AvgDist = 0
        
        if len(self.neighbors) > 0:
            
            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)
                    
                    
                    TotalDist = TotalDist + ((Dist * (self.neighborstrength[self.neighbors.index(neighbor)]))-Dist)
                else:
                    
                    NewLine = rs.AddCurve([neighbor, self.point])
                    NewPoint = rs.AddPoint(rs.CurveMidPoint(NewLine))
                        
                    NewMuscle = Muscle(rs.PointCoordinates(NewPoint), self.startThres, self.resetthreshold, (len(listOfAgents)+1), NewPoint, (self.resetStrength) )
                    listOfAgents.append(NewMuscle)
                    rs.DeleteObject(NewLine)
                    self.strength = self.resetStrength
                    self.threshold = self.startThres
                    print "we got one"
            
            
        
            AvgDist = TotalDist/len(self.neighbors)
            
        
        MoveVec = rs.VectorUnitize(MoveVec)
        if MoveVec == None:
            MoveVec = [0,0,0]
        MoveVec = rs.VectorScale(MoveVec, AvgDist)
        
        self.id = rs.MoveObject(self.id, MoveVec)
        self.point = rs.PointCoordinates(self.id)
        self.strength = self.strength + 0.008*rs.VectorLength(MoveVec)
        self.threshold = self.threshold + 0.1*rs.VectorLength(MoveVec)
            
    def Trail(self):
        if self.MyTrail != "empty":
            rs.DeleteObject(self.MyTrail)
        if len(self.positions) > 2:
            
            self.MyTrail = rs.AddCurve(self.positions,2)
            rs.ObjectLayer(self.MyTrail, "TRAILS", True)
        
    def cleanUp(self):
        self.neighbors = []
        self.neighborIndexes = []
            
            
            
            
#########################################################################
#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:
                    
                    dist = rs.Distance(myAgent.point, otherAgent.point)
                    
                    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)
        
        """#CREATE ANIMATION FRAMES"
        path = "C:UserstomDesktopTESTANIMATION"
        frame = str(y)
        
        rs.EnableRedraw(enable=True)
        rs.Command("_Render")
        #rs.Command("-_SaveRenderWindowAs "C:UserstomDesktopENCODED MATTERanimationtestRenderingone.jpg"")
        rs.Command("-_SaveRenderWindowAs " + path +frame+".png")
        rs.Command("_CloseRenderWindow")
        rs.EnableRedraw(enable=False)"""
        
        
        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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享
关于大陆地区Rhino原厂培训中心
Jorin 发表于 2013-10-23 13:05:12
2
将两组点放到不同图层执行楼主这段代码,发现以下问题:
第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)就没有返回值了。

评分

参与人数 1技术 +1 收起 理由
Jessesn + 1 神马都是浮云

查看全部评分

陈志伟o0 发表于 2013-10-23 21:07:53
3
您需要登录后才可以回帖 登录 | 注册成为会员

本版积分规则