 # Polyline in python

excuse me, but i don’t find the command “Add polyline” in python,
and i don’t find how can i make a list of 3D points to construc my polyline… the list is generate in a loop…

Using `rhinoscriptsyntax`:

Or directly using `RhinoCommon`:
https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Polyline__ctor_1.htm

excuse me, in fact, my Browser window was not full screan, so the module Search Bar was hidden!!!

but can i do a list point by this way:
‘’'python

``````for b in range(0,NbObjX-1*14,14):
PtTraj(0)=[X,Y,Z]
PtTraj(1)=[X,Y+DiamObj+DeltaObj,Z]
...
....
PtTraj(13)=[X,Y+DiamObj+DeltaObj,Z]
``````

‘’’

That all looks a bit odd. For instance assigning to a tuple like that. It helps tremendously to provide a full minimal working example when asking coding questions (also formatting the code helps). Anywho, here’s two different minimal examples using `RhinoCommon`:

A) Making the polyline vertices first, then the polyline:

``````import Rhino as rc

# Make vertices
vts = []
for i in range(10):
pt = rc.Geometry.Point3d(i,i,i)
vts.append(pt)

# Make polyline
pl = rc.Geometry.Polyline(vts)
``````

B) Making the polyline directly within a loop:

``````import Rhino as rc

# Make polyline
pl = rc.Geometry.Polyline()
for i in range(10):
pt = rc.Geometry.Point3d(i,i,i)
``````
3 Likes

Ok Thanks, but I’m really going to sound new, but I started programming with Rhinoscript, but I realize that Python would be more efficient, a language that I’m beginning to understand, and there you talk about rhinocommon … is this another language?
I do not recognize anything in your code…

`RhinoCommon` is the Rhino API, which most end-user coding things in Rhino are simply wrappers around (such as the `rhinoscriptsyntax` Python package). I’d suggest going through these guides before going much further:

yes I know, I’m looking in this python guide, it seems to me that I miss something …
why do you offer me a rhinocommon, we can not mix python and rhinocommon, right?

thanks anyway for the time you spend, on my questions …

Hello,

Yes you can mix them. The rhinoscriptsyntax python library is designed to emulate the syntax of rhinoscript. It is written in a functional way : you apply a function, you get a result. The result is generally updated directly in the Rhino document so you can see it working straight away.

It is written using rhinocommon in python. rhinocommon is written in an object oriented way : a point is an object with properties (x, y, z position,etc) and methods (functions, eg to calculate the distance to another object).

You can see the rhinocommon implementation of rhinoscriptsyntax by using this script : Python : May the source be with you!

You can mix both methods but you need to be careful as rhinoscriptsyntax generally refers to objects(points, etc) using their GUID number whereas rhinocommon refers to the rhinocommon object.

-Graham

3 Likes

More on python/rhinoscriptsyntax/RhinoCommon compared to Rhinoscript…

If you already know some Rhinoscript, you can start using the same methods in python rhinoscriptsyntax. Most of them work pretty much the same way - there a few exceptions which you may run into from time to time. There are also a few missing functions, usually they can be handled by diving down into a little RhinoCommon. As @Dancergraham said, python rhinoscriptsyntax is just a set of methods that emulate Rhinoscript, but in reality are calling RhinoCommon base functions. If you want, you can look at what each function does, the entire rhinoscriptsyntax library is located at:

`C:\Users\<username>\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\rhinoscript`

Now, there are a couple of important differences when scripting Rhino with python - and most of these are why it’s much more powerful and fun.

First, Python is an object-oriented programming language. The saying is that “EVERYTHING in Python is an object”. Why is that important? Because objects have things like “Properties” and “Methods”. Properties are things that are intrinsic to the object. If the object was an apple, its properties might be size, color, variety, etc. Methods are things you can do with the object - again if it was an apple, you could have eat, peel, throw, etc.

Let’s take a simple point as a Rhino example. In VB Rhinoscript, a point in space is represented simply as an array of 3 coordinates X,Y,and Z. You create one with Array(x,y,z). But it remains simply an array of 3 numbers - most of the Rhinoscript methods accept this as representing a point (or a vector).

In Python a point in space is an object, a ‘Point3d’.

https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Point3d.htm

It is more than just a list of 3 numbers - like all objects, it has both properties and methods. Properties include of course its coordinates, which, if the object is named ‘pt’ can be accessed via pt.X, pt.Y, pt.Z. However, a point3d object also has methods and even ‘operators’.

In Rhinoscript, to get the distance between two points, you need to program:

`dist=Rhino.Distance(ptA,ptB)`

In Python, you can use

`dist=ptA.DistanceTo(ptB)` - DistanceTo() is a Point3d method.

To add two points in Rhinoscript you invoke

