Monday, August 17, 2009

The worm

Here is an example of the worm script - it creates a worm like lofted surface from two curves. One curve specifies the centre line, and the other curve specifies the radius of arcs generated for lofting.

This works with version 0.1.4 of py2rhino.

Remember that you will need to insert your own curve ids into the script.



import py2rhino as p2r
import py2rhino.util.vector as v
#===============================================================================
# Parameters
#===============================================================================
#global variables
total_arcs = 15

#these ids will change - get the from your rhino file
radius_crv_id = "0b20eb2b-3229-4c54-97d3-c90c81f73f83"
center_crv_id = "b19973bf-9dc9-42f5-9e8d-b530e51cff23"

#===============================================================================
# Functions
#===============================================================================
#function to draw an arc
def draw_arc(crv, arc_num, step, radius):

#get the parameter
parameter = arc_num * step

#get the xyz point and the tangent vector at this parameter
origin = crv.eval.evaluate(parameter)
t = crv.eval.tangent(parameter)

#get the vector at right angles to the tangent and the z axis
y_axis = v.cross_product(t, (0,0,1))

#create three point using vector geometry
pt1 = v.add(origin, v.scale(y_axis, radius))
pt2 = v.add(origin, v.scale(v.reverse(y_axis), radius))
pt3 = v.add(origin, (0,0,radius))

#create the arc
arc = p2r.obj.Arc.create_by_3pt(pt1, pt2, pt3)

#return the arc
return arc
#------------------------------------------------------------------------------
#function to get the radius
def get_radius(crv, arc_num, step):

#get the parameter
parameter = arc_num * step

#get the xyz point at this parameter
pt = crv.eval.evaluate(parameter)

#draw a sphere just to see where this point is (optional)
p2r.obj.Sphere.create(pt, 0.3)

#return the y value of the point - this will be the height
return pt[1]
#------------------------------------------------------------------------------
#function to get the step size
def get_step_size(crv):

#get the step size by dividing the domain by the total_arcs
d = crv.prop.domain()
interval = d[1] - d[0]

#divide the domain interval by the total number of steps
#the number of steps is one less than the number of arcs
step = interval/(total_arcs - 1)

#return the size of the step
return step
#===============================================================================
# The main Script
#===============================================================================

#create two NurbsCurve objects
radius_crv = p2r.obj.NurbsCurve(radius_crv_id)
center_crv = p2r.obj.NurbsCurve(center_crv_id)


#get the step size of each curve
radius_step = get_step_size(radius_crv)
centre_step = get_step_size(center_crv)


#create an empty list to store the arcs that will be created
arcs = []

#loop to create arcs
arc_num = 0
for i in range(total_arcs):
radius = get_radius(radius_crv, radius_step, total_arcs)
arc = draw_arc(center_crv, arc_num, centre_step, radius)
arcs.append(arc)
arc_num = arc_num + 1

#create the surface by lofting the arcs
srf = p2r.obj.NurbsSurface.create_by_loft(arcs)


print "done"

No comments:

Post a Comment