AECBytes Architecture Engineering Construction Newsletters  

AECbytes Tips and Tricks Issue #42 (May 22, 2009)

Bezier Splines in SketchUp Using Ruby Scripts

Scott Onstott
Book & Video Author

When I first started using SketchUp I was amazed at what an intuitive and capable polygonal modeling program it was. At the same time, I was also disappointed to realize that SketchUp didn't deal with curves very well. Sure, there's a Freehand tool that reproduces every shake of your hand, and there's an Arc tool that faithfully creates sectors of circles. But I wanted true curvilinear power like I'm used to with the bezier spline and NURBS toolsets in 3ds Max. Curves like these allow you to do anything from turning wood, blowing glass, and throwing pottery, to modeling modern designer furniture, making digital sculptures, terra forming landscapes, and producing smooth animation flight paths.

Perhaps it's because there's a free version of SketchUp that there's such a vibrant community of individuals involved in extending its power with Ruby (an elegant open source programming language with a focus on simplicity). The whole SketchUp Ruby API reminds me in so many ways of Lisp, which I used in a previous life to write routines to extensively customize AutoCAD. Ruby is modern and object-oriented rather than being procedural like Lisp. But what is most significant to me is that same community feeling inspiring the goodwill of dozens of individual programmers, who offer up powerful scripts to the world for free.

The best thing is you don't have to know anything about programming to actually use Ruby scripts in SketchUp. This article is about one such ruby script called BezierSpline.rb which was written by someone known only by his screen name—Fredo6. He has written a plethora of excellent scripts (BezierSpline, BezierRoundEdge, JointPushPull, Tools on Surface, and FreeScale) and all I can say is, "Thank you Fredo6 and please keep them coming." SketchUp is a far better program with the addition of quality scripts such as these.

BezierSpline gives SketchUp all the curvilinear power it is missing in its vanilla install. With BezierSpline, you get a variety of re-editable and fillet-able polylines, splines, and Uniform B-spline curves (reminiscent of NURBS), plus you get the equivalent of the AutoCAD DIVIDE and MEASURE commands thrown in. Let's see how it works.

Installing BezierSpline

The first place I usually look for ruby scripts is in the SketchUp Library Depot, a comprehensive site maintained by Didier Bur at the Ecole Nationale Supérieure d'Architecture de Nancy in France. There is a copy of BezierSpline there, but it's an older version as of this article's publication.

To get the most current and much improved version 1.21 of BezierSpline, surf over to the forums at SketchUcation.com by clicking here. You'll have to register in the forum (the site root has a different registration process separate from the forums) in order to download ruby scripts. After you register, click here to access the BezierSpline thread where you can download the script underneath the first forum posting.

Download the zip file and the Help PDF. On the Mac, unzip the files into the root library under /Library/Application Support/Google SketchUp 7/SketchUp/plugins/. On Windows, unzip the files into C:\Program Files\Google\Google SketchUp 7\Plugins.

Installing the Arc Plug-in to BezierSpline

Fredo6 wrote the code to BezierSplibe.rb in such a way that he actually created plug-in architecture within the ruby script, hoping that others would contribute to its development. And later on, he published a plug-in to his plug-in (if you can believe it!) called BZ__Arc. It gives polylines the ability to have arc segments (fillets) connecting their vertices and is totally worth the cost of admission, which is again, FREE!

To get the most out of BezierSpline, click here to access the BZ__Arc thread. Download BZ__Arc.rb and place it within the BZ_Dir_12 sub-folder. Save the toolbar thumbnail images even deeper in IMAGES_CadFather. The Cadfather is yet another generous individual who creates toolbar icons for various SketchUp ruby scripts, and he has collaborated with Fredo6 on this particular script.

Finally, go into SketchUp Preferences (Mac: Apple > Preferences; PC: Windows > Preferences) and turn on the Bezier Spline extension. Quit SketchUp and re-launch. Choose View > Toolbars > BZ__Toolbar. Now we're ready to get started.

Creation Mode

