Intro to Python Scripting: 08 More Control Flow

Software: 
Tag: 
Video Duration: 
11 minutes
Author: 
Zach Downey

In this tutorial we look at control flow a little more. We add a boolean flag that we alternately turn on and off as we iterate through the loop. This causes our code to take a different path eatch time it executes. We also use RhinoScript's verion of the frange() function to iterate the loop by decimal values. We also create a series of sine wave curves that have variable amplitude parameters, which creates a cool effect.

#If Else with Math
#If Else with Boolean Flag
 
import rhinoscriptsyntax as rs
import math
 
rs.EnableRedraw(False)
 
#boolean flag
flip = True
color01 = [255,0,255]
color02 = [0,255, 255]
 
for i in rs.frange(0.0, 10.0, 0.1):
    ptsForCurve = []
    for j in rs.frange(0.0, 10.0, 0.1):
        x = j
        y = i
        z = math.sin(i)*math.sin(j)
        pt = [x,y,z]
        ptsForCurve.append(pt)
    curve = rs.AddCurve(ptsForCurve)
    if flip == True:
        rs.ObjectColor(curve, color01)
        flip = False
    else:
        rs.ObjectColor(curve, color02)
        flip = True
 
rs.Redraw()

Rating

Please rate this tutorial below. Thanks.

5
Average: 4.9 (12 votes)

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)