`newPoint=Rhino.PointAdd(ptA,ptB)`

In Python you can simply add them like this:

`new_point=ptA+ptB`

Rhinoscriptsyntax methods that return a point or a collection of points in Python will return a point3d object or a list of point3d objects, not just lists of numbers. Once you learn how this works, however, you will see that it is nice and powerful.

The other thing about working in Python that is very different from working in VB script, is its handling of lists. Lists in Python are always dynamic, so there is no need to Redim your array(list) any time you want to add to it. The tools available for working with lists in Python are incredible compared to Vb Script.

OK, I could go on and on, but maybe I’ll stop here for the moment. 2 Likes

Ok, but the link you posted is a rhinocommon API Page, so it’s means that the properties of a point 3D is the same in rhinocommon and python?

where can i find properties and methods of all type of rhino’s object?
at this place:https://developer.rhino3d.com/api/RhinoCommon

Yes. As stated above, all rhinoscriptsyntax functions are really calling RhinoCommon methods.

There are a lot of them… Look under the Rhino.Geometry class…

https://developer.rhino3d.com/api/RhinoCommon/html/N_Rhino_Geometry.htm

Other things besides geometry are also objects - things like layers, views, materials, etc.

I read that Python2 was going to disappear to make way for Python 3 … how is it going to happen for Rhino?

Hello,

There are several recent discussions of this if you search for Python end of life, death clock, …

The current Ironpython python 2 interpreter is likely to remain unchanged for the foreseeable future. At some unspecified time a Python 3 interpreter of some type may join it.

Graham

1 Like

Hello,
when i write:
‘’’'python

``````for a in range(0,int(NbObjY)-1):
for b in range(0,int(NbObjX)-1*14,14):
PtTraj=[X,Y,Z]
PtTraj=[X,Y+DiamObj+DeltaObj,Z]
PassY+=PassY
``````

‘’’’
Script doesn’t pass the third line, first for OK, bt the second it go back to the first line i don’t understand why…

Hello,

try `for b in range(0,(int(NbObjX)-1)*14,14):` (added brackets)

Graham

how can i construct all point one after one to make a polygona with 12 edges, inverse clockwise, start to the top…

first one is Ok, but must i have to work with vector, i know angle (150°) and the length(c=2Rsin(Pi/12))
so:
1 - I create a vector (0,2Rsin(Pi/12),0)
2 - I rotate it
3 - Pt2=Pt1 +Vect
4 - Pt3 = Rotate Pt1 around Pt2 (210°)
5- Pt4= Rotate Pt2 around Pt3 (210°)

or is there a simpliest solution? with a loor for… in range…

i’m not sure for the syntax…

Here’s one approach:

``````import math
import Rhino as rc

""" Make a regular polygon with its centre at the point """

if sides > 2 and radius > 0:

# Move point in Y direction
mPt = point + yVec

# Compute step angle
stepAngle = (2*math.pi)/sides

# Rotate moved point around central axis sides times
rotatedPts = []
for i in range(0,sides):
cPt = rc.Geometry.Point3d(mPt)
cPt.Transform(rc.Geometry.Transform.Rotation(stepAngle*i,point))
rotatedPts.append(cPt)

# Make polyline
pl = rc.Geometry.Polyline(rotatedPts)

return pl
``````

Edit: I just realised that this was added in RhinoCommon, probably the simplest approach https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_Polyline_CreateInscribedPolygon.htm

Anders was faster than me, this is essentially the same, maybe just using more rhinoscriptsyntax…

``````import rhinoscriptsyntax as rs
import Rhino

c_pt=rs.GetPoint("Center point?")
dia=rs.GetReal("Diameter?")
count=rs.GetInteger("Number of points?",12,2)

#create intiial point at "top" (center point + radius in Y+)
i_pt=Rhino.Geometry.Point3d(c_pt+Rhino.Geometry.Vector3d(0,dia/2,0))
#create an empty list
pts=[]
#loop, incrementing the rotation angle by 360/count each time (starts at 0 deg.)
for i in range(count):
#create the transform
xform=rs.XformRotation2(i*(360/count),rs.ViewCPlane().Normal,c_pt)
#transform the point and add it to the list
pts.append(rs.PointTransform(i_pt,xform))
#add the points (if you want)
#add the polyline - need to add the first point to the end of the list for closed
pts.append(pts)
``````

Ok… thank you, i was searching about the lenght of the edge, but i needn’t it…
i start to understand how python run…

thank you again.

Why this doesn’t work?

``````PassY=0

#creation de la liste PtTraj
PtTraj=[]
#first point + creation de list PtTraj
PtTraj.append(Rhino.Geometry.Point3d(X,Y,Z))

for a in range(0,int(NbObjY)-1):
testpair = math.modf(PassY/2)
If testpair=0 or a=0:``````