我是Python的初学者,在Rhino3D Grasshopper中运行python模块时遇到了麻烦。 Python模块中的代码如下所示。
单独的Agent实例运行良好但是当我在下一个python模块中尝试使用dla.update()
Timer
时(它只是执行dla.update()
),我得到的消息说有一些东西self.pos = rs.EvaluateCurve(cirCrv, rndNum)
和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)