Quantcast
Channel: Grasshopper - McNeel Forum
Viewing all articles
Browse latest Browse all 39494

Update class instance

$
0
0

@lepli wrote:

I'm a beginner in Python and have a trouble in running a python module in Grasshopper. The code is to create an instance object to randomly walk to the centre point(0,0,0) and it stops at a certain distance to the centre.
It is a bit lengthy because avoiding an error caused by the problem with Guid ID, RhinoscriptSyntax and Common. I am still confused with handling this.
Anyway, the code in a Python module is shown as below. An instance of Agent alone is working well but when I try to dla.update() with Timer in a next python module(which is simply executing "dla.update()"), I got the message saying that there is something wrong in "self.pos = rs.EvaluateCurve(cirCrv, rndNum)" and "self.curAgent = Agent(self.cirCrv, self.cenPt, self.walkDist)".
Is there a way to make this working ?
Many thanks in advance.


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)

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 39494

Trending Articles