All the Bezier spline tools have two modes, creation and editing. When you start drawing any of the shapes, you're in creation mode.

The simplest tool on the toolbar is Polyline (second from left) because all its vertices are connected with straight segments. (Note that SketchUp doesn't have a polyline tool, although the Circle and Polygon tools create polylines, and the well-known Weld.rb script allows you to connect adjoining edges into polylines.) All the curve tools are created just like the Polyline, so let's start using it first.

Click the Polyline tool in the toolbar and click a few points. Hit the Esc key once to remove the last point. Hit the Esc key twice within 2 seconds (called double Esc) and all its vertices are removed, allowing you to start over from scratch while still in the tool.

By default the Polyline tool is in Open-ended mode, indicated by a yellow triangle at the midpoint of the current segment. Press the Shift key twice within 2 seconds (called double Shift) and you toggle into Start/End mode. In this mode, you must first click the start and end points and then you can click intermediate points, starting closest to the start point.

Some of the tools default into Start/End mode so it's important to know how to toggle between modes by using double Shift, as open-ended mode is more useful in most situations.

Editing Mode

Double click after you've created some vertices and you're automatically taken into editing mode. You can alternately right-click and skip right over editing mode and be done, or choose editing mode explicitly from the context menu.

The editing mode is very straightforward. Drag a vertex to move it. Double click a vertex to remove it. Double click somewhere on a segment to add a new vertex there. And finally double click off to the side to exit editing mode and be done.

Fredo6 has done a good job of making the BezierSpline tools easy to use, much like SketchUp itself.

Plane and Axis Locks

When I first started experimenting with BezierSpline tools, I was mystified as to how to create a 3D curve. Most of the time you'll probably want Plane lock to remain on, as profile curves are necessarily two dimensional. But if you ever want to create a complex Follow Me path (for a handrail or a camera path), then you'll need to take the curve into the third dimension, and that's when you'll need to know how to turn Plane lock off.

Right click a polyline you've already drawn and choose Edit Polyline from the menu. Press Option (PC: Ctrl) to turn off Plane lock. The cursor changes to a hollow black box when Plane lock is off. Press the Up arrow key to turn on blue axis lock and drag a vertex up out of the red-green plane.

Curves that Use a Control Polygon

Once you have a handle on creating and editing polylines, it's time to dive into different curve tools. There are essentially two curve types in BezierSpline, those that use a control polygon, and those whose points lie on the curve itself. This division is echoed in programs that have NURBS toolsets (Alias, Maya, Rhino, and 3ds Max, for example) because it's mathematically based. Curves that use a control polygon are easier to shape but are a bit unwieldy because you don't have the ability to ensure that the curve passes through any specific points other than the starting and ending points of the control polygon.

In order to compare curve types, I've drawn a hexagon (and grouped it to protect its edges). You can do this or just play with the curve types in free space. Select the Classic Bezier Curve tool (first icon on toolbar) and click a few points. Notice that the points you click are part of a control polygon, not on the curve itself. Try dragging a point in editing mode and see how its position subtly influences the entire shape of the curve.

Click the Uniform B-spline tool (fourth icon when the Arc tool is present in the toolbar). You are presented with a dialog box asking for the Order. Leave 0 as the order because it will automatically choose an integer order for you. Draw a curve and observe that it works just like the Classic Bezier curve.

Press the Tab key, which calls back the same dialog box. Type Order 4 and click OK. The curve gets pulled closer to the control polygon. Thus, uniform basis splines have this one extra ability to shape the curve by pulling it closer to or pushing it farther from the control polygon, depending upon the order. In NURBS curves (like in 3ds Max), each control polygon vertex has a weight that exerts a kind of magnetism over the shape of the curve itself. Automotive and industrial designers use NURBS to precisely shape curvilinear forms.

Another difference between mathematically curved NURBS and BezierSpline.rb is the fact that all curves in SketchUp eventually must be represented by straight line segments. The Precision value controls how faceted the curve appears. Type 10s and press Return (PC: Enter) when you're editing a BezierSpline curve and you'll set the precision low enough to clearly see the facets. Type 40s and press Return (PC: Enter) to have more vertices and smaller straight segments that more closely approximate a true curve.

Choosing a precision value is a balancing act. Too much precision will lead to curves and 3D forms that have a high polygon count, and this means larger file sizes, slower real-time graphical performance and longer rendering times. Too little precision results in chunky curves that say "computer generated" in a bad way.

Closing the Loop

Every curve can be closed either with a straight line or "nicely" with a smooth curve that's tangential to the start and end points. Right click when you're in editing mode and choose Close loop nicely to see how this works. Remember that SketchUp automatically creates a face whenever you generate a planar closed loop.

It's helpful to toggle vertex marks on (fourth button from right in the toolbar) when you are closing loops. The loop precision is controlled separately from the precision of the curve itself. Type 40s and press Return (PC: Enter) and then type 5c and press Return (PC: Enter). S refers to curve preciSion and C refers to the Closing loop precision. Try 20s and 20c to see the difference.

The majority of curve tools are those whose curves pass through the points you select, and such curves have a clear practical advantage. Catmull Splines are the tightest curves and are very similar to the Cubic Bezier type. The Cubic Bezier curve is slightly smoother and more bulbous. Both of these types have points that affect the entire curve when dragged.

The F-spline creates a curve that gives local control over the curve shape; drag a vertex and see how the deformations are closer to the vertex as compared with Catmull and Cubic curves. I like F-spline the best because of this finer control. Note that the precision value for F-spline is distributed across the entire curve (higher number depending on how many points you've drawn) whereas the precision for Catmull and Cubic types is set in between points, so its precision number is correspondingly lower.

The Courbette is in a class by itself as its curves are actually tangent circular arcs, so it's not much different than the Arc tool in SketchUp. Courbettes are good for making quick work of spirals.

To simplify, my recommendations are to use either Uniform B-splines for control polygon curves or F-spline for point curves.

Converting between Curve Types and Arc Corners

You can convert SketchUp polylines (made using Weld.rb) to BezierSpline polylines. Right click a polyline and make a selection from the context menu. Once you have a BezierSpline polyline, you can right click again and convert it to any of the curve types. Or you can take an existing curve type and convert it into another curve type.

If you installed the Arc Corners plug-in, you have an extra tool in the toolbar that lets you draw polylines with arc corners directly. On the other hand, if you already have a polyline you can convert it to an Arc Corners polyline using the context menu.

Explode the hexagon group so that you have a hexagonal polyline. Right click the SketchUp polyline and choose Convert to Polyline Arc Corners. You are presented with a dialog box that asks for an offset value. Enter an offset distance and click OK and each sharp corner gets rounded off (filleted) with an arc.

The Polyline Divider and Polyline Segmentor options in the context menu allow you to re-parametrize polylines so that they have a set distance between vertices or a set number of equally spaced vertices (equivalent to the AutoCAD MEASURE and DIVIDE commands respectively). This is quite a nice bonus buried deeply within the plugin.

And of course, any curve you make with these tools can be turned into a 3D model with PushPull or FollowMe and the sky's the limit in terms of applications for curvilinear forms. Think (and draw) outside the box with BezierSpline.rb!


About the Author

Scott Onstott has a degree in architecture from UC Berkeley and worked in several prominent architecture, engineering, and interiors firms in San Francisco. In addition, he taught AEC software to thousands of students at several Bay Area universities.

Scott has written and edited scores of books, magazine articles, and video tutorials. He can be reached via ScottOnstott.com.



If you found this article useful and have not yet subscribed to AECbytes, please consider doing so. Subscription is free, and more subscribers will allow this publication to provide more of such content to you.

Tips & Tricks > Bezier Splines in SketchUp Using Ruby Scripts > Printer-friendly format

 
© 2003-2009 Lachmi Khemlani, AECbytes. All rights reserved.
Site design by Vitalect, Inc