Grasshopper

algorithmic modeling for Rhino

the following python code doesn't work in the GHPython component.

Can anybody kindly advise?


Thank you!

import rhinoscriptsyntax as rs

# declare a List to store all pts
allPts = []

x = 1
y = 1
z = 1

runComponent(x,y,z)

def runComponent(x,y,z):
    if _runIt :
        # create an array of pts
        for i in range(0,x):
            for j in range(0,y):
                for k in range(0,z):
                    # create a point
                    myPt = rs.AddPoint( (i,j,k) )
                    # add the pt to the pt list allPts
                    allPts.append(myPt)
                  
        #return all pts
        allPoints = allPts

Views: 629

Attachments:

Replies to This Discussion

Hi Grasshope,

Run your "runComponent" function after you have defined it. Not before it.

thanks, djordje!

However, the code is still not working following your suggestion (shown below). Can you kindly help to point out what I'm missing here? Thanks!

# import the Rhinoscript syntax
import rhinoscriptsyntax as rs

# inputs
# x [int]: number of pts along x axis
# y [int]: number of pts along y axis
# z [int]: number of pts along z axis
# _runIt [bool] : run the component or not

 # declare a List to store all pts
allPts = []

def runComponent():
    if _runIt :
        # create an array of pts
        for i in range(0,x):
            for j in range(0,y):
                for k in range(0,z):
                    # create a point
                    myPt = rs.AddPoint( (i,j,k) )
                    # add the pt to the pt list allPts
                    allPts.append(myPt)
                  
        #return all pts
        allPoints = allPts

runComponent()

Hi Grasshope,

My apologies for attaching the incorrect file (weaverbird-0.7.0.0.msi).

You need to un-indent the line:

        allPoints = allPts

Have in mind that appending elements to a global list (allPts) inside a function is not always a good solution.
Instead you should define a local list inside a function, append elements to it, and then return it at the end of the function.

Attachments:

Thank you, djordje!

Here's the revised code:

# import the Rhinoscript syntax
import rhinoscriptsyntax as rs

######################################################
# INPUTS
# x [int]: number of pts along x axis
# y [int]: number of pts along y axis
# z [int]: number of pts along z axis
# run [bool] : run the component or not

# OUTPUTS
# readMe! : information
# allPoints : a list of coordinate for all the points

######################################################
# declare a list to store all pt coordinates
allPoints = []
# a temp list to store all pts in the function
allPts = []

def runComponent(_x, _y, _z, _run):
    if run :
        # create an array of pts
        for i in range(0,_x):
            for j in range(0,_y):
                for k in range(0,_z):
                    # create a point
                    myPt = rs.AddPoint( (i,j,k) )
                    # add the pt to the pt list allPts
                    allPts.append(myPt)
        
        # print results
        numPts = _x*_y*_z
        print(str(numPts) + " points in a " + \
                            str(_x) + "x" +\
                            str(_y) + "x" +\
                            str(_z) + " [x,y,z] array are created!")
                            
        # return all points
        return allPts

allPoints = runComponent(x, y, z, run)

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service