Grasshopper

algorithmic modeling for Rhino

Hi guys,

i am learning python for grasshopper recently, but there are some odd problems that i can't figure out. today i typed some code in python following a video tutorial step by step, but it did't work when i finished, saying as follows:

Runtime error (MissingMemberException): walker instance has no attribute 'x'
Traceback:
line 21, in step, "<string>"
line 34, in script

the code is as follows:

import rhinoscriptsyntax as rs
import random as r

r.seed(seed)

#class
class walker:
  def _init_(self):
  self.x = 0
  self.y = 0

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

def step(self):
  choice = r.randint(0,3)
  if choice == 0:
    self.x = self.x + 1
  elif choice == 1:
    self.x = self.x - 1
  elif choice == 2:
    self.y = self.y + 1
  else:
    self.y = self.y - 1


#time
w = walker()

pList = []
for t in range(time):
  w.step()

  pList.append(w.point())

a = pList

can anyone help me figure it out?

thanks

file attached

grasshopper version 0.9.0064

Views: 1933

Attachments:

Replies to This Discussion

Hi Chunwei,

You made a typo: python constructor has two underscores in front and after "init": __init__
You typed one. What happens is that when you perform an instantiation of your "w" object, it does not automatically run the __init__ method, as it hasn't been written correctly. Therefor the self.x and self.y inside of it, do not get their values (0) assigned. So when you try to execute your solution, and it gets up the the line: self.x = self.x + 1, it does not know which value it needs to assign to self.x.

By the way this is not really a frequent questions.
Try moving this topic of yours from "FAQ (Frequent Questions and Problems)" section to "VB, C# and Python Coding". See here how to do that:


For all future questions on coding, post your topic it that section.
Thanks.

Hi djordje,

thanks for your so detailed explaination:) i will move my question to the right section.

thanks again.

Thanks for this! same typo :)

thanks a lot, even 3 years later for me ;)

Just to take little step forward I tried to draw a polyline with the points in the list "a":

a = plist
path = rs.AddPolyline(a)

runs ok but trying to use the points from "a" as spheres centers with this: 


sph = rs.AddSphere(a,rd)

its says: Could not convert [<System.Guid object at 0x0000000000000C0C [878bf11a-629b-46c4-ac7c-ac0dfcdda377]>] to a Point3d  (it can't convert a list [] to a point3D)

do i need to feed addsphere with single points one at a time? can't I use a list?

I know it must be one of the simplest questions ever but would be grateful for any advice¡¡

Hi Chunwei and Djordje

very cool that you guys figured this all out.

Thanks,

Giulio

--

Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service