Automatic SketchUp Creating 3-D Models in Ruby Automatic SketchUp Creating 3-D Models in Ruby by Matthew Scarpino Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been identified with the ® symbol. SketchUp is a registered trademark of Google, Inc. Windows, Internet Explorer, and Microsoft Paint are registered trademarks of Microsoft Corporation in the United States and other countries. Mac and Mac OS are trademarks of Apple Inc., registered in the U.S. and other countries. AutoCad, Maya, and 3ds Max are registered trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document. ©2010, Autodesk, Inc. All rights reserved. Blender is a trademark of NaN Technologies B.V. Java and JavaScript are trademarks of Sun Microsystems. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. Visit us on the web: www.autosketchup.com Copyright © 2010, Eclipse Engineering LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, including electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Permissions may be sought directly from Eclipse Engineering LLC: Fax: (925) 262-1757 E-mail: [email protected] ISBN-13: 978-0-9840592-0-1 ISBN-10: 0-9840592-0-2 First printing, April 2010 Contents v Table of Contents Chapter 1: Introduction to SketchUp Scripting.................... 1 1.1 Ten Reasons to Learn SketchUp Scripting .......................................... 2 1.2 Obtaining and Installing SketchUp .................................................... 4 1.3 A Brief Example ............................................................................... 5 1.4 SketchUp Scripting and Ruby ............................................................ 7 1.5 Organization of This Book ................................................................ 8 1.6 Conclusion ...................................................................................... 9 Chapter 2: Ruby Lesson #1: Data Structures .................... 11 2.1 The Ruby Console Window ..............................................................12 2.2 Numbers and Numeric Operators .....................................................13 2.3 Strings ...........................................................................................18 2.4 Variables and Constants ..................................................................23 2.5 Arrays ............................................................................................26 2.6 Objects, Classes, and Methods.........................................................32 2.7 Class Inheritance ............................................................................37 2.8 Conclusion .....................................................................................39 vi Contents Chapter 3: Fundamentals of SketchUp Scripting ............... 41 3.1 The Three Basic SketchUp Structures ...............................................42 3.2 The Entity and Drawingelement Classes ...........................................46 3.3 Edges ............................................................................................49 3.4 Introduction to SketchUp Scripting ...................................................51 3.5 Vectors ..........................................................................................55 3.6 Edge Arrays: Curves, Circles, Arcs, and Polygons ..............................57 3.7 Creating Figures in Three Dimensions ..............................................62 3.8 Conclusion .....................................................................................72 Chapter 4: Transformations and Additional Geometry ...... 73 4.1 Transformations ..............................................................................74 4.2 Text ...............................................................................................83 4.3 Images ..........................................................................................87 4.4 PolygonMeshes ...............................................................................89 4.5 Conclusion .....................................................................................93 Chapter 5: Ruby Lesson #2: Control Structures and Files . 95 5.1 Conditional Processing: if and case ..................................................96 5.2 Processing Loops: while, until, and for ...........................................104 5.3 Iterators and Blocks ......................................................................106 Contents vii 5.4 Iterators and the SketchUp Collections ...........................................109 5.5 Files in Ruby and SketchUp ...........................................................112 5.6 SketchUp File Access ....................................................................121 5.7 Conclusion ...................................................................................124 Chapter 6: Colors, Textures, and Materials ..................... 125 6.1 Materials ......................................................................................126 6.2 Colors ..........................................................................................130 6.3 Textures .......................................................................................135 6.4 Conclusion ...................................................................................141 Chapter 7: Managing Geometry: Layers, Groups, and Components .................................................................... 143 7.1 Layers ..........................................................................................144 7.2 Groups .........................................................................................147 7.3 Components .................................................................................153 7.4 Conclusion ....................................................................................167 Chapter 8: Ruby Lesson #3: Classes and Modules ........... 169 8.1 Creating Ruby Classes ...................................................................170 8.2 Modules .......................................................................................182 8.3 Object Methods ............................................................................185 8.4 Conclusion ...................................................................................187 viii Contents Chapter 9: Attributes, Options, and Observers ............... 189 9.1 Attributes and AttributeDictionaries ................................................190 9.2 Options and RenderingOptions ......................................................194 9.3 Configuring Shadows ....................................................................198 9.4 Observers ....................................................................................202 9.5 Conclusion ...................................................................................212 Chapter 10: The SketchUp Interface: Dialogs, Menus, Commands, and Plugins ................................................... 213 10.1 Dialogs and the UI Module ..........................................................214 10.2 Menus ........................................................................................220 10.3 Commands .................................................................................225 10.4 Plugins .......................................................................................227 10.5 Conclusion .................................................................................232 Chapter 11: The SketchUp Interface: Views, Cameras, Pages, and Tools ............................................................... 233 11.1 View ..........................................................................................234 11.2 Camera ......................................................................................239 11.3 Pages .........................................................................................243 11.4 Toolbars .....................................................................................251 11.5 The Tool Class ............................................................................254 11.6 The Tools Class ...........................................................................263 Contents ix 11.7 Conclusion ..................................................................................264 Chapter 12: Actions & Animation ..................................... 265 12.1 Actions .......................................................................................266 12.2 View Animation ...........................................................................267 12.3 Page Animation ..........................................................................270 12.4 Simple Object Animation .............................................................272 12.5 Skeletal Animation ......................................................................276 12.6 Conclusion .................................................................................290 Chapter 13: WebDialogs ................................................... 291 13.1 Introducing WebDialogs ..............................................................292 13.2 The WebDialog and the Browser ..................................................294 13.3 Introduction to JavaScript ............................................................302 13.4 Combining JavaScript and SketchUp .............................................314 13.5 Conclusion .................................................................................324 Appendix A: Classes and Modules ................................... 327 Appendix B: Advanced Geometry ..................................... 391 B.1 The Point3d and Vector3d Classes .................................................392 B.2 Example: Creating Arcs in Code .....................................................401 B.3 Introduction to Transformation Matrices .........................................406