RhinoPythonPrimerRev3.pdf

Full Transcript

Python for Rhinoceros 5 101 © 2011 All Rights Reserved Revision 3 Introduction You’ve just opened the first edition of the Rhino Python primer. This booklet was originally written by David Rutten of Robert McNeel & Associates for Rhin...

Python for Rhinoceros 5 101 © 2011 All Rights Reserved Revision 3 Introduction You’ve just opened the first edition of the Rhino Python primer. This booklet was originally written by David Rutten of Robert McNeel & Associates for Rhino 4 and VBscript and has now been translated to encompass Python for Rhino 5. As always, this primer is intended to teach programming to absolute beginners, people who have tinkered with programming a bit or expert programmers looking for a quick introduction to the methods in Rhino. Rhinoscript (VBscript) has been supported for many years, with a large user group and extensive support material. As well as giving a basic introduction, this primer looks to easily transition those familiar with VBscript into the world of Rhino Python. For this reason, David Rutten's original primer has been used extensively as the underlying framework for this Python Primer. Python offers exciting new potentials for programming in Rhino with Object-Oriented functionality, simple syntax, access to the.NET framework and a vast number of user-built libraries to extend Rhino's functionality. The same powerful methods that were previously in VBscript are still available, as well as a ton of other exciting methods and features available natively with Python. Similar to the previous primers, we have the advantage of using geometric and visual examples to help teach programming. In many traditional scenarios, programming is taught with non-visual examples and difficult to understand engineering problems. For this reason, as well as Python's easy-to-read syntax, we should hopefully be able to bring everyone to understand and write simple programs to help automate and design within Rhino. Programming offers users the powerful ability to automate tasks, make decisions, perform powerful calculations and geometric manipulations, thus, essentially acting as a designer's side kick. This can allow thousands of computations to occur based on dynamic conditions, something that would take a human far too long to process. As a tool for iteration, generation, analysis and design evolution, programming is limitless! Programming also offers a new language to communicate with the world because almost every discipline, from the Sciences, Engineering to Art, utilize code as a progressive new medium - and this primer should hopefully give you an easy introduction into this powerful language for communicating with the world. (With that example, it should be noted that programming may be looked at as any other human language in the sense that it truly takes many hours of practice to become fluent. So don't get discouraged if you aren't an expert in one day!) I hope we have convinced you of the powerful and exciting potential for this new opportunity of Python in Rhino. Without further ado, lets dive in! Good luck! Skylar Tibbits SJET www.sjet.us www.scriptedbypurpose.net Arthur van der Harten Kirkegaard Associates www.perspectivesketch.com www.kirkegaard.com Steve Baer Robert McNeel & Associates www.rhino3d.com www.python.rhino3d.com A special thanks to David Rutten for the inspiration and invaluable work, pioneering the original Rhinoscript101 Primer. Also many thanks to Bob McNeel and everyone at Robert McNeel & Associates for their generous support! Where to find help... Forums: The RhinoPython community is very active and offers a wonderful resource for posting questions/answers and finding help on just about anything!: http://python.rhino3d.com/forums/ General References for Python: Python's main website offers a plethora of information about the syntax, building-in functionality, libraries etc! This is the main resource for anything Python! http://docs.python.org/ The Python Documentation also has a great introduction into the basics of Python: http://docs.python.org/tutorial/introduction.html http://docs.python.org/tutorial/ A very useful Python style guide: http://www.python.org/dev/peps/pep-0008/ Another very thorough resource for Python is from MIT, called "How to Think Like a Computer Scientist": http://www.greenteapress.com/thinkpython/thinkCSpy/thinkCSpy.pdf Common Exceptions/Errors: For a list of common errors, exceptions and pitfalls that you are likely to run into when coding see: http://docs.python.org/release/3.1.3/library/exceptions.html#bltin-exceptions http://secant.cs.purdue.edu/_media/proghints.pdf Syntax & Programming Reminders: -Python is Case Sensitive ("A" and "a" are NOT the same thing!) -Python is Indent Sensitive (Use indentation to delineate the scope of loops, conditionals, functions and classes) Remember an extra space or the absence of a space can make a world of a difference! -You do NOT need to declare variables or variables types! Just simply use them (x=3)! -The " # " sign is used for comments, the computer will skip over them. -Print and Return are NOT the same thing - print writes something to the screen, return actually passes a value! -Remember Variable Scope - where you define a variable is important! Variables defined within functions & classes can only be used within those functions/classes unless passed as input or through the return statement! -Develop code incrementally, testing, debugging and printing as you finish smaller sections. Writing hundreds of lines and hitting run will most likely not work and will make it far more difficult to spot errors! ***If this makes no sense to you yet - no fear! Keep reading (and come back to it later)!... Table of Contents Introduction Where to find help Table of Contents 1 What's it all about? 2 1.1 Macros 2 1.2 Scripts 3 1.3 Running Scripts 3 2 Python Essentials 4 2.1 Language origin 4 2.2 Flow control 5 2.3 Variable data 5 2.3.1 Integers and Doubles 6 2.3.2 Booleans 7 2.3.3 Strings 7 2.3.4 None variable 8 2.3.5 Using variables 9 3 Script anatomy 11 3.1 Programming in Rhino 11 3.2 The bones 12 3.3 The guts 13 3.4 The skin 13 3.5 The Debugger 14 4 Operators and functions 15 4.1 What on earth are they and why should I care? 15 4.2 Careful… 16 4.3 Logical operators 17 4.4 Functions and Procedures 19 4.4.1 A simple function example 20 4.4.2 Advanced function syntax 22 4.5 Mutability 24 5 Conditional execution 25 5.1 What if? 25 5.2 Looping 27 5.3 Conditional loops 27 5.4 Incremental loops 30 6 Tuples, Lists, Dictionaries 32 6.1 Tuples 32 6.2 Lists 32 6.2.1 List Comprehensions 33 6.3 Dictionaries 35 6.4 Points and Vectors 35 6.5 An AddVector() example 40 6.6 Nested Lists 41 7 Classes 44 7.1 Class Syntax 44 8 Geometry 46 8.1 The openNURBS™ kernel 46 8.2 Objects in Rhino 46 8.3 Points and Pointclouds 48 8.4 Lines and Polylines 53 8.5 Planes 58 8.6 Circles, Ellipses and Arcs 60 Ellipses 63 Arcs 65 8.7 Nurbs Curves 72 Control-point curves 75 Interpolated curves 77 Geometric curve properties 82 8.8 Meshes 87 Geometry vs. Topology 88 Shape vs. Image 96 8.9 Surfaces 99 Nurbs surfaces 100 Surface Curvature 108 Vector And Tensor Spaces 110 9 Notes 114 1 What’s it all about? 1.1 Macros Rhinoceros is based on a command-line interface. This means you can control it by using only the keyboard. You type in the commands and the program will execute them. Ever since the advent of the mouse, a user interface which is purely command-line based is considered to be primitive, and rightly so. Instead of typing: Line 0,0,0 10,0,0 you can also click on the Line button and then twice in the viewport to define the starting and ending points of a line-curve. Because of this second (graphical) interface some people have done away with the command-line entirely. Emotions run high on the subject; some users are command-line fanatics, others use only toolbars and menus. Programmers have no emotions in this respect, they are all wedded to the command-line. It’s no use programming the mouse to go to a certain coordinate and then simulate a mouse click, that is just plain silly. Programmers pump text into Rhino and they expect to get text in return. The lowest form of programming in Rhino is using macros. I do not wish to offend those of you who write macros for a living, but it cannot be denied that it is a very primitive way to automate processes. I shall only briefly pause at the subject of macros, partly so we know which is which and partly because we might at some point simulate macros using RhinoScript. A macro is a prerecorded list of orders for Rhino to execute. The _Line command at the top of this page is an example of a very simple macro. If your job is to open Rhino files, add a line from 0,0,0 to 10,0,0 to each one and save the file again, you would probably get very tired very quickly from typing "_Line w0,0,0 w10,0,0" six times a minute. Enter macros. Macros allow you to automate tasks you would normally do by hand but not by brain. Macros cannot be made smart, nor do they react to the things they help create. They’re a bit like traffic wardens in that respect. An example of a more sophisticated macro would be: _SelNone _Polygon _NumSides=6 w0,0,0 w10,0,0 _SelLast -_Properties _Object _Name RailPolygon _Enter _Enter _SelNone _Polygon _NumSides=6 w10,0,0 w12,0,0 _SelLast _Rotate3D w0,0,0 w10,0,0 90 -_Properties _Object _Name ProfilePolygon _Enter _Enter _SelNone -_Sweep1 _SelName RailPolygon _SelName ProfilePolygon _Enter _Enter _Closed=Yes Enter The above code will create the same hexagonal torus over and over again. It might be useful, but it's not flexible. You can type the above text into the command-line by hand, or you can put it into a button. You can even copy-paste the text directly into Rhino. Incidentally, the underscores before all the command names are due to Rhino localization. Using underscores will force Rhino to use English command names instead of -say- Italian or Japanese or whatever the custom setting is. You should always force English command names since that is the only guarantee that your code will work on all copies of Rhino worldwide. The hyphen in front of the _Properties and _Sweep1 command is used to suppress dialog boxes. If you take the hyphens out you will no longer be able to change the way a command works through the command-line. There’s no limit to how complex a macro can become, you can keep adding commands without restrictions, but you’ll never be able to get around the limitations that lie at the heart of macros. 2 1.2 Scripts The limitations of macros have led to the development of scripting languages. Scripts are something halfway between macros and true (compiled) programs and plug-ins. Unlike macros they can perform mathematical operations, evaluate variable conditions, respond to their environment and communicate with the user. Unlike programs they do not need to be compiled prior to running. Rhinoceros implements the standard ‘Microsoft® Visual Basic® Scripting Edition’ language, as well as the Python Programming language. This primer will introduce the Python Programming Language and how to utilize its functionality within Rhinoceros. Scripts, then, are text files which are interpreted one line at a time. But here’s the interesting part; unlike macros, scripts have control over which line is executed next. This flow control enables the script to skip certain instructions or to repeat others. Flow control is achieved by what is called "conditional evaluation" and we must familiarize ourselves with the language rules of Python before we can take advantage of flow control. The language rules are usually referred to as the syntax and they indicate what is and isn’t valid: 1. "There is no apple cake here." » valid 2. "There is here no apple cake" » invalid 3. "Here, there is no apple cake." » valid 4. "There is no Apfelstrudel here." » invalid The above list is a validity check for English syntax rules. The first and third lines are proper English and the others are not. However, there are certain degrees of wrong. Nobody will misunderstand the second line just because the word order is wrong. The forth line is already a bit harder since it features a word from a different language. Although most of us are smart enough to understand all four lines, a computer is not. Python is a wonderful language for beginners or advanced programmers. It offers a simple and efficient syntax as well as powerful programming functions, object-oriented capabilities and a large fan-base with user-built libraries. Also, since Rhino Python is available on both Windows and Mac, the exact same python scripts will run on both versions of Rhino! Don't get too excited yet - will get more of the details in the following sections! 1.3 Running Scripts There are several ways to run scripts in Rhino, each has its own (dis)advantages. You could store scripts as external text files and have Rhino load them for you whenever you want to run them. You could also use Rhino's in-build script editor which means you can run the Scripts directly from the editor. The last option is to embed scripts in toolbar buttons, which makes it very hard to edit them, but much easier to distribute them. Throughout this book, I will use the in-build editor method. I find this to be the best way to work on simple scripts. In order to run a script via the in-build editor, Use the _EditPythonScript command to activate it, then type in your script and press the Run button: All the example code in this primer can be copy-pasted directly into the _EditPythonScript dialog. 3 2 Python essentials 2.1 Language origin Like conversational languages, programming languages group together in clusters. Python is a high level language, indicating that the language was designed to be easy for humans to understand. On the opposite end of the spectrum are extremely low level languages, (often referred to as machine-code), that are most definitely not easy to understand. In between are languages such as C or C++ which offer layers of abstraction above machine- code. As I mentioned, Python is a step even higher, meaning that it is far easier to read (closer to the English language) and we don't need to manage difficult functionality like memory allocation, or declaring variables! Lucky us. Python was first released in 1991, since then it has grown to become freely available with a user-group exceeding tens of thousands. The Python documentation claims, " Python plays well with others," " Python runs everywhere," " Python is friendly... and easy to learn" and " Python is Open!" For more information about the Python programming language and its development see: http://www.python.org. Assuming that you might be reading these pages without any prior programming experience whatsoever, I still dare guess that the following example will not give you much trouble: somenumber = rs.GetReal("Line length") line = rs.AddLine([0,0,0], [somenumber,0,0]) print "Line curve inserted with id", line Of course you might have no conception of what [0,0,0] actually means and you might be confused by rs.GetReal() but on the whole it is pretty much the same as the English you use at the grocers: Ask Rhino to assign a number to something called 'somenumber'. Tell Rhino to add a line from the world origin to the point on the x-axis indicated by 'somenumber' print a success message Translating Python code to and from regular English should not be very difficult, at least not at this featherweight level. It is possible to convolute the code so that it becomes unreadable, but this is not something you should take pride in. The syntax resembles English for a good reason, I suggest we stick to it. As mentioned before, there are three things the syntax has to support, and the above script uses them all: 1. Flow control » Depending on the outcome of the second line, some lines are not run 2. Variable data » somenumber is used to store a variable number 3. Communication » The user is asked to supply information and is informed about the result 4 2.2 Flow control We use flow-control in Python to skip certain lines of code or to execute others more than once. We can also use flow-control to jump to different lines in our script and back again. You can add conditional statements to your code which allow you to shield off certain portions. If…Else…Else If structures are examples of conditional statements, but we'll discuss them later. A typical conditional statement is: You have to be this tall (1.5m) to ride the roller coaster. This line of ‘code’ uses a condition (taller than 1.5m) to evaluate whether or not you are allowed to ride the roller coaster. Conditional statements like this can be strung together indefinitely. We could add a maximum height as well, or a weight limitation, or a ban on spectacles or heart-conditions. Instead of skipping lines we can also repeat lines. We can do this a fixed number of times: Add 5 tea-spoons of cinnamon. Or again use a conditional evaluation: Keep adding milk until the dough is kneadable. The repeating of lines is called ‘Looping’ in coding slang. There are several loop types available but they all work more or less the same. They will be covered in detail later on. 2.3 Variable data Whenever we want our code to be dynamic we have to make sure it can handle all kinds of different situations. In order to do that we must have the ability to store variables. For instance we might want to store a selection of curves in our 3D model so we can delete them at a later stage. Or perhaps our script needs to add a line from the mouse pointer to the origin of the 3D scene. Or we need to check the current date to see whether or not our software has expired. This is information which was not available at the time the script was written. Whenever we need to store data or perform calculations or logic operations we need variables to remember the results. Since these operations are dynamic we cannot add them to the script prior to execution. We need placeholders. In the example on the previous page the thing named "somenumber" is a placeholder for a number. It starts out by being just a name without any data attached to it, but it will be assigned a numeric value in the line: somenumber = rs.GetNumber("Line length") Then later on we retrieve that specific value when we add the line curve to Rhino: curve = rs.AddLine([0,0,0], [somenumber,0,0]) All the other coordinates that define the line object are hard-coded into the script. There is no limit to how often a variable can be read or re-assigned a new value, but it can never contain more than one value and there’s no undo system for retrieving past values. Apart from numbers we can also store other types of data in variables. For the time being, we’ll restrict ourselves to the four most essential ones, plus a special one which is used for error-trapping: 1. Integers 2. Doubles 3. Booleans 4. Strings 5. Null variable 5 2.3.1 Integers and Doubles Integers and Doubles are both numeric variable types, meaning they can be used to store numbers. They cannot store the same kind of numbers, which is why we ended up with more than one type. Integers can only be used to store whole numbers. Their range extends from roughly minus two- billion to roughly plus two-billion. Every whole number between these extremes can be represented using an integer. Integers are used almost exclusively for counting purposes (as opposed to calculations). Doubles are numeric variables which can store numbers with decimals. Doubles can be used to represent numbers as large as 1.8×10308 and as small as 5.0×10-324, though in practise the range of numbers which can be accurately represented is much smaller. Those of you who are unfamiliar with scientific notation need not to worry, I shall not make a habit out of this. It is enough to know that the numeric range of doubles is truly enormous. Integers Doubles 0 0.00 1 1.5 -1 -34.9372 3785 2.7e40 (2.7×1040) « A very big positive number -2000000000 -6.2e-12 (-6.2×10 ) « -12 A very small negative number The set of all possible Double and Integer numbers is not continuous; it has gaps. There exists no Integer between zero and one and there exists no Double between zero and 5.0×10-324. The fact that the size of the gap is so much smaller with Doubles is only because we’ve picked a number close to zero. As we move towards bigger and bigger numbers, the gaps between two adjacent Double values will become bigger as well and as we approach the limits of the range, the gaps are big enough to fit the Milky Way. 2×10300 minus one billion is still 2×10300, so beware when using extremely large numbers. Normally, we never stray into the regions where 32-bit computing starts to break down, we tend to restrict ourselves to numbers we can actually cope with. The Python syntax for working with numeric variables should be very familiar: x = 15 + 26 » x equals 41 x = 15 + 26 * 2.33 » x equals 75.58 x = math.sin(15 + 26) + math.sqrt(2.33) » x equals 1.368 You can use the print() method to display the result of these computations. The print() method will display the value in the command-line: x = 2 * math.sin(15 + 26) + math.log(55) print(x) Of course you can also use numeric variables on the right hand side of the equation: x = x + 1 x = math.sin(y) + math.sqrt(0.5 * y) The first line of code will increment the current value of x by one, the second line will assign a value to x which depends on the value of y. If y equals 34 for example, x will become 4.65218831173768. Note, there is a special shortcut in Python that allows you to define multiple variables in a single line of code: x, y, z = [1,2,3] print x >> returns 1 print y >> returns 2 print z >> returns 3 6 2.3.2 Booleans Numeric variables can store a whole range of different numbers. Boolean variables can only store two values mostly referred to as Yes or No, True or False. Obviously we never use booleans to perform calculations because of their limited range. We use booleans to evaluate conditions... remember? You have to be taller than 1.5m to ride the roller coaster. "Taller than 1.5m" is the condition in this sentence. This condition is either True or False. You are either taller than 1.5m or you are not. Since most of the Flow-control code in a script is based on conditional statements, booleans play a very important role. Let’s take a look at the looping example: Keep adding milk until the dough is kneadable. The condition here is that the dough has to be kneadable. Let’s assume for the moment that we added something (an algorithm) to our script that could evaluate the current consistency of the dough. Then our first step would be to use this algorithm so we would know whether or not to add milk. If our algorithm returns False (I.e. "the dough isn’t kneadable") then we will have to add some milk. After we added the milk we will have to ask again and repeat these steps until the algorithm returns True (the dough is kneadable). Then we will know there is no more milk needed and that we can move on to the next step in making our Apfelstrudel. In Python we never write "0" or "1" or "Yes" or "No", for boolean values we always use "True" or "False". if curve is None: print "Something went terribly wrong!" This will return either True or False, only if the result is True (the curve is None) will the code pass into the conditional statement and print "Something went terribly wrong!." 2.3.3 Strings Strings are used to store text. Whenever you add quotes around stuff in Python, it automatically becomes a String. So if we encapsulate a number in quotes, it will become text: variable1 = 5 variable2 = "5" You could print these variables to the command line and they would both look like 5, but the String variable behaves differently once we start using it in calculations: print (variable1 + variable2) » Results in an "Unsupported Operand Type" Error Python throws an error when we try to add a String variable to an Integer Variable. We must first convert the string to an integer, then we can add them together. print (variable1 + int(variable2)) » Results in 10 7 When you need to store text, you have no choice but to use Strings. The syntax for Strings is quite simple, but working with Strings can involve some very tricky code. For the time being we’ll only focus on simple operations such as assignment and concatenation: a = "Apfelstrudel" » Apfelstrudel a = "Apfel" + "strudel" » Apfelstrudel a = "4" + " " + "Apfelstrudel" » 4 Apfelstrudel a = "The sqrt of 2.0 = " + str(math.sqrt(2.0)) » The sqrt of 2.0 = 1.4142135623731 Internally, a String is stored as a series of characters. Every character (or 'char') is taken from the Unicode table, which stores a grand total of ~100.000 different characters. The index into the unicode table for the question mark for example is 63, lowercase e is 101 and the blank space is 32: Char code 65 112 102 101 108 63 32 35 49 Char A p f e l ? # 1 Char # 1 2 3 4 5 6 7 8 9 Further down the road we'll be dealing with some advanced String functions which will require a basic understanding of how Strings work, but while we are still just using the simple stuff, it's good enough to know it just works the way you expect it to. Strings are used heavily in RhinoScript since object IDs are always written as strings. Object IDs are those weird codes that show up in the Object Property Details: D7EFCF0A-DB47-427D-9B6B-44EC0670C573. IDs are designed to be absolutely unique for every object which will ever exist in this universe, which is why we can use them to safely and unambiguously identify objects in the document. 2.3.4 None variable Whenever we ask Rhino a question which might not have an answer, we need a way for Rhino to say "I don't know". Using the example on page 5: curve = rs.AddLine([0,0,0], [somenumber,0,0]) It is not a certainty that a curve was created. If the user enters zero when he is asked to supply the value for somenumber, then the startpoint of the line would be coincident with the endpoint. Rhino does not like zero- length lines and will not add the object to the document. This means that the return value of rs.AddLine() is not a valid object ID. Almost all methods in Rhino will return a None variable if they fail, this way we can add error-checks to our script and take evasive action when something goes wrong: curve = rs.AddLine([0,0,0], [somenumber,0,0]) if not curve: print "Something went terribly wrong!" The statement, if not x in Python will return a value True if the variable "curve" is None, 0 or an empty list. 8 2.3.5 Using variables Conventionally, whenever we intend to use variables in a script, we would have to declare them first. However, with Python, we are relieved of this duty and we can simply create and use variables without initially declaring them. Python also does not require that we declare the type of variable we are using, as in other programming languages. Both of these qualities emphasize why Python is such a quick and easy to learn language. So, to declare a variable we simply write: a = "Apfelstrudel" When using a variable, you choose the name and then set it equal to a value (Number, String, Boolean etc). The name you get to pick yourself. In the example above we have used a, which is not the best of all possible choices. For one, it doesn't tell us anything about what the variable is used for or what kind of data it contains. A better name would be strFood. The str prefix indicates that we are dealing with a String variable here and the Food bit is hopefully fairly obvious. A widely used system for variable prefixes is as follows: Variable type Prefix Example Boolean bln blnSuccess Byte byt bytNumber Integer int intAmount Long lng lngIterations Single sng sngFactor Double dbl dblValue Decimal dec decInput Currency cur curSalary Date dtm dtmToday String str strMessage Error err errProjection Variant var varData Array arr arrNames Object obj objExplorer Don't worry about all those weird variable types, some we will get to in later chapters, others you will probably never use. The scope (sometimes called "lifetime") of a variable refers to the region of the script where it is accessible. Whenever you declare a variable inside a function, only that one function can read and write to it. Variables go 'out of scope' whenever their containing function terminates. 'Lifetime' is not a very good description in my opinion, since some variables may be very much alive, yet unreachable due to being in another scope. But we'll worry about scopes once we get to function declarations. For now, let's just look at an example with proper variable usage: strComplaint = "I don't like " strFood = "Apfelstrudel. " strNag = "Can I go now?" print(strComplaint + strFood + strNag) An important note to reiterate is Python's case sensitivity. Unlike other languages, in Python "Apfelstrudel", "apfelstrudel" and "ApfelStrudel" are not equivalent, this is also true for all variable names, functions, classes and any other part of the code. Just remember to be very careful with upper and lower case letters! Now, high time for an example. We'll be using the macro from page 2, but we'll replace some of the hard coded numbers with variables for added flexibility. This script looks rather intimidating, but keep in mind that the messy looking bits (line 10 and beyond) are caused by the script trying to mimic a macro, which is a bit like trying to drive an Aston-Martin down the sidewalk. Usually, we talk to Rhino directly without using the command-line and the code looks much friendlier: 9 1 import rhinoscriptsyntax as rs 2 3 dblMajorRadius = rs.GetReal("Major radius", 10.0, 1.0, 1000.0) 4 dblMinorRadius = rs.GetReal("Minor radius", 2.0, 0.1, 100.0) 5 intSides = rs.GetInteger("Number of sides", 6, 3, 20) 6 7 strPoint1 = " w" + str(dblMajorRadius) + ",0,0" 8 strPoint2 = " w" + str(dblMajorRadius + dblMinorRadius) + ",0,0" 9 10 rs.Command ("_SelNone") 11 rs.Command ("_Polygon _NumSides=" + str(intSides) + " w0,0,0" + strPoint1) 12 rs.Command ("_SelLast") 13 rs.Command ("-_Properties _Object _Name Rail _Enter _Enter") 14 rs.Command ("_SelNone") 15 rs.Command ("_Polygon _NumSides=" + str(intSides) + strPoint1 + strPoint2) 16 rs.Command ("_SelLast") 17 rs.Command ("_Rotate3D w0,0,0 w1,0,0 90") 18 rs.Command ("-_Properties _Object _Name Profile _Enter _Enter") 19 rs.Command ("_SelNone") 20 rs.Command ("-_Sweep1 _SelName Rail _SelName Profile _Enter _Enter _Closed=Yes _Enter") 21 rs.Command ("_SelName Rail") 22 rs.Command ("_SelName Profile") 23 rs.Command ("_Delete") 24 25 Line Description 2..5 This is where we ask the user to enter a number value ("Real" is another word for "Double"). We supply the rs.GetReal() method with four fixed values, one string and three doubles. The string will be displayed in the command-line and the first double (10.0) will be available as the default option: We're also limiting the numeric domain to a value between one and a thousand. If the user attempts to enter a larger number, Rhino will claim it's too big: 7…8 On these lines we're creating the strings, based on the values of dblMajorRadius and dblMinorRadius. If we assume the user has chosen the default values in all cases, dblMajorRadius will be 10.0 and dblMinorRadius will be 2.0, which means that strPoint2 will look like " w12,0,0". 10…23 This is the same as the macro on page 3, except that we've replaced some bits with variables and there are three extra lines at the bottom which get rid of the construction geometry (so we can run the script more than once without it breaking down). 10 3 Script anatomy 3.1 Programming in Rhino Rhinoceros offers various ways of programmatic access. We've already met macros and scripts, but the plot thickens. Please invest a few moments of your life into looking at the diagram below, which you will never be asked to reproduce: COM Application VB.NET Scripts openNURBS™ UI MFC Rhino RMA Script External Application TroutLake C# Rhinoceros C++ The above is a complete breakdown of all developer tools that Rhino has to offer. I'll give you a brief introduction as to what this diagram actually represents and although that is not vital information for our primary goal here ("learning how to script" in case you were wondering), you might as well familiarize yourself with it so you have something to talk about on a first date. At the very core of Rhino are the code libraries. These are essentially collections of procedures and objects which are used to make life easier for the programs that link to them. The most famous one is the openNURBS library which was developed by Robert McNeel & Associates but is completely open source and has been ported by 3rd party programmers to other operating systems such as Unix and Linux. OpenNURBS provides all the required file writing and reading methods as well the basic geometry library. Practically all the 3D applications that support the 3dm file format use the openNURBS library. These code libraries have no knowledge of Rhino at all, they are 'upstream' so to speak. Rhino itself (the red blob) is tightly wrapped around these core libraries, it both implements and extends them. Apart from this obvious behavior, Rhino also adds the possibility of plugins. Whereas most companies provide plugin support for 3rd party developers, McNeel has taken a rather exotic approach which eliminates several big problems. The technical term for this approach is "eating your own dogfood" and it essentially boils down to McNeel programmers using the same tools as 3rd party programmers. Rather than adding code to Rhino itself, McNeel programmers prefer writing a plugin instead. For one, if they screw up the collateral damage is usually fairly minor. It also means that the SDK (Software Development Kit, that which is used to build plugins) is rigorously tested internally and there is no need to maintain and support a separate product. Unfortunately the result of this policy has made plugins so powerful that it is very easy for ill-informed programmers to crash Rhino. This is slightly less true for those developers that use the dotNET SDK to write plugins and it doesn't apply at all to us, scripters. A common proverb in the software industry states that you can easily shoot yourself in the foot with programming, but you can take your whole leg off with C++. Scripters rarely have to deal with anything more severe than a paper-cut. The orange pimples on Rhino's smooth surface are plugins. These days plugins can be written in C++ and all languages that support the DotNET framework (VB.NET, CSharp, Delphi, J#, IronPython etc. etc.). One of these plugins is the RhinoScript plugin and it implements and extends the basic Microsoft Visual Basic Scripting language as well as Python at the front end, while tapping into all the core Rhino resources at the back end. Scripts thus gain access to Rhino, the core libraries and even other plugins through the RhinoScript plugin. Right, enough fore-play, time to get back to hard core programming. 11 3.2 The bones Once you run a script through the in-build editor (remember you can access the editor by typing "EditPythonScript" in Rhino's command line) the Python interpreter will thumb through your script and superficially parse the syntax. It will not actually execute any of the code at this point, before it starts doing that it first wants to get a feel for the script. The interpreter is capable of finding certain syntax errors during this prepass. If you see a dialog box like this: before anything has actually taken place, it means the compiler ran into a problem with the syntax and decided it wasn't worth trying to run the script. If the script crashes while it is running, the Source of the error message will not be the Python Compiler. However, even scripts without syntax errors might not function as expected. In order for a script to run successfully, it must adhere to a few rules. Apart from syntax errors -which must be avoided- every script must implement a certain structure which tells the interpreter what's what: Option ImportExplicit area* Statements Option Explicit area* Global Global Variables variables* Global variables* Functions call Main function & Classes Main functionCalls Function call & Class Instances Main function Main function Note that the example script on page 11 did notAdditional adhere to these rules. It ran just the same, but it was a bad functions* example in this respect. Additional functions* The Import Statement allows the user to import different modules that are either built into Python when its downloaded, or from external developments. Importing modules allows a user to access methods outside of the current file and reference objects, functions or other information. There are various types of Import Statements: import X, from X import, from X import a, b, c, X = __import__(‘X’), each with advantages and disadvantages. For simplicity we can stick with import X for the time being. This technique imports module X and allows us to use any methods within that module. Comments (blocks of text in the script which are ignored by the compiler and the interpreter), can be used to add explanations or information to a file, or to temporarily disable certain lines of code. It is considered good practise to always include information about the current script at the top of the file such as author, version and date. Comment lines are indicated with a # sign. Global variables are variables that can be accessed anywhere in your code (outside of functions, within functions and within classes). Variable scope refers to the limitation or accessibility of a variable across different portions of code. Global variables obviously can be accessed globally, while other variables may be limited to certain areas of your code. For example, any variable that is created within a class or a function (we will cover classes and functions later) is limited to within that function. This means they cannot be used outside of that function or class (unless they are specifically passed as input/output). For now, we don't need to worry about different types of scope and let's assume that our variables are globally accessible unless otherwise noted. Functions are blocks of code that compact certain functionality into a small package. Functions can have variables, take input, provide output and do a number of other important tasks. We will go into further detail about functions in the coming chapters. Classes are similar in that they provide an opportunity for creating module code to package/compress segments of your code, while also providing other powerful tools. Functions and classes must be created before they can be used (this is rather obvious). For that reason, the Functions & Classes section comes before the Function Calls and Class Instances section. This just means that before we can actually Call (use) a Function, we need to first create the function. 12 3.3 The guts The following example shows the essential structure that was just described, including: the Import Statement (always needed!), Global Variables, a Function and a Call to the Function. The importance of syntax should also be stated - Please take note of the capitalization and indentation within this example. Python is both case sensitive and indent sensitive. If you spell a variable name once with a capital letter and another time with a lowercase letter, it will not recognize it as the same variable! The indent is used to indicate if certain lines should be included within a Function, Class, Loop or Conditional statement. In this example, the line "print (text)" is indented to be contained within the function "simpleFunction" because it should only be executed once that function is called (Don't worry yet about how and why functions work, we will explain them soon). Indentation and Case Sensitivity should be highly emphasized since they are a couple of the most common mistakes that you will run into! import rhinoscriptsyntax as rs « Import Statement #Script written by Skylar Tibbits on 03-09-2011 « Default comments strInfo = "This is just a test" « Global Variable def simpleFunction(text): « Function Declaration print(text) « Code to Execute Within the Function (Note the Indentation) simpleFunction(strInfo) « Calling the Function (After it's created) One of the key features of VBScript that made it easy to write powerful scripts was the large library of Rhino specific functions. The Python implementation includes a set of very similar functions that can be imported and used in any python script for Rhino. This set of functions is known as the rhinoscript package. To import the rhinoscript package you must include the "import rhinoscriptsyntax" statement, "as rs" indicates that we will be using the name "rs" whenever we refer to this package. In the Editor, go to Help>Python Help for a list of all the rhinoscript methods. Documentation can also be found at http://www.rhino3d.com/5/ironpython/index.html Note: McNeel has made all of the classes in the.NET Framework available to Python, including the classes available in RhinoCommon. This allows you to do some pretty amazing things inside of a python script. Many of the features that once could only be done in a.NET plug-in can now be done in a python script! (Don't stress about this until you become a master of the basics...for now, just know its available!) 3.4 The skin After a script has been written and tested, you might want to put it in a place which has easy access such as a Rhino toolbar button. If you want to run scripts from within buttons, there are two things you can do: 1. Link the script 2. Implement the script If you want to implement the script, you'll have to wrap it up into a _RunPythonScript command. Imagine the script on the previous page has been saved on the hard disk as an *.py file. The following button editor screenshot shows how to use the two options: 13 3.5 The Debugger The Debugger is an essential tool for any programmer. Luckily, the script-editor within Rhino has a built-in Debugger for testing and working line-by-line through any script! It is extremely good practice to use the debugger when writing any code longer than just a few lines. The expression "bug in your code," means that something has gone wrong in your code - i.e your code fails, cannot continue to run or has given the wrong output. (Of interesting note - the first computer bug is said to have been found in 1947, when Harvard University's Mark II Aiken Relay Calculator machine was experiencing problems. An investigation showed that there was a moth trapped in the machine. The operators removed the moth and taped it into the log book. The entry reads: "First actual case of bug being found." And thus, the world of debugging was born!) With any malfunctioning code, the programmers job is to quickly and easily identify the bug, however, this can be sometimes extremely difficult, especially if the code has many loops, conditional statements, functions, classes and spans hundreds or thousands of lines. The debugger allows the user to put a breakpoint in the code which suspends the execution of the code and allows the user to see the status of the variables. Without a breakpoint the debugger would run entirely through to completion and would not allow us to see the guts! To add a breakpoint simply click to the left of the line number and a red circle will appear (You can also add multiple breakpoints). This indicates the code will pause at this line. Press the Green arrrow at the top of the editor to start the debugger. Stop Step Out Step Over Step Into Debugger Start Button Breakpoint Use the "Step Into", "Step Over", "Step Out" buttons to walk line-by-line through the code. When you come to a loop or conditional statement you can decide to enter or step over it completely. After each line is executed, the debugger will show the variable, object or expressions' name, its value and type. As the lines are run, the variables and values will be updated directly. This will allow you to check if your variables are taking the correct values, if your code passes the correct conditional statement or if it loops for a given number of times. Many unforeseen errors can quickly be spotted and adjusted by using the Debugger! Type Value Name Variable "line" has a GUID # 07d1..... 14 4 Operators and functions 4.1 What on earth are they and why should I care? When we were discussing numeric variables in paragraph 2.3.1, there was an example about mathematical operations on numbers: x = 15 + 26 * 2.33 x = math.sin(15 + 26) + math.sqrt(2.33) x = math.tan(15 + 26) / math.log(55) The four lines of code above contain four kinds of code: 1. Numbers » 15, 26, 2.33 and 55 2. Variables » x 3. Operators » =, +, * and / 4. Functions » math.sin(), math.sqrt(), math.tan() and math.log() Numbers and variables are well behind us now. Arithmetic operators should be familiar from everyday life, Python uses them in the same way as you used to during math classes. Python comes with a limited amount of arithmetic operators and they are always positioned between two variables or constants (a constant is a fixed number). The function first signifies that we have imported math at the top of our code, using "import math", and then call a function that is within the math module called "sin()". Thus we write: math.sin(value). Operator Example Arithmetic operators: = Assign a value to a variable x = 5 + Add two numeric values x = x + 1 - Subtract two values x = 1 - x * Multiply two values x = x * (x-1) / Divide two values x = (x+1) / (2*x + 1) // Floored Quotient x = x // 10 ** Raise a number to the power of an exponent x = x ** 2.3 means: x2.3 % Divide two numbers and return only the remainder x = x % 5 Concatenation operators: + Concatenate two String variables x = x + " _Enter" Comparison operators: < Less than if(x < 5): -1): >= Greater than or equal to if(x >= 0): == Equal to if(x == 10.0): != Not equal to if(x != 10.0): Is Compare object variables for equality Logical and bitwise operators: And Logical conjunction if(A and B): Or Logical disjunction if(A or B): Not Logical negation if(A not B): 15 4.2 Careful… One thing to watch out for is operator precedence. As you will remember from math classes, the addition and the multiplication operator have a different precedence. If you see an equation like this: x = 4 + 5 * 2 x = (4 + 5) * 2 » wrong precedence x = 4 + (5 * 2) » correct precedence x doesn't equal 18, even though many cheap calculators seem to disagree. The precedence of the multiplication is higher which means you first have to multiply 5 by 2, and then add the result to 4. Thus, x equals 14. Python is not a cheap calculator and it has no problems whatsoever with operator precedence. It is us, human beings, who are the confused ones. The example above is fairly straightforward, but how would you code the following? x 2 + ^ x - 1h 2x y= + x 0.5x x-3 Without extensive use of parenthesis, this would be very nasty indeed. By using parenthesis in equations we can force precedence, and we can easily group different bits of mathematics. All the individual bits in the mathematical notation have been grouped inside parenthesis and extra spaces have been inserted to accentuate transitions from one top level group to the next: y = ( math.sqrt(x ** 2 + (x - 1)) / (x - 3) ) + abs( (2 * x) / (x ** (0.5 * x)) ) It is still not anywhere near as neat as the original notation, but I guess that is why the original notation was invented in the first place. Usually, one of the best things to do when lines of code are getting out of hand, is to break them up into smaller pieces. The equation becomes far more readable when spread out over multiple lines of code: A = x**2 + (x-1) B = x-3 C = 2*x D = x**(0.5* x) y = (math.sqrt(A) / B) + abs(C / D) 16 4.3 Logical operators I realize the last thing you want right now is an in-depth tutorial on logical operators, but it is an absolute must if we want to start making smart code. I'll try to keep it as painless as possible. Logical operators mostly work on booleans and they are indeed very logical. As you will remember, booleans can only have two values. Boolean mathematics were developed by George Boole (1815-1864) and today they are at the very core of the entire digital industry. Boolean algebra provides us with tools to analyze, compare and describe sets of data. Although George originally defined six boolean operators we will only discuss three of them: 1. Not 2. And 3. Or The Not operator is a bit of an oddity among operators. It is odd because it doesn't require two values. Instead, it simply inverts the one on the right. Imagine we have a script which checks for the existence of a bunch of Block definitions in Rhino. If a block definition does not exist, we want to inform the user and abort the script. The English version of this process might look something like: Ask Rhino if a certain Block definition exists If not, abort this sinking ship The more observant among you will already have noticed that English version also requires a "not" in order to make this work. Of course you could circumvent it, but that means you need an extra line of code: Ask Rhino if a certain Block definition exists If it does, continue unimpeded Otherwise, abort When we translate this into Python code we get the following: if (not rs.IsBlock("SomeBlockName")): print ("Missing block definition: SomeBlockName") And and Or at least behave like proper operators; they take two arguments on either side. The And operator requires both of them to be True in order for it to evaluate to True. The Or operator is more than happy with a single True value. Let's take a look at a typical 'one-beer-too-many' algorithm: person = GetPersonOverThere() colHair = GetHairColour(person) if((IsGirl(person)) and (colHair == Blond or colHair == Brunette) and (Age(person) >= 18)): neighbour = GetAdjacentPerson(person) if(not IsGuy(neighbour) or not LooksStrong(neighbour)): print("Hey baby, you like Heineken?") else: RotateAngleOfVision 5.0 As you can see the problem with Logical operators is not the theory, it's what happens when you need a lot of them to evaluate something. Stringing them together, quickly results in convoluted code not to mention operator precedence problems. 17 A good way to exercise your own boolean logic is to use Venn-diagrams. A Venn diagram is a graphical representation of boolean sets, where every region contains a (sub)set of values that share a common property. The most famous one is the three-circle diagram: A Not B Not C Not A Not B Not C B+A A+C -or- Not C Not B A+B+C Not A Not A B+C Not C Not A Not B B C Every circular region contains all values that belong to a set; the top circle for example marks off set {A}. Every value inside that circle evaluates True for {A} and every value not in that circle evaluates False for {A}. If you're uncomfortable with "A, B and C", you can substitute them with "Employed", "Single" and "HomeOwner". By coloring the regions we can mimic boolean evaluation in programming code: A Not A A And B A Or B A Or B Or C (A Or B) And Not C C And Not A And Not B B Or (C And A) Try to color the four diagrams below so they match the boolean logic: (A And B) Or ((B And C) And Not A) Or (B And Not C) Or A And B And C (B And C) Or (A And C) (A And Not B And Not C) (C And Not B) Venn diagrams are useful for simple problems, but once you start dealing with more than three regions it becomes a bit opaque. The following image is an example of a 6-regional Venn diagram. Pretty, but not very practical: 18 4.4 Functions and Procedures In the end, all that a computer is good at is shifting little bits of memory back and forth. When you are drawing a cube in Rhino, you are not really drawing a cube, you are just setting some bits to zero and others to one. At the level of Python there are so many wrappers around those bits that we can't even access them anymore. A group of 32 bits over there happens to behave as a number, even though it isn't really. When we multiply two numbers in Python, a very complicated operation is taking place in the memory of your PC and we may be very thankful that we are never confronted with the inner workings. As you can imagine, a lot of multiplications are taking place during any given second your computer is turned on and they are probably all calling the same low-level function that takes care of the nasty bits. That is what functions are about, they wrap up nasty bits of code so we don't have to bother with it. This is called encapsulation. A good example is the math.sin() function, which takes a single numeric value and returns the sine of that value. If we want to know the sine of -say- 4.7, all we need to do is type in x = math.sin(4.7). Internally the computer might calculate the sine by using a digital implementation of the Taylor series: In other words: you don't want to know. The good people who develop programming languages predicted you don't want to know, which is why they implemented a math.sin() function. Python comes with a long list of predefined functions all of which are available to RhinoScripters. Some deal with mathematical computations such as math.sin(), others perform String operations such as abs() which returns the absolute value. Python lists 75 native procedures plus many more in any of the modules that can be imported (i.e. the math module). I won't discuss them here, except when they are to be used in examples. Apart from implementing the native Python functions, Rhino adds a number of extra ones for us to use. The current RhinoScript helpfile for Rhino5 claims a total number of about 800 additional functions, and new ones are added frequently. Rhino's built in functions are referred to as "methods". They behave exactly the same as Python procedures although you do need to look in a different helpfile to see what they do. (http://www.rhino3d.com/5/ironpython/index.html) So how do functions/procedures/methods behave? Since the point of having procedures is to encapsulate code for frequent use, we should expect them to blend seamlessly into written code. In order to do this they must be able to both receive and return variables. math.sin() is an example of a function which both requires and returns a single numeric variable. The datetime.now() function on the other hand only returns a single value which contains the current date and time. It does not need any additional information from you, it is more than capable of finding out what time it is all by itself. An even more extreme example is the rs.Exit() method which does not accept any argument and does not return any value. There are two scenarios for calling procedures. We either use them to assign a value or we call them out of the blue: 1. strPointID = rs.AddPoint([0.0, 0.0, 1.0]) » Correct 2. rs.AddPoint([0.0, 0.0, 1.0]) » Correct 2. rs.AddPoint [0.0, 0.0, 1.0] » Wrong If you look in the RhinoScript helpfile and search for the AddLayer() method, you'll see the following text: rs.AddLayer (name=None, color=0, visible=True, locked=False, parent=None) rs.AddLayer() is capable of taking five arguments, all of which are optional. We can tell they are optional because it says "Optional" next to each item under the "Parameters" section of the helpfile. The "Parameters" signify the Input values for the Function, while the "Returns" section tells us what the Function will return. Optional arguments have a default value which is used when we do not override it. If we omit to specify the lngColor argument for example the new layer will become black. 19 4.4.1 A simple function example This concludes the boring portion of the primer. We now have enough information to actually start making useful scripts. I still haven't told you about loops or conditionals, so the really awesome stuff will have to wait until Chapter 5, though. We're going to write a script which uses some Python functions and a few RhinoScript methods. Our objective for today is to write a script that applies a custom name to selected objects. First, I'll show you the script, then we'll analyze it line by line: 1 import rhinoscriptsyntax as rs 2 import time 3 #This script will rename an object using the current system time 4 5 strObjectID = rs.GetObject("Select an object to rename",0,False,True) 6 7 if strObjectID: 8 9 strNewName = "Time: " + str(time.localtime()) 10 11 rs.ObjectName(strObjectID, strNewName) This is a complete script file which can be run directly from the disk. It adheres to the basic script structure according to page 13. We'll be using two variables in this script, one to hold the ID of the object we're going to rename and one containing the new name. On line 5 we declare a new variable. Although the "str" prefix indicates that we'll be storing Strings in this variable, that is by no means a guarantee. You can still put numbers into something that starts with str. It is simply the convention to name a variable with strSomething if it is storing a string, similarly you can use intSomething for integers etc. On line 5, we're assigning a value to strObjectID by using the RhinoScript method GetObject() to ask the user to select an object. The help topic on GetObject() tells us the following: Rhino.GetObject (message=None,filter=0,preselect=False,Select=False,custom_filter=None,subobjects=False) Returns: String » The identifier of the picked object if successful. None » If not successful, or on error. This method accepts six arguments, all of which happen to be optional. In our script we're only specifying the first and fourth argument. The strMessage refers to the String which will be visible in the command-line during the picking operation. We're overriding the default, which is "Select object", with something a bit more specific. The second argument is an integer which allows us to set the selection filter. The default behavior is to apply no filter; all objects can be selected whether they are points, textdots, polysurfaces, lights or whatever. We want the default behavior. The same applies to the third argument which allows us to override the default behavior of accepting preselected objects. The fourth argument is False by default, meaning that the object we pick will not be actually selected. This is not desired behavior in our case. The fifth argument takes a bit more explaining so we'll leave it for now. 20 Note that we can simply omit optional arguments and put a closing bracket after the last argument that we do specify. When the user is asked to pick an object -any object- on line 5, there exists a possibility they changed their mind and pressed the escape button instead. If this was the case then strObjectID will not contain a valid Object ID, it will be None instead. If we do not check for variable validity (line 7) but simply press on, we will get an error on line 11 where we are trying to pass that None value as an argument into the rs.ObjectName() method. We must always check our return values and act accordingly. In the case of this script the proper reaction to an Escape is to abort the whole thing. The If: structure on Line 7 will abort the current script if strObjectID turns out to be None. If strObjectID turns out to be an actual valid object identifier, our next job is to fabricate a new name and to assign it to the selected object. The first thing we need is a variable which contains this new name. We declare it and assign it a value on line 9. The name we are constructing always has the same prefix but the suffix depends on the current system time. In order to get the current system time we use the time.localtime() function which is a function built into the time module (which we have imported at the top of our script). Since a Time and a String are not the same thing, we cannot concatenate them with the ampersand operator. We must first convert the Time into a valid String representation. The str() function is another Python native function which is used to convert non-string variables into Strings. When I tested this script, the value assigned to strNewName at line 11 was: Time: (2011, 3, 10, 22, 17, 53, 3, 69, 0) Finally, at line 11, we reach the end of our quest. We tell Rhino to assign the new name to the old object: Instead of using strNewName to store the name String, we could have gotten away with the following: rs.ObjectName(strObjectID, "Time: " & str(time.localtime())) This one line replaces lines 9 through 11 of the original script. Sometimes brevity is a good thing, sometimes not. Especially in the beginning it might be smart to be explicit and take up multiple lines; it makes debugging a lot easier (until you feel comfortable making your code shorter and possibly harder to decipher). 21 4.4.2 Advanced function syntax Whenever you call a function it always returns a value, even if you do not specifically set it. By default, every function returns a None value, since this is the default value for all variables and functions in Python. So if you want to write a function which returns you a String containing the alphabet, doing this is not enough: 1 def Alphabet(): 2 strSeries = "abcdefghijklmnopqrstuvwxyz" The word "def" signifies the start of a function. "Alphabet" is a name we have made-up for our function. Again, the indentation indicates that line 2 is within the function and should only be run after the function is called. Although the function actually assigns the alphabet to the variable called strSeries, this variable will go out of scope once the function ends on line #2 and its data will be lost. You have to assign the return value to the function name, like so: 1 def Alphabet(): 2 strSeries = "abcdefghijklmnopqrstuvwxyz" 3 return strSeries 4 5 print Alphabet() The "return value" identifies what will be returned once the method is called and the code within its scope is executed. When this code is run, it will call the function Alphabet(), the code within the function's scope will be run and the function will return the value of strSeries. This returned value will then be printed to the command line. It should be noted that at first glance, the return and print functions appear to be very similar. However, they are not! print() will print anything to the command line and console. return(), on the other hand, will only return a value from a function - basically assigning a value to a variable whenever the function was called. Return is used for the output of a function (in this case the function "Alphabet"), print is used for debugging code or whenever the user wants to see a value printed to the screen. Imagine you want to lock all curve objects in the document. Doing this by hand requires three steps and it will ruin your current selection set, so it pays to make a script for it. A function which performs this task might fail if there are no curve objects to be found. If the function is designed-not-to-fail you can always call it without thinking and it will sort itself out. If the function is designed-to-fail it will crash if you try to run it without making sure everything is set up correctly. The respective functions are: 1 def lockcurves_fail(): 2 rs.LockObjects(rs.ObjectsByType(rs.filter.curve)) 1 def lockcurves_nofail(): 2 curves = rs.ObjectsByType(rs.filter.curve) 3 if not curves: return False 4 rs.LockObjects(curves) 5 return True If you call the first function when there are no curve objects in the document, the rs.ObjectsByType() method will return a None variable. It returns None because it was designed-not-to-fail and the None variable is just its way of telling you; "tough luck". However, if you pass a None variable as an argument to the rs.LockObjects() method it will keel over and die, generating a fatal error! Error Message: iteration over non-sequence of type NoneType This means that the rs.LockObjects() method requires a list to iterate through and we have provided None variable - thus the error! 22 The second function, which is designed-not-to-fail, will detect this problem on line 6 and abort the operation. As you can see, it takes a lot more lines of code to make sure things run smoothly... A custom defined function can take any amount of arguments between nill and a gazillion. Anyone who calls this function must provide a matching signature or an error will occur. More on the argument list in a bit. The first line which contains the name and the arguments is called the function declaration. Everything in between is called the function body, and is noted by the indentation. In the function body you can declare variables, assign values, call other functions and return variables. The argument list takes a bit more of explaining. Usually, you can simply comma separate a bunch of arguments and they will act as variables from there on end: def MyBogusFunction(intNumber1, intNumber2): This function declaration already provides three variables to be used inside the function body: 1. MyBogusFunction (when a user calls this function - it will provide the return value) 2. intNumber1 (the first argument) 3. intNumber2 (the second argument) Let's assume this function determines whether intNumber1 plus 100 is larger than twice the value of intNumber2. The function could look like this: 1 def MyBogusFunction(intNumber1, intNumber2): 2 intNumber1 = intNumber1 + 100 3 intNumber2 = intNumber2 * 2 4 return (intNumber1 > intNumber2) In this function, we can see that we have used "def" to indicate that we are creating a new function, we have called it "MyBogusFunction" and have given it two input variables (intNumber1, intNumber2). Within the indentation (the guts of the function), we have done a few calculations and we have used the "return" statement to output an evaluation of our calculations. Now, when we call the function somewhere else in our code, the variable will be set to the return value of our function.: 1 print MyBogusFunction(5, 6) The result will be True (105 is indeed greater than 36)! Previously, we mentioned something called variable scope - this refers to where a variable has been defined and where it can be used. Functions and Classes are very specific when it comes to variable scope. Variables that are defined within a function cannot be referenced outside of the function unless they are passed through the input or return statements!! For example: 1 def testFunction(): 2 y=20 3 return y 4 print y*testFunction() This code will return an error, "'y' is not defined" because the variable named "y" has only been defined within a function. That means that we cannot use that variable outside of the function unless we pass it through the input or return statements. The code literally does not understand what "y" means because it was created inside of the function. Otherwise, we could have defined y outside of the function which would make it have global scope and we could use it anywhere within the code. 1 y = 20 2 def testFunction(): 3 return y 4 print testFunction() 23 4.5 Mutability Python includes a fairly confusing, although sometimes useful, quality pertaining to variables, tuples, lists and dictionaries (the last three we will dive into deeper a bit later). When we create a variable it points to a specific place in memory and if we create a second variable that is equal to the first - Does y point to the same space in memory as x, or does it now have its own referenced space? For example: 1 #VARIABLE EXAMPLE: 2 x = 10 3 y = x 4 x = 5 5 print(y) What will be printed? It turns out, the result is 10! That means that y is referencing the initial value of x, it is NOT referencing the variable (and thus it does not change when x changes). Although we haven't gone through them, Tuples will act the same as this variable example, while Lists and Dictionaries will be changed based on the referenced variable. For example: 1 #TUPLE EXAMPLE: 2 x = (1,2) 3 y = x 4 x = (3,4) 5 print y # The result = (1,2) Tuples act very similar to variables with regard to referencing other items. In this example, the tuple called "y" is NOT changed once we change the value of "x". 1 #LIST EXAMPLE - BAD: 2 x = [1,2] 3 y = x 4 x.append(3) 5 print y The result = (1,2,3) In this example, the List "y" DOES change once we change the value of "x". Thus, the result is (1,2,3), not (1,2) as was the case in the previous examples. This demonstrates that Lists are referencing the variable not the value of "x". In order to make "y" act as its own, independent variable and value, we must create a copy of the first variable: 1 #LIST EXAMPLE - GOOD: 2 x = [1,2] 3 y = x[:] #This creates a copy of the list "x" 4 x.append(3) 5 print y The result = (1,2) The variable[:] symbol creates a copy of the variable. This means that "y" will now be an independent list and will not change when "x" changes! One more example: 1 #DICTIONARY EXAMPLE - BAD: 2 x = {1:'a',2:'b'} 3 y = x 4 x = 'c' 5 print y The result = {1:'a',2:'b',3:'c'} In this example the dictionary "y" will be changed with the dictionary "x", unless we use x.copy(). 1 #DICTIONARY EXAMPLE - GOOD: 2 x = {1:'a',2:'b'} 3 y = x.copy() 4 x = 'c' 5 print y The result = {1: 'a', 2: 'b'} This gets into the topic of mutability. An element is considered mutable if it can be changed/modified once they are created. Variables and Tuples are considered Immutable, meaning that they cannot be changed unless you create a new variable with the newly desired value (or copy over top of the old variable). Lists and Dictionaries are considered mutable, because they can be modified once they have been created. This means that we can freely add, remove, slice the values within a List or Dictionary. This is an exciting and powerful tool, that was previously not available with VBscript Arrays! More on this later when we get into Tuples, Lists and Dictionaries... 24 5 Conditional execution 5.1 What if? What if I were to fling this rock at that bear? What if I were to alleviate that moose from its skin and wear it myself instead? It's questions like these that signify abstract thought, perhaps the most stunning of all human traits. As a programmer, you need to take abstract thought to the next level; the very-very-conscious level. A major part of programming is recovering from screw-ups. A piece of code does not always behave in a straightforward manner and we need to catch these aberrations before they propagate too far. Other times we design our code to deal with more than one situation. In any case, there's always a lot of conditional evaluation going on, a lot of 'what if' questions. Let's take a look at three conditionals of varying complexity: 1. If the object is a curve, delete it. 2. If the object is a short curve, delete it. 3. If the object is a short curve, delete it, otherwise move it to the "curves" layer. The first conditional statement evaluates a single boolean value; an object is either is a curve or it is not. There's no middle ground. The second conditional must also evaluate the constraint 'short'. Curves don't become short all of a sudden any more than people grow tall all of a sudden. We need to come up with a boolean way of talking about 'short' before we can evaluate it. The third conditional is identical to the second one, except it defines more behavioral patterns depending on the outcome of the evaluation. The translation from English into Python is not very difficult. We just need to learn how conditional syntax works. Problem 1: if (rs.IsCurve(strObjectID)): rs.DeleteObject(strObjectID) Problem 2: if (rs.IsCurve(strObjectID)): if (rs.CurveLength(strObjectID) < 0.01): rs.DeleteObject(strObjectID) Problem 3: if (rs.IsCurve(strObjectID)): if (rs.CurveLength(strObjectID) < 0.01): rs.DeleteObject(strObjectID) else: rs.ObjectLayer(strObjectID, "Curves") The most common conditional evaluation is the If…Then statement. If…Then allows you to bifurcate the flow of a program. The simplest If…Then structure can be used to shield certain lines of code. It always follows the same format: 1 if (SomethingOrOther): 2 DoSomething() 3 DoSomethingElseAsWell() The bit of code that is indented after the if(): is evaluated and when it turns out to be True, the block of code between the first and last line will be executed. If SomethingOrOther turns out to be False, lines 2 and 3 are skipped and the script goes on with whatever comes after line 3. In case of very simple If…Then structures, such as the first example, it is possible to use a shorthand notation which only takes up a single line instead of three. The shorthand for If…Then looks like: if (SomethingOrOther): DoSomething() Whenever you need an If…Then…Else structure, you can use the following syntax: 25 1 if (SomethingOrOther): 2 DoSomething() 3 else: 4 DoSomethingElse() If SomethingOrOther turns out to be True, then the bit of code between lines 1 and 3 are executed. This block can be as long as you like of course. However, if SomethingOrOther is False, then the code after else is executed. So in the case of If…Else, one -and only one- of the two blocks of code is put to work. You can nest If…Then structures as deep as you like, though code readability will suffer from too much indenting. The following example uses four nested If…Then structures to delete short, closed curves. 1 if (rs.IsCurve(strObjectID)): 2 if (rs.CurveLength(strObjectID) < 1.0): 3 if (rs.IsCurveClosed(strObjectID)): 4 rs.DeleteObject(strObjectID) 5 When you feel you need to split up the code stream into more than two flows and you don't want to use nested structures, you can instead switch to something which goes by the name of the If…Elif…Else statement. As you may or may not know, the Make2D command in Rhino has a habit of creating some very tiny curve segments. We could write a script which deletes these segments automatically, but where would we draw the line between 'short' and 'long'? We could be reasonably sure that anything which is shorter than the document absolute tolerance value can be removed safely, but what about curves which are slightly longer? Rule #1 in programming: When in doubt, make the user decide. That way you can blame them when things go wrong. A good way of solving this would be to iterate through a predefined set of curves, delete those which are definitely short, and select those which are ambiguous. The user can then decide for himself whether those segments deserve to be deleted or retained. We won't discuss the iteration part here. The conditional bit of the algorithm looks like this: 1 dblCurveLength = rs.CurveLength(strObjectID) 2 3 if (dblCurveLength != None): 4 if (dblCurveLength < rs.UnitAbsoluteTolerance()): 5 rs.DeleteObject(strObjectID) 6 elif (dblCurveLength < (10 * rs.UnitAbsoluteTolerance())): 7 rs.SelectObject(strObjectID) 8 else: 9 rs.UnselectObject(strObjectID) In Python you can say the same thing in many different ways. The above snippet could have been written as a nested If…Then structure, but then it would not resemble the way we think about the problem. 26 5.2 Looping Executing certain lines of code more than once is called looping in programming slang. There are two types of loops; conditional and incremental which can be described respectively as: Keep adding milk until the dough is kneadable Add five spoons of cinnamon Conditional loops will keep repeating until some condition is met where as incremental loops will run a predefined number of times. Life isn't as simple as that though, and there are many different syntax specifications for loops in Python, we'll only discuss the two most important ones in depth. 5.3 Conditional loops Sometimes we do not know how many iterations we will need in advance, so we need a loop which is potentially capable of running an infinite number of times. This type is called a Do…Loop. In the most basic form it looks like this: 1 while (something is true): 2 DoSomething() 3 if (condition is met): 4 break All the lines indented after the while keyword will be repeated until we abort the loop ourselves. If we do not abort the loop, I.e. if we omit the break statement or if our condition just never happens to be met, the loop will continue forever. This sounds like an easy problem to avoid but it is in fact a very common bug. In Python it does not signify the end of the world to have a truly infinite loop. The following example script contains an endless While...Loop which can only be cancelled by shutting down the application. 1 import rhinoscriptsyntax as rs 2 import datetime as dt 3 4 def viewportclock(): 5 now = dt.datetime.now() 6 textobject_id = rs.AddText(str(now), (0,0,0), 20) 7 if textobject_id is None: return 8 rs.ZoomExtents(None, True) 9 while True: 10 rs.Sleep(1000) 11 now = dt.datetime.now() 12 rs.TextObjectText(textobject_id, str(now)) 13 14 if __name__=="__main__": 15 viewportclock() Rhino Viewport Here's how it works: 27 Line Description 1 & 2 Import calls referencing external code - in this case, Rhinoscriptsyntax and datetime. We assign each of them an alias using the 'as' keyword in order simplify function calls later. 4 Main Function declaration 5 We create a time object which contains a record the date and time of the function call datetime.now(). 6 We create a new Rhino Text object to display the date and time from step 5. rs.AddText (Text, point_or_plane , Height=1.0 , Font="Arial" ,font_style=0 ) Five arguments, the last three of which have default assignments, and so are optional. When adding a text object to Rhino we must specify the text string and the location for the object. There are no defaults for this. The height of the text, font name and style do have default values. However, since we're not happy with the default height, we will override it to be much bigger: textobject_id = rs.AddText(str(now), (0,0,0), 20) The Text argument must contain a String description of the current system time. We will simply nest casting function to get it. Since a cast operation for a datetime object is a well known and solid operation, we do not have to check for a Null variable and we can put it 'inline'. This will give us the date and the time. we could have pared this down to just the time by calling the dt.datetime.time(now) function. Neither of these return a String type variable, so before we pass it into Rhino we have to cast it to a proper String using the str() function. This is analogous with our code on page 20. The point_or_plane argument requires a list of doubles. We haven't done lists yet, but it essentially means we have to supply the x, y and z coordinates of the text insertion point. (0,0,0) means the same as the world origin. The default height of text objects is 1.0 units, but we want our clock to look big since big things look expensive. Therefore we're overriding it to be 20 units instead. 7 I don't think there's anything here that could possibly go wrong, but it never hurts to be sure. Just in case the text object hasn't been created we need to abort the subroutine in order to prevent an error later on. 9 We start an infinite While... loop, lines 10, 11 and 12 will be repeated for all eternity. 10 There's no need to update our clock if the text remains the same, so we really only need to change the text once every second. The Rhino.Sleep() method will pause Rhino for the specified amount of milliseconds. We're forcing the loop to take it easy, by telling it to take some time off on every iteration. We could remove this line and the script will simply update the clock many times per second. This kind of reckless behaviour will quickly flood the undo buffer. 11 Here we update our now object. This will give us an updated datetime object. 12 This is the cool bit. Here we replace the text in the object with a new String representing the current system time. 14 & 15 This is where the viewport clock function is called. In IronPython, the main function call must be executed after the definition of the function. The if __name__ == "__main__":... trick exists in Python so that our Python files can act as either reusable modules, or as standalone programs. 28 A simple example of a non-endless loop which will terminate Rhino Viewport itself would be an iterative scaling script. Imagine we need a tool which makes sure a curve does not exceed a certain length {L}. Whenever a curve does exceed this predefined value it must be scaled down by a factor {F} until it no longer exceeds {L}. This approach means that curves that turn out to be longer than {L} will probably end up being shorter than {L}, since we always scale with a fixed amount. There is no mechanism to prevent undershooting. Curves that start out by being shorter Result curve Input curve than {L} should remain untouched. A possible solution to this problem might look like this: 1 import rhinoscriptsyntax as rs 2 # Iteratively scale down a curve until it becomes shorter than a certain length 3 4 def fitcurvetolength(): 5 curve_id = rs.GetObject("Select a curve to fit to length", rs.filter.curve, True, True) 6 if curve_id is None: return 7 8 length = rs.CurveLength(curve_id) 9 10 length_limit = rs.GetReal("Length limit", 0.5 * length, 0.01 * length, length) 11 if length_limit is None: return 12 13 while True: 14 if rs.CurveLength(curve_id)

Use Quizgecko on...
Browser
Browser