This collection of small script snippets presents handy little routines that are usually too small to put into a proper extension. Use them with the Ruby Code Editor (just paste the code and hit “run”) or make them more permanent as a menu item (see Appendix D in my book).

At least when it comes to creating realistic-looking vegetation in SketchUp, it is important to be able to randomly place, scale, and rotate objects. Nothing looks worse than a “sterile” copied/pasted row of trees, for example. Of course, this applies to other objects as well, like e.g. a shag carpet or fur. Let’s fix this with today’s code snippet.

Typically, the objects to be placed would come in the form of components (trees, grass, bushes, as in the image above). And those then need to be placed somewhat randomly on one or more faces in your model. A good example for that is a lawn, as I discussed in Chapter 5 in my book. As I mention in Chapter 4, there are of course several extensions available that let you accomplish this task. However, as it turns out, we can re-use two pieces of example code from Chapter 7 and do this with just a few lines of Ruby script.

Want to use this without coding? This script is also in one of the tools of my Random Tools extension.

Let’s Explore the Code

The code below first asks for some parameters. You can decide on the number of items per face, how much they should get rotated and how much variation there should be when scaling. Since the rotating and scaling operations are randomized, even a little bit of variation will make things look quite a bit more realistic than simply copying things.

After that, the code will look through the previously selected objects (using Ruby’s grep method) and pick the first component instance (i.e. make sure you also select one before running the code!) and all the selected (ungrouped) faces. It then iterates through each face and will attempt to place your intended number of copies on it.

As you can see, this code uses the face’s bounding box to place random points onto the plane of that face (using SketchUp’s project_to_plane and classify_point methods). First, random points are placed into the space of the bounding box, which next get projected onto the plane of the face. The code then eliminates all points that lie on the face’s plane but not on the face itself. While this will likely reduce the number of overall insertion locations, it would not have been realistic to include copies that might be located “just off” the actual face.

When placing copies, the code will create those grouped and on a new layer. That allows you to hide the potentially many copies for efficiency until you need them (e.g. for rendering). As an added bonus, the code also includes some feedback (using Sketchup.status_text) so that it doesn’t seem that SketchUp is stuck while it iterates through all the faces.

As you can see, this code combines the “Randomizing Everything” and “Solar-Responsive Design” code examples from Chapter 7 and expands a bit on those. But as in my earlier examples, only a few lines of code give you a ton of flexibility to accomplish a rather tedious task.

Next Steps

You may want to try the following here:

  • Use objects that need to be perpendicular (“normal”) to a surface (e.g. grass) as well as those that need to be vertical (e.g. trees). Just run the code multiple times with varying parameters.
  • Add objects to different layers by simply editing the layer name before running the code.
  • Try placing a “proxy component” (e.g. a simple vertical line in a component) and then swap it out using SketchUp’s component tools. This often helps with efficiency.

Code Snippet

Reference

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *