How to link an object from rhino to a python script?


#1

Hi everyone!

I would like to write a loop in rhino-python (or gpython) which at some point asks me to modify manually in Rhino some objects and then directly, these modifications, are later read by the script. By directly I mean, I do not wish to give to the script again these objects. Is that possible?

An example:
Let’s say I add two points in Rhino through a Python script in a certain position and get the distance among them. The, these points are baked/drawn in Rhino. Then, within the loop, python asks me to modify manually through rhino the position of these nodes. Finally, Python tells me where are these two points now and calculates again the distance between them. Since I wish to repeat this many times and for a much larger of object I would like to reduce as much as possible to manually feed back to Python the modified objects.

1)In Python:
p1=rs.Addpoint ([0,0,0])
p2=rs.Addpoint ([10,10,0])
d=rs.Distance(p1,p2)
print d

(then p1 and p2 are drawn/baked in rhino)

2)In Rhino:
I manually modify p1 and p2

3)In Python:
The position of p1 and p2 is updated and again the new distance d is calculated.

Any help will be welcome!


#2

When the points are added to the Rhino document, they will be assigned object ID’s. AddPoint() will return the(se) ID’s. I see you got that, the ID’s will be stored under the variables p1 and p2.

At any time, you can grab those ID’s and query for their current position with rs.PointCoordinates(obj_id) or retransform them into a 3d point object with rs.coerce3dpoint(obj_id). You can then use rs.Distance to get the distance between the two.

HTH, --Mitch


#3

Thanks for the answer. It helped me to understand a bit about the ID’s.

I tried running first this:
p1=rs.AddPoint([0,0,0])
print p1

This created a point object in Rhino and printed the ID. Then, I closed the script and modified the position of the point manually. And then I run a second script with:

print rs.PointCoordinates(“cbdb8910-bf25-471a-b6c6-f2c5a95d392c”)

This told me the new position of the point. This is nice, however, I now have greater doubts:

  1. Would it be possible to run everything in one single script. One which asks me to modify the position of the point manually and then gives me the coordinates of this point.
  2. There has to be a more efficient way to store the ID than keeping and plugin the whole code. Which is that?

Thanks in advance