A presentation about Scripting in Rhino 3D Software. It was designed, developed and delivered in King Abdullah University of Science and Technology on April, 2011
1. SCRIPTING IN RHINO
KAUST ‐ April, 2011
Geometric Modeling and Scientific Visualization CenterGeometric Modeling and Scientific Visualization Center
Khaled Mohamed Ahmed Abd El Gawad
www.khaledarch.comwww.khaledarch.com
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
2. Outline
Introduction
VBScript Essentials
Operators and functionsp
Conditional Execution/Branching
Arrays
Surface and Polysurface MethodsSurface and Polysurface Methods
Show Cases
Grasshopper Overview
Useful Resources
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
Useful Resources
8. Scripting vs. Programming Languages.
These days, the line between a scripting language and
i l i bl da programming language is blurred.
Scripting Languages:
Run Inside other programsRun Inside other programs
Are not compiled.
A t d t it Are easy to use and easy to write.
Scripting languages are used to build complex
software.
Scripting languages are so efficient
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
p g g g
9. Macros
Rhinoceros is based on
a command linea command‐line
interface.
A macro is a prerecorded list of orders for Rhino to
executeexecute.
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
17. Flow Control
We use it to skip certain lines of code or
To execute others more than once.
To jump to different lines in our script and back again.To jump to different lines in our script and back again.
You can add conditional statements to your code You can add conditional statements to your code
which allow you to shield off certain portion
3/12/2014 17
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
18. Variable Data
From a programmer's point of view, a variable is a
location in your computer's memory in which you canlocation in your computer s memory in which you can
store a value and from which you can later retrieve
that valuethat value.
A visual representation of memory
3/12/2014 18
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
A visual representation of memory
19. Variable Data
Whenever we need to store data or perform
calculations or logic operations we need variables tocalculations or logic operations we need variables to
remember the results.
33 33..44 datadata
X Y Variable name
3/12/2014 19
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
X Y Variable name
25. Variable Data: Strings
When using & you can treat numeric variables as
StringsStrings
3/12/2014 25
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
27. Using Variables
Variable Declaration:
W ll d l i bl i h Di We normally declare a variable using the Dim
keyword.
We can declare multiple variables using a singleWe can declare multiple variables using a single
Dim keyword if we comma‐separate them.
3/12/2014 27
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
28. Using Variables
Variable Names:
I i d d fi It is recommended to use prefixes.
Use a 3 character prefix which indicates the type of
variable
V i bl t P fi E lVariable type Prefix Example
Boolean bln blnSuccess
Integer int intX
Double dbl dblYDouble dbl dblY
String str strfood
3/12/2014 28
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
29. Using Variables
Variable Assignment:
B f f i bl Before you can use any of your variables, you must
first assign them a value.
3/12/2014 29
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
31. Operators and Functions
Th i fi t i th f ll i The previous figure contains the followings:
Numbers (15, 26, 2.33 and 55)
Variables (x, y, and z)
Operators ( =, +, * and /)p ( , , /)
Functions (Sin, Sqr, Tan and Log)
3/12/2014 31
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
32. Operators in RhinoScript
Arithmetic Operators
C i O Concatenation Operators
Comparison Operators
Logical and bitwise Operators
3/12/2014 32
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
33. Operators in RhinoScript
Arithmetic Operators
C i O Concatenation Operators
Comparison Operators
Logical and bitwise Operators
3/12/2014 33
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
34. Operators in RhinoScript
Operator Precedence
The acronym PEMDAS or "Please Excuse My Dear
Aunt Sally" is common.
It stands for Parentheses, Exponents,
Multiplication, Division, Addition, Subtraction.Multiplication, ivision, Addition, Subtraction.
3/12/2014 34
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
35. Operators in RhinoScript
Operator Precedence
B d i i f h ld d h Based on previous info how could we code the
following?
PEMDAS
3/12/2014 35
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
36. Operators in RhinoScript
Operator Precedence
B d i i f h ld d h Based on previous info how could we code the
following?
PEMDAS
Solution:
y = Sqr(x ^ 2 + (x ‐ 1)) / (x ‐ 3) ) + Abs( (2 * x) / (x ^ (0.5 * x))
Solution:
3/12/2014 36
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
37. Operators in RhinoScript
Operator Precedence
T i lif h i d i To simplify the equation we can spread it out over
multiple lines of code
y = Sqr(x ^ 2 + (x ‐ 1)) / (x ‐ 3) ) + Abs( (2 * x) / (x ^ (0.5 * x))
3/12/2014 37
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
38. Operators in RhinoScript
Arithmetic Operators
C i O Concatenation Operators
Comparison Operators
Logical and bitwise Operators
3/12/2014 38
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
39. Operators in RhinoScript
Arithmetic Operators
C i O Concatenation Operators
Comparison Operators
Logical and bitwise Operators
3/12/2014 39
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
40. Operators in RhinoScript
Arithmetic Operators
C i O Concatenation Operators
Comparison Operators
Logical and bitwise Operators
3/12/2014 40
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
47. Functions in RhinoScript
Microsoft VBScript has 89 built in functions:
Sin(n) Sine of a number
Cos(n) Cosine of a numberCos(n) Cosine of a number
Atn(n) ArcTangent of a number
Log(n) Natural logarithm of a number larger than 0
Sqr(n) Square root of any positive numberSqr(n) Square root of any positive number
Abs(n) Absoluter (positive) value of any number
3/12/2014 47
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
48. Functions in RhinoScript
Rhino adds over 200 functions called methods:
Rhino.command (“_Move 0, 0, 0 2, 0, 1”)
Rhino.UnselectAllobjects()Rhino.UnselectAllobjects()
Rhino.Selectobject (strCurveID)
Rhino.Copyobject (strCurveID)
Rhino.GetObject (“Select an object”)Rhino.GetObject ( Select an object )
3/12/2014 48
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
49. What is a Function?
A function is a closed body of code that executes a
specific taskspecific task.
A function is like a black box.
It takes in input,
does something with it does something with it,
and then spits out an answer.
A function may not take any inputs at all or it may not
return anything at all.return anything at all.
3/12/2014 49
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
51. Why Would We Need Functions?
we can write it as a single function and access the
same function again and again as many times as it issame function again and again as many times as it is
required.
We can avoid writing redundant code of some
instructions again and again.
Programs with using functions are compact & easy to
understand.understand.
Testing and correcting errors is much easier.
3/12/2014 51
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
55. Script File Structure
Option Explicit:
It is optional, but it is highly
recommended.
If you use it, you have to define all
your variables before you can useyour variables before you can use
them.
If you don’t use it your variables If you don t use it, your variables
will be declared for you by the
compilercompiler.
3/12/2014 55
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
56. Script File Structure
Option Explicit:
It may also contains a set of
comments.
Comments are blocks of text in the
script which are ignored by thescript which are ignored by the
compiler and the interpreter.
comments are used to add comments are used to add
explanations to a file, or to disable
certain lines of codecertain lines of code.
Comments are always preceded by
3/12/2014 56
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
an apostrophe.
57. Script File Structure
Global Variables:
They are optional.
Typically you do not need globalTypically you do not need global
variables and you're usually better
off without them.off without them.
3/12/2014 57
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
63. IF….Then Statement
If we have three different situations:
1. If the object is a curve, delete it.
2. If the object is a short curve, delete it.2. If the object is a short curve, delete it.
3. If the object is a short curve, delete it, otherwise
it t th “ ” lmove it to the “curves” layer.
How would this be implemented using RhinoScript?
3/12/2014 63
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
64. IF….Then Statement
If we have three different situations:
1. If the object is a curve, delete it.
2. If the object is a short curve, delete it.2. If the object is a short curve, delete it.
3. If the object is a short curve, delete it, otherwise
it t th “ ” lmove it to the “curves” layer.
How would this be implemented using RhinoScript?
This is so easy. We just need to learn how
conditional syntax works.
3/12/2014 64
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
y
80. Select … Case (Sort objects based on type)
If h lIf h lIf the layer we are If the layer we are
about to assign does about to assign does
not yet existnot yet existnot yet exist…not yet exist…
3/12/2014 80
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
83. Looping
Executing certain lines of code more than once is
called looping in programming slangcalled looping in programming slang.
The two most important syntax for loops:
Conditional Loops (Do loop Do While loop Conditional Loops (Do…loop, Do While…loop,
Do Until…loop)
Incremental loops (For...Next)
3/12/2014 83
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
87. Conditional loops: Do…Loop
5: Main function declaration
6 W d l i bl hi h i bl f t i 6: We declare a variable which is capable of storing a
Rhino object ID
3/12/2014 87
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
90. Conditional loops: Do…Loop
10: Starting the loop
11 W d t h th t t d 11: We need to change the text once every second.
The Rhino.Sleep() method will pause Rhino for the
specified amo nt of milliseconds
3/12/2014 90
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
specified amount of milliseconds.
99. Arrays
An array is a list of variables.
Arrays start counting at zero.
The first element of an array is the element withThe first element of an array is the element with
index 0.
Arrays are just like other variables in VBScript with Arrays are just like other variables in VBScript with
the exception that we have to use parenthesis to set
and retrieve valuesand retrieve values.
3/12/2014 99
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
101. Arrays
you can also use a shorthand notation in which case
h h h h blyou have to omit the parenthesis in the variable
declaration:
The Array() function in VBScript takes any number of The Array() function in VBScript takes any number of
variables and turns them into an array.
You can add as many arguments as you like
3/12/2014 101
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com
106. Basic 3D Geometry: Dynamic Creation
Add Sphere
Exercise
Add Box
Add Cylinder
Please write a script
b
Exercise
Add Cylinder
Add Cone
to create a Box by
getting the length,
width and the height
Add Torus
width and the height
from the user.
Please do the samePlease do the same
for Cylinder, cone
and Torus
3/12/2014 106
Khaled Abd El Gawad – All rights reserved – www.khaledarch.com