User Rating:  /  (4 / 12)
PoorBest 

ruby-thumb

Ruby, the scripting language that is built into SketchUp is a bit of a mystery for many SketchUp users. They know it is there and that plugins wouldn’t work without it but they don’t know what else they could do with it. As it turns out, Ruby can not only be used to write plugins but also to make beautiful designs – especially those types of designs that can’t be modeled using any other tool in SketchUp. A good example is geometry that is created using a mathematical formula.

What I want to do in this article is give you a quick tutorial how you can create a beautiful Christmas card using Ruby to place objects. Of course the same method can be used for other purposes than making Christmas cards.

The method I am using here is one that I describe in much more detail in Chapter 6 of my newly released book “Architectural Design with SketchUp” (published by John Wiley & Sons). In the book, I am showing how one can use small Ruby 10-liners to easily create all kinds of “scripted” geometry such as patterned panels, solar-responsive façade designs or attractor-based designs. Other chapters in this book cover photorealistic rendering, plugins, component-based design as well as provide a SketchUp refresher/primer.

So how does this work?

The image that I had envisioned for this card was one that didn’t feature a tree but rather shows the outline of the tree as it is created by stars that align in a spiraling pattern (like the lights on a tree would), which narrows to a point (the topmost star) at the peak. This is a little easier explained using the image below, which shows the finished model in SketchUp. As you can see there is not only one spiral, but rather two. Also, the stars are rotated based on where they are in the spiral such that their points always face outwards.

ruby-1

So how is this done? First, you need a star. Obviously you can model it any way you like. In my case, the star is a simple extrusion but you might want to play with the Artisan plugin a bit to make them rounder.

Once you have your star modeled, turn it into a component by selecting it, right-clicking on it and selecting “Make Component”. At this point, it is very important that you place the origin in the absolute center of the object. This is shown in the following image. Use the “Set Component Axes” button in the “Create Component” dialog to do this. The copies of the star will all be placed based on this internal origin, so things won’t line up if the component’s origin is not in the center.

ruby-2

You can now place the star anywhere you like in your model. All of the copies that will be created by the script revolve around the global origin, independent of where this component has been placed.

Enter Ruby!

For the next step you need to be able to execute multi-line Ruby code in SketchUp. This is not natively possible in SketchUp since the Ruby Console that you can find under the Window menu only runs single-line Ruby code. Therefore, you need a plugin!

While there are now quite a few editors available for SketchUp that do this trick, you might want to get my Ruby Code Editor as a start. Download it from here:

http://www.alexschreyer.net/projects/sketchup-ruby-code-editor/

Install it using the instructions provided on the website. Then start the editor and paste the following code into it:

model = Sketchup.active_model
entities = model.entities
selection = model.selection
total = 24
radius = 2
n = 12
angle = (360/n).degrees
height = 8*12
vspace = height/total
if (selection[0].typename == 'ComponentInstance')
    (0..total).each { |i|
        t1 = Geom::Transformation.translation([Math::sin(i*angle)*radius*i,Math::cos(i*angle)*radius*i,i*vspace])
        componentinstance = entities.add_instance(selection[0].definition, t1)
        t2 = Geom::Transformation.rotation componentinstance.bounds.center, [0,0,1], -(i*angle)
        componentinstance.transform!(t2)
    }
end

Before you run the code, make sure you select your star component. Once you hit the “Run” button in the Code Editor, the handful of lines above get to work and create the spiral pattern that you can see in the image below. Adjust the parameters to your liking to modify the overall appearance of the spiral. Afterwards, you can group the resulting spiral and copy it at a 180-degree rotation to create the second spiral.

ruby-3

Obviously the spiral is still upside down at this point, but that is fixed easily. As you can see in the first image in this article, I also added a ground plane onto which I applied SketchUp’s standard water material.

As a last step, I rendered the image using the amazing Twilight rendering software. For the water, make sure you apply the water texture as a bump map and for the environment use any spherical environment that you like. To get the night effect, I reduced intensity of the environment to quite a low value (something like 0.3).

The image below shows the completed rendering: 

ruby-4

With a little bit of Photoshop post-processing, this can be made quite a bit more magical as the image below shows.

ruby-5

At this point, the “starry swirl” Christmas card is done!

As you saw in this example, it is actually quite easy to create amazing things in SketchUp using a few lines of Ruby code. Combining scripting with manual modeling then serves as a very efficient way to accomplish quite a complex result. Even if you are not a programmer, the code is actually quite easy to read and you can always make small modifications to adapt it to other projects.

For the curious: The equation I used was that of a conical helix. You can find it and other spirals here:

http://www.mathematische-basteleien.de/spiral.htm

Don't forget to check out the companion site for Architectural Design with SketchUp where you can read sample chapters and browse the blog to learn more about the book and read my latest posts.

About the Author

Alex Schreier

As an Assistant Program Director and Lecturer in the Building and Construction Technology (BCT) and Architecture+Design programs at the University of Massachusetts (UMass Amherst), has been teaching classes in digital design, CAD and BIM (Building Information Modeling) for the past ten years to a varied audience of students and professionals coming from construction, engineering as well as architecture backgrounds. Alex has also authored various plugins for SketchUp and is a regular beta-tester for this software. His blog is a popular resource for information on AEC/CAD and wood design topics.

In addition, Alex' background encompasses structural engineering, wood science and heavy-timber construction. He teaches and conducts research in structural wood systems and regularly speaks at international conferences and meetings.


Visit our sponsors:

Visit 3D Connexion

 

Visit Polantis

 

thea-sponsor-logo