algorithmic modeling for Rhino
Hello everyone !
I'm completely new in coding and i'm trying to make an iterative 3D tree with python but i'm blocked here:
import rhinoscriptsyntax as rs
line = rs.GetObject("pick line")
ang = rs.GetReal("type angle",35.0)
ang1 = rs.GetReal("type angle",120.0)
scale = rs.GetReal("type angle",0.9)
startPt = rs.CurveStartPoint(line)
endPt = rs.CurveEndPoint(line)
vec = rs.VectorCreate(endPt, startPt)
baseP = rs.PlaneFromNormal (startPt, vec)
print baseP
it returns me: Origin=4,2,0 XAxis=1,0,0, YAxis=0,1,0, ZAxis=0,0,1
After that i want to rotate the "vec" vector with the VectorRotate command and give the ZAxis value to it. How can i retrieve the ZAxis from the BaseP ??
Thanks and if someone is a very big newbee like me in coding, watch the EncodedMatters tutorials from Ezio Blasetti, there are really good !
Tags:
Thank you very much David !!
I feel so dumb... hehe
Hello everyone, i managed to make the 3d Tree in python for rhino, it works perfectly but when i try to make it in grasshopper nothing goes out of the python component and i don't know where i'm wrong ?? Maybe i cannot write
a = Main()
Here's the rhino python script and i'm attaching the gh file.
import rhinoscriptsyntax as rs
def Main():
startLine = rs.GetObject("pick line")
ang = rs.GetReal("type angle",33.0)
ang1 = rs.GetReal("type angle",120.0)
gens = rs.GetInteger("how many generation to produce",4)
scale = rs.GetReal("type angle",0.9)
curGeneration = []
curGeneration.append(startLine)
for i in range(gens):
newGeneration = []
for line in curGeneration:
newLines = branch(line,ang/(i+1),ang1,scale)
newGeneration.extend(newLines)
curGeneration = newGeneration
def branch(line, ang, ang1, scale):
startPt = rs.CurveStartPoint(line)
endPt = rs.CurveEndPoint(line)
vec = rs.VectorCreate(endPt, startPt)
vec = rs.VectorScale(vec, scale)
baseP = rs.PlaneFromNormal(endPt, vec)
vec1 = rs.VectorRotate(vec, ang, (baseP.YAxis))
vec2 = rs.VectorRotate(vec1, ang1, (baseP.ZAxis))
vec3 = rs.VectorRotate(vec1, -ang1, (baseP.ZAxis))
endPt1 = rs.PointAdd(endPt, vec1)
endPt2 = rs.PointAdd(endPt, vec2)
endPt3 = rs.PointAdd(endPt, vec3)
line1 = rs.AddLine(endPt, endPt1)
line2 = rs.AddLine(endPt, endPt2)
line3 = rs.AddLine(endPt, endPt3)
return [line1, line2, line3]
Main()
Internalised !!
Your main() does not return anything. Hence a will not output anything. Not quite sure what the script is doing, but lets say you wanted to output the string "fooBar" from the output parameter a. This would work:
def main():
return "fooBar"
a = main()
Hope that helps.
Sorry but i've not understand...
Here's the code in the python component:
import rhinoscriptsyntax as rs
def Main():
....curGeneration = []
....curGeneration.append(startLine)
....for i in range(gens):
........newGeneration = []
........for line in curGeneration:
............newLines = branch(line,ang/(i+1),ang1,scale)
............newGeneration.extend(newLines)
............curGeneration = newGeneration
def branch(line, ang, ang1, scale):
....startPt = rs.CurveStartPoint(line)
....endPt = rs.CurveEndPoint(line)
....vec = rs.VectorCreate(endPt, startPt)
....vec = rs.VectorScale(vec, scale)
....baseP = rs.PlaneFromNormal(endPt, vec)
....vec1 = rs.VectorRotate(vec, ang, (baseP.YAxis))
....vec2 = rs.VectorRotate(vec1, ang1, (baseP.ZAxis))
....vec3 = rs.VectorRotate(vec1, -ang1, (baseP.ZAxis))
....endPt1 = rs.PointAdd(endPt, vec1)
....endPt2 = rs.PointAdd(endPt, vec2)
....endPt3 = rs.PointAdd(endPt, vec3)
....line1 = rs.AddLine(endPt, endPt1)
....line2 = rs.AddLine(endPt, endPt2)
....line3 = rs.AddLine(endPt, endPt3)
....return [line1, line2, line3]
a = Main()
My imputs are starLine as a Curve, ang as integer, ang1, integer, gens, integer, scale float
import rhinoscriptsyntax as rs
def main():
....curGeneration = []
....curGeneration.append(startLine)
....for i in range(gens):
........newGeneration = []
........for line in curGeneration:
............newLines = branch(line,ang/(i+1),ang1,scale)
............newGeneration.extend(newLines)
............curGeneration = newGeneration
....return curGeneration
def branch(line, ang, ang1, scale):
....startPt = rs.CurveStartPoint(line)
....endPt = rs.CurveEndPoint(line)
....vec = rs.VectorCreate(endPt, startPt)
....vec = rs.VectorScale(vec, scale)
....baseP = rs.PlaneFromNormal(endPt, vec)
....vec1 = rs.VectorRotate(vec, ang, (baseP.YAxis))
....vec2 = rs.VectorRotate(vec1, ang1, (baseP.ZAxis))
....vec3 = rs.VectorRotate(vec1, -ang1, (baseP.ZAxis))
....endPt1 = rs.PointAdd(endPt, vec1)
....endPt2 = rs.PointAdd(endPt, vec2)
....endPt3 = rs.PointAdd(endPt, vec3)
....line1 = rs.AddLine(endPt, endPt1)
....line2 = rs.AddLine(endPt, endPt2)
....line3 = rs.AddLine(endPt, endPt3)
....return [line1, line2, line3]
a = main()
Hey Ivo. You were missing the return statement in the main function.
Hi Ivo,
As Anders already pointed out your Main function is not returning anything.
It needs to return something, before you reference "a" output to it.
There's a difference between the way rhinoscriptsyntax functions work in RhinoPython and in Grasshopper (that's ghpython component):
In Rhino Python Editor, when calling some rhinoscriptsyntax functions, they return certain geometry, add it to the Rhino document (like lines in this case), and therefor it is automatically visible. In grasshopper (ghpython component) the fact that certain rhinoscriptsyntax function has been called and that it returned a geometry to the Grasshopper document, does not mean that geometry will be visible. You still have to add that geometry to a certain variable/list and then return that list at the end of your function (in case you enclose your code in function(s), which you did in this example). Then assign that returned list to a certain ghpython component output variable. Check the attached file.
If this sounds a bit difficult too grasp, or if I am just terrible at explaining things, you can check the Rhino Python Primer. It has some nice beginner friendly explanations on python basics.
Thank you very much Djordje !!
Ok, i think i've understand... and i've understand also that coding is not easy, not easy at all !! Thanks again i'll check the primer !!
Great answer Djordje. Guess mine was a wee bit brief and assuming. Glad you figured it out Nenov :)
Hello everyone !
I have a big problem trying to understand python... chinese for me.
So i have an inversed tree: every generations it creats 3 new points.
On generation (1) i have 4 points counting the start point.
Generation (2) i have 4 points + (3*3points) = 13 points.
Generation (3) i have 13 points + (9*3points) = 50 points.
But when i bake the python component i have 157 points ? Why ?
What's the logic behind this ?
Also how can i have in a, lists of points according to generations and for exemple in b lines according to generations too ??
Here's the code:
import rhinoscriptsyntax as rs
import random as r
r.seed(seed)
def Main():
....allGenerations = []
....allGenerations.append(startPt)
....curGeneration = []
....curGeneration.append(startPt)
....for i in range(gens):
........newGeneration = []
........for pt in curGeneration:
............ang1 = r.randint(-30,30)
............ang2 = r.randint(90,150)
............ang3 = r.randint(210,270)
............dist1 = r.randint(10,40)
............dist2 = r.randint(10,40)
............dist3 = r.randint(10,40)
............zV = -1
............newPoints = branch(pt, ang1, ang2, ang3, dist1/(i+1), dist2/(i+1), dist3/(i+1), zV)
............newGeneration.extend(newPoints)
............curGeneration = newGeneration
............allGenerations.extend(newGeneration)
....return allGenerations
def branch(pt, ang1, ang2, ang3, dist1, dist2, dist3, zV):
....ptP1 = rs.Polar(pt, ang1, dist1)
....ptP2 = rs.Polar(pt, ang2, dist2)
....ptP3 = rs.Polar(pt, ang3, dist3)
....ptA1 = rs.AddPoint(ptP1)
....ptA2 = rs.AddPoint(ptP2)
....ptA3 = rs.AddPoint(ptP3)
....pt1 = rs.MoveObject(ptA1, [0,0,zV])
....pt2 = rs.MoveObject(ptA2, [0,0,zV])
....pt3 = rs.MoveObject(ptA3, [0,0,zV])
....ln1 = rs.AddLine(pt, pt1)
....ln2 = rs.AddLine(pt, pt2)
....ln3 = rs.AddLine(pt, pt3)
....return [pt1, pt2, pt3]
a = Main()
Thanks for you replies and sorry for my noob questions...
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by