Class problem


#1

Hello,

I scripted a walker path using a class. So points are aggregating randomly on the w, y and z directions.
Then I would like to add a line between them (pt1 to pt2, pt2 to pt3, etc.).
So I added a definition for the line.
But it seems that the script doens’t recognize the variables defined just before, in the def point and def step.
So, I tried to return the values, but it doesn’t work. I really don’t know what to do now.
I putted in comment the part that doesn’t work.

Thank you for your help!

import rhinoscriptsyntax as rs
import random as r
import math as m
r.seed(1)

time = 100


class Aggregation:
    
    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0
        
        
    def point(self):
        shape = rs.AddPoint(self.x, self.y, self.z)
        return shape
    
    def step(self):
        stepX = r.uniform(-1,1)
        stepY = r.uniform(-1,1)
        stepZ = r.uniform(-1,1)
        
        self.x += stepX
        self.y += stepY
        self.z += stepZ
        
        """return self.x
        return self.y
        return self.z
        return stepX
        return stepY
        return stepZ

    
    def line(self):
        shape1 = (self.x, self.y, self.z)
        shape2 = (stepX, stepY, stepZ)
        lineShape = rs.AddLine(shape1, shape2)
        return lineShape"""

a = Aggregation()

pList = []
for t in range(time):
    a.step()
    pList.append(a.point())
    #pList.append(a.line())
a = pList

#2

Hi PPoinet,

“step” function does not need to return values. It just needs to be executed, and therefor the self.x self.y and self.z class variables will get a new a value, which can then be used for generating a point (from “point” function).

You can just add a Polyline, at the end of the all code, when the list of points has been generated:

import rhinoscriptsyntax as rs
import random as r
import math as m

r.seed(1)
time = 100

class Aggregation:

    def __init__(self):
        self.x = 0
        self.y = 0
        self.z = 0

    def point(self):
        shape = rs.AddPoint(self.x, self.y, self.z)
        return shape

    def step(self):
        stepX = r.uniform(-1,1)
        stepY = r.uniform(-1,1)
        stepZ = r.uniform(-1,1)

        self.x += stepX
        self.y += stepY
        self.z += stepZ

walker = Aggregation()

pList = []
for t in range(time):
    walker.step()
    pList.append(walker.point())
a = rs.AddPolyline(pList)

#3

thanks a lot for your answer.