Adjusted texture position

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).

This post follows a theme that I explored in several other of my script examples as well: Copied objects in SketchUp don’t appear overly realistic if they represent natural items; their geometry is simply too perfect. This is true for landscape items (trees, shrubs) but also for things like wooden boards (see left side in the image above): On a collection of real boards, the knots would be distributed somewhat randomly, not as shown here.

Turns out, we can fix this easily with a few lines of code. Just select textured objects, run the code snippet below, and your textures are randomized enough to look realistic (as can be seen in the right side of the image above).

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

Drag the slider to see the result of this code

Let’s Explore the Code, Step by Step

The following steps reference the code snippet shown below.

  1. Select all ungrouped faces

    We use sel.grep( Sketchup::Face ) to get a collection of all faces from the selection set. This works well for ungrouped faces, but once those are grouped (as they indeed should be for e.g. a wood board), things get trickier.

  2. Select all grouped faces

    We use similar code to add faces that are contained in a group’s entities collection to the all_faces array. Note the *, which makes sure the added entities come in as individual items, not another array.
    A note on components: You wouldn’t want to adjust textures inside component instances, because that would adjust other instances (copies) as well, which wouldn’t create randomness. And we can’t just create unique copies of components since that would defeat the purpose of using components (= efficiency). Therefore, the code below only considers groups in addition to ungrouped faces since those are just collections of geometry anyways.
    We have to keep one thing in mind, though: As of a few versions back, SketchUp now considers groups somewhat similar to components. This means that all copies of a group share the same definition. As a result, adjusting the texture on one copy would also apply the same adjustment to all copies – again defeating the purpose of this tool. This can only be solved by applying the make_unique method to all selected group copies. While this may increase the model size, it is the only way to create randomness in those copies and it is the expected behavior for groups anyways.

  3. Now reposition the materials randomly

    Once we have our collection of all faces, we can then use position_material to adjust the front material’s texture as well as the back material’s texture.
    One thing to keep in mind for groups is that the texture cannot be positioned when it is applied to the group object (instead of its contained faces). This is consistent with regular SketchUp behavior. Therefore, make sure textures are applied inside a group (and directly to faces) if you want to position it this way.

Code Snippet


Comments and Reactions