0

我是 Python 的初学者,在 Rhino3D Grasshopper 中运行 python 模块时遇到了麻烦。Python模块中的代码如下所示。单独的 Agent 实例运行良好,但是当我尝试dla.update()在下Timer一个 python 模块(它只是正在执行dla.update())中使用时,我收到消息说self.pos = rs.EvaluateCurve(cirCrv, rndNum)and中有问题self.curAgent = Agent(self.cirCrv, self.cenPt, self.walkDist)

有没有办法让它工作?

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import ghpythonlib.components as gh
import random as rnd


class Agent :
    def __init__(self, cirCrv, cenPt, walkDist):
        self.cenPt = cenPt
        self.walkDist = walkDist
        rndNum = rnd.random()
        self.pos = rs.EvaluateCurve(cirCrv, rndNum)

    def randWalk(self) :
        v = rs.VectorSubtract(rs.AddPoint(0,0,0), self.pos)
        v1 = rs.VectorUnitize(v)
        v2 = rs.VectorScale(v1, self.walkDist)
        v3 = rs.VectorSubtract(self.pos, rs.AddPoint(0,0,0))
        v4 = rs.VectorAdd(v3, gh.UnitZ(1))
        rotAxis = rs.VectorSubtract(v4, v3)
        newV1 = rs.VectorRotate(v2, rnd.uniform(-70, 70), rotAxis)
        self.pos = rs.VectorAdd(self.pos, newV1)

    def checkDist(self):

        dist1 = rs.Distance(self.pos, rs.AddPoint(0,0,0))
        return dist1


class dla :

    def __init__(self, cirCrv, cenPt, walkDist):
        self.Agents = []
        self.cirCrv = cirCrv
        self.cenPt = cenPt
        self.walkDist = walkDist
        self.curAgent = Agent(self.cirCrv, self.cenPt, self.walkDist)
        self.pos =self.curAgent.pos 


    def update(self):
        if self.curAgent.checkDist() < 1:
           self.Agents.append(self.curAgent)
           self.curAgent = Agent(self.cirCrv, self.cenPt, self.walkDist)
        else:
            self.curAgent.randWalk()

a = dla(cirCrv,cenPt,walkDist)

4

0 回答 0