Converting Python Script to GH Python Component

script
python

#1

Hi All,

I could use a hand converting a python script into a GH python component. (noob alert)
Attached are both files. I’m getting null outputs from the GH component, and I cannot figure out why, but I suspect it has something to do with the point list not being read. Thanks in advance for any insight.

Hex Problem_1.gh (8.2 KB)
Hexagon_ angle 4.715.py (1.3 KB)


#2

I’ll try to be more specific. The original .py script (author referenced in file) radially draws a hexagonal tiling on a 2D plane. One of the important parameters is the angle of rotation, which in my case allows the hex tiling to work without overlap (so long as the rotation angle is 4.715 deg. and the ratio of hex edge length to distance between points is 1:10.53-ish). In the screenshot you’ll notice a small reveal I left between the ‘modules’ to illustrate this.

I’d like to do two things - one, make it so that a list of points can be input at once or in an automated sequence and two, convert the multi-point functionality into a python GH component, with a list of GH-generated points as the input geometry.

I hope this helps! Thanks in advance for your time and thoughts.


(Lando Schumpich) #3

Hey nick i can take a look after work.
For now: if you right-click an input node on the ghpython component you can specify the type of Data going in and More importantly for you there is an option for ‘List access’ maybe this will fix your problems

L


(Giulio Piacentino) #5

Hi @Nick_Bruscia

did @lando.schumpich’s suggestions help?

Also, in general, if a parameter has “item access” and items are input more than once, the script is repeated to allow the logic to work on each item. You could experiment with this a little in a simpler component.

Thanks,

Giulio


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


#6

Hi @lando.schumpich and @piac,

Thanks for the hints. Previously, I read some of the online documentation and made sure the Type Hints were set properly for the inputs (Point 3D for points, and float for numeric inputs). However, when I change the Pts input to List Access, I receive this error message:

It seems Item Access is the way to go, so each point can be acted on independently as described by Giulio. I also experimented a bit with a component that tries to input a single point as the original script was intended, but came out with the same results.

PS. here’s the code that’s inside the GH component, in case it’s faster for ya’ll to just look at it here:


import rhinoscriptsyntax as rs
import math

edges = 6
levels = 3
hexGrid = []
work = 2 * math.sqrt((Hex_EL * Hex_EL) - ((Hex_EL / 2) * (Hex_EL / 2)))

def polygon(center,edges,size,angle):
    tempCircle = rs.AddCircle(center,size)
    tempPoints = rs.DivideCurve(tempCircle, edges, create_points=False, return_points=True)
    tempPoints.append(tempPoints[0])
    tempHexago = rs.AddPolyline(tempPoints)
    tempReturn = rs.RotateObject(tempHexago, center, angle, axis=None, copy=False)
    rs.DeleteObject(tempCircle)
    return tempReturn
    
for i in range(0, levels):
    tempSize = work * (i + 1)
    dut = polygon(Pts, edges, tempSize, 30 + Hex_RA)
    temp = rs.DivideCurve(dut, (i+1) * edges, create_points=False, return_points=True)
    rs.DeleteObject(dut)
    hexGrid.extend(temp)
        
for i in range(0, len(hexGrid)):
    polygon(hexGrid[i], edges, Hex_EL, Hex_RA)
    rs.AddPoint(hexGrid[i])


(qythium) #7

You’re not actually assigning anything to the output variable ‘a’ of the GhPython component, that’s why it’s displaying nulls.
Also it’s useful to think of rhinoscriptsyntax as adding things to a hidden document, in order to retrieve them you have to assign the GhPython output to the return value of the rs.Add** functions.

e.g. in the last for block:

hexagons = []
for i in range(0, len(hexGrid)):
    hexagon = polygon(hexGrid[i], edges, Hex_EL, Hex_RA))
    hexagons.append(hexagon)
    # rs.AddPoint(hexGrid[i]) -- not needed

Then set the nicknames of your output parameters to the variables that you’re interested in extracting:


#8

@qythium - thanks! Your tip re: ‘adding things to a hidden document and to retrieve them with rs.Add’ is super helpful. Cheers, n


(Giulio Piacentino) #9

A post was split to a new topic: Input function question