kaust rhino workshop 2011

117
SCRIPTING IN RHINO KAUST ‐ April, 2011 Geometric Modeling and Scientific Visualization Center Geometric Modeling and Scientific Visualization Center Khaled Mohamed Ahmed Abd El Gawad www.khaledarch.com www.khaledarch.com Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Upload: khaled-ahmed

Post on 12-May-2015

148 views

Category:

Education


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Kaust rhino workshop 2011

SCRIPTINGINRHINOKAUST‐ April,2011

Geometric Modeling and Scientific Visualization CenterGeometricModelingandScientificVisualizationCenter

KhaledMohamedAhmedAbdElGawadwww.khaledarch.comwww.khaledarch.com

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 2: Kaust rhino workshop 2011

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

Page 3: Kaust rhino workshop 2011

Introduction

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 4: Kaust rhino workshop 2011

WhatWeCanDoUsingRhinoScript?

Computational Design Solution ‐ IAP Coursehttp://www.kaschaandjohn.com/rhinoscripting/

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 5: Kaust rhino workshop 2011

WhatWeCanDoUsingRhinoScript?

Khaled Abd El Gawad – All rights reserved – www.khaledarch.comComputational Design Solution ‐ IAP Course http://www.kaschaandjohn.com/rhinoscripting/

Page 6: Kaust rhino workshop 2011

WhatWeCanDoUsingRhinoScript?

Computational Design Solution ‐ IAP Coursehttp://www.kaschaandjohn.com/rhinoscripting/

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 7: Kaust rhino workshop 2011

WhatWeCanDoUsingRhinoScript?

http://www.rhinoscript.org/gallery/2

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 8: Kaust rhino workshop 2011

Scriptingvs.ProgrammingLanguages. 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

Page 9: Kaust rhino workshop 2011

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

Page 10: Kaust rhino workshop 2011

Macros Example:

A (0, 0, 0)

B(10, 0, 0)

Open100 Add line Save FilesRhino Files

Macros allow you to automate tasks you would normally do by hand but not by brain. 

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 11: Kaust rhino workshop 2011

Scripts Scripts are text files which are interpreted one line at 

ia time.

Scripts have control over which line is executed next.

We must familiarize ourselves with the language rules f VBS ript b f fl t lof VBScript before we can use flow control.

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 12: Kaust rhino workshop 2011

HowtoRunaScript? We could store scripts as external text files and have Rhino load them whenever we want to run themRhino load them whenever we want to run them

We could also use Rhino's in‐build script editor. _EditScript Command line_ p Tools  Rhinoscript Edit

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 13: Kaust rhino workshop 2011

HowtoRunaScript?The Script Window will show upshow up

Then we can write i t i t thour script into the 

Script Editor window th li k tthen click execute

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 14: Kaust rhino workshop 2011

HowtoRunaScript? Or we can embed scripts in toolbar buttons, which makes it very hard to edit them but much easier tomakes it very hard to edit them, but much easier to distribute them.

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 15: Kaust rhino workshop 2011

VBScriptEssentials

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 16: Kaust rhino workshop 2011

Languageorigin VBScript is a member of the BASIC language family.

The language was designed to be easy for humans to understand.

BASIC stands for Beginner’s All‐purpose Symbolic Instruction Code.Instruction Code.

Rhino Scripting syntax rules are basically the same as Rhino Scripting syntax rules are basically the same as Visual BASIC.

3/12/2014 16Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 17: Kaust rhino workshop 2011

FlowControl 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 17Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 18: Kaust rhino workshop 2011

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 memory3/12/2014 18Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

A visual representation of memory

Page 19: Kaust rhino workshop 2011

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 name3/12/2014 19Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

X Y Variable name

Page 20: Kaust rhino workshop 2011

Variable Data There are different data types: Integers, doubles, Booleans strings and Null variableBooleans, strings, and Null variable.

D blInteger

Double Boolean

NullSt i datadataString datadata

Variable name

3/12/2014 20Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 21: Kaust rhino workshop 2011

Variable Data: Integers and Doubles Doubles are numeric variables which can store numbers with decimalsnumbers with decimals. 

3/12/2014 21Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 22: Kaust rhino workshop 2011

Variable Data: Booleans Boolean variables can only store two values mostly referred to as Yes or No True or Falsereferred to as Yes or No, True or False.

In VBScript we never write "0" or "1" or "Yes" or "No”

We always use "True" or "False".

3/12/2014 22Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 23: Kaust rhino workshop 2011

Variable Data: Strings Strings are used to store text. 

Whenever you add quotes around stuff in VBScript, it automatically becomes a String. 

So if we encapsulate a number in quotes, it will become textbecome text

3/12/2014 23Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 24: Kaust rhino workshop 2011

Variable Data: Strings String Assignment and Concatenation

3/12/2014 24Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 25: Kaust rhino workshop 2011

Variable Data: Strings When using  & you can treat numeric variables as StringsStrings

3/12/2014 25Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 26: Kaust rhino workshop 2011

Variable Data: Null Variable When a variable has no value, it considered to be null.

Having a null value is different than having a value of 0, since 0 is an actual value.

Whenever we ask Rhino a question which might not have an answer, we need a way for Rhino to say "I don't know"

3/12/2014 26Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 27: Kaust rhino workshop 2011

Using Variables Variable Declaration: W ll d l i bl i h Di We normally declare a variable using the Dimkeyword.

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 27Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 28: Kaust rhino workshop 2011

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 ExampleBoolean bln blnSuccessInteger int intXDouble dbl dblYDouble dbl dblYString str strfood

3/12/2014 28Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 29: Kaust rhino workshop 2011

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 29Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 30: Kaust rhino workshop 2011

OperatorsandFunctions

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 31: Kaust rhino workshop 2011

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 31Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 32: Kaust rhino workshop 2011

Operators in RhinoScript Arithmetic Operators C i O Concatenation Operators Comparison Operators Logical and bitwise Operators

3/12/2014 32Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 33: Kaust rhino workshop 2011

Operators in RhinoScript Arithmetic Operators C i O Concatenation Operators Comparison Operators Logical and bitwise Operators

3/12/2014 33Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 34: Kaust rhino workshop 2011

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 34Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 35: Kaust rhino workshop 2011

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 35Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 36: Kaust rhino workshop 2011

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 36Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 37: Kaust rhino workshop 2011

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 37Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 38: Kaust rhino workshop 2011

Operators in RhinoScript Arithmetic Operators C i O Concatenation Operators Comparison Operators Logical and bitwise Operators

3/12/2014 38Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 39: Kaust rhino workshop 2011

Operators in RhinoScript Arithmetic Operators C i O Concatenation Operators Comparison Operators Logical and bitwise Operators

3/12/2014 39Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 40: Kaust rhino workshop 2011

Operators in RhinoScript Arithmetic Operators C i O Concatenation Operators Comparison Operators Logical and bitwise Operators

3/12/2014 40Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 41: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators

Logical operators mostly work on Booleans.

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.

M f h B l O N A d Most famous three Boolean Operators are: Not, And, and Or.

3/12/2014 41Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 42: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators

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) setBoolean sets, where every region contains a (sub) set of values that share a common property.

3/12/2014 42Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 43: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators

3/12/2014 43Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 44: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators

3/12/2014 44Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 45: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators

3/12/2014 45Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 46: Kaust rhino workshop 2011

Operators in RhinoScript: Logical Operators Venn diagrams are useful for simple problems, but once you start dealing with more than three regions itonce you start dealing with more than three regions it becomes a bit opaque.  6 i l V di P tt b t t ti l 6‐regional Venn diagram. Pretty, but not very practical

3/12/2014 46Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 47: Kaust rhino workshop 2011

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 47Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 48: Kaust rhino workshop 2011

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 48Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 49: Kaust rhino workshop 2011

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 49Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 50: Kaust rhino workshop 2011

Function Example

3/12/2014 50Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 51: Kaust rhino workshop 2011

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 51Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 52: Kaust rhino workshop 2011

Why Would We Need Functions? We can understand the flow of program, and its code easily since the readability is enhanced while using theeasily since the readability is enhanced while using the functions.

A single function written in a program can also be used in other programs also.

3/12/2014 52Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 53: Kaust rhino workshop 2011

Script File Structure Every script must implement a certain structure which tells the interpreter what's whattells the interpreter what s what.

3/12/2014 53Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 54: Kaust rhino workshop 2011

Script File Structure Every script must implement a certain structure which tells the interpreter what's whattells the interpreter what s what.

3/12/2014 54Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 55: Kaust rhino workshop 2011

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 55Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 56: Kaust rhino workshop 2011

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 56Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

an apostrophe. 

Page 57: Kaust rhino workshop 2011

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 57Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 58: Kaust rhino workshop 2011

Script File Structure The Main Function

Every script requires at least one function contains the main code of the script. 

it can place calls to any number ofit can place calls to any number of other functions 

It delineates the extents of the It delineates the extents of the script. 

The script starts running as soon as this function is called and it stops 

3/12/2014 58Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

when the function completes. 

Page 59: Kaust rhino workshop 2011

A Simple Function Example

3/12/2014 59Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 60: Kaust rhino workshop 2011

A Simple Function Example

3/12/2014 60Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 61: Kaust rhino workshop 2011

ConditionalExecution/Branching

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 62: Kaust rhino workshop 2011

Conditional execution or Branching IF….Then Statement

Select case LoopingLooping

Conditional loops Incremental loops

Nested LoopsNested Loops

3/12/2014 62Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 63: Kaust rhino workshop 2011

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 63Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 64: Kaust rhino workshop 2011

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 64Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

y

Page 65: Kaust rhino workshop 2011

IF….Then Statement 1. If the object is a curve, delete it.

2. If the object is a short curve, delete it.

3/12/2014 65Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 66: Kaust rhino workshop 2011

IF….Then Statement 3. If the object is a short curve, delete it, otherwise 

move it to the “curves” layermove it to the  curves  layer.

3/12/2014 66Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 67: Kaust rhino workshop 2011

Select … Case We use Select…Case when we want to check the  

equalityequality.

3/12/2014 67Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 68: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

An Integer to store the An Integer to store the Rhi ObjRhi Obj T dT dRhino ObjectRhino Object‐‐Type codeType code

3/12/2014 68Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 69: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

This means the object This means the object does not exist; abortdoes not exist; abort

3/12/2014 69Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 70: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

A String to store a layer A String to store a layer namename

3/12/2014 70Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 71: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Compare the actual Compare the actual d h hd h htype code with the type code with the 

preset onespreset ones

3/12/2014 71Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 72: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Points andPoints and PointCloudPointCloudPoints and Points and PointCloudPointCloudobjectsobjects

3/12/2014 72Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 73: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

CurvesCurves

3/12/2014 73Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 74: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Surfaces and Surfaces and PolySurfacesPolySurfacesPolySurfacesPolySurfaces

3/12/2014 74Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 75: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

MeshesMeshes

3/12/2014 75Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 76: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

LightsLights

3/12/2014 76Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 77: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Annotations and Annotations and TextDotsTextDots

3/12/2014 77Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 78: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

BlocksBlocks

3/12/2014 78Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 79: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Any other objects; Any other objects; AbortAbort

3/12/2014 79Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 80: Kaust rhino workshop 2011

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 80Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 81: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Create itCreate it

3/12/2014 81Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 82: Kaust rhino workshop 2011

Select … Case (Sort objects based on type)

Assign the object to the Assign the object to the layerlayer

3/12/2014 82Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 83: Kaust rhino workshop 2011

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 83Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 84: Kaust rhino workshop 2011

Conditional loops We use it when we do not know how many iterations 

we will need in advancewe will need in advance. 

This type is called a Do…Loop.

3/12/2014 84Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 85: Kaust rhino workshop 2011

Conditional loops: Do…Loop This example script contains an endless Do…Loop

which can only be cancelled by the user pressingwhich can only be cancelled by the user pressing (and holding) escape.

3/12/2014 85Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 86: Kaust rhino workshop 2011

Conditional loops: Do…Loop

1 & 2: Option Explicit declaration and comments about who's who and what's whatabout who s who and what s what

4: Main Function call

3/12/2014 86Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 87: Kaust rhino workshop 2011

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 87Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 88: Kaust rhino workshop 2011

Conditional loops: Do…Loop

7:We create a new Rhino Text object

Rhi AddT t ( t T t P i t [ dblH i ht [ t F t [Rhino.AddText (strText, arrPoint [, dblHeight [, strFont [, intStyle]]])

3/12/2014 88Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 89: Kaust rhino workshop 2011

Conditional loops: Do…Loop

8: Just in case the text object hasn't been created we need to abort the subroutine in order to prevent anneed to abort the subroutine in order to prevent an error later on.

3/12/2014 89Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 90: Kaust rhino workshop 2011

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 90Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

specified amount of milliseconds.

Page 91: Kaust rhino workshop 2011

Conditional loops: Do…Loop

12: Here we replace the text in the object with a new String representing the current system time.String representing the current system time.

3/12/2014 91Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 92: Kaust rhino workshop 2011

Conditional loops: Do While…Loop

This kind of loop syntax will abort the loop when S C di i i lSomeCondition is False.

3/12/2014 92Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 93: Kaust rhino workshop 2011

Conditional loops: Do Until…Loop

If we want the loop to terminate when a condition b i d f l h U ilbecomes True instead of False, we can use the Until keyword instead of the While keyword

3/12/2014 93Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 94: Kaust rhino workshop 2011

Conditional loops: Do…Loop While

You can be guaranteed that DoSomething will be ll d l b i h f ll icalled at least once by using the following syntax:

3/12/2014 94Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 95: Kaust rhino workshop 2011

Incremental loops

We use it when the number of iterations is known in dadvance.

The variable i starts out by being equal to A and it is incremented by N until it becomes larger than Bincremented by N until it becomes larger than B.

Once i > B the loop will terminate. 

3/12/2014 95Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 96: Kaust rhino workshop 2011

Incremental loops

Example

3/12/2014 96Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 97: Kaust rhino workshop 2011

Nested Loops

The following example shows how nested For…Next b d di ib istructures can be used to compute distributions:

3/12/2014 97Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 98: Kaust rhino workshop 2011

Arrays

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 99: Kaust rhino workshop 2011

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 99Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 100: Kaust rhino workshop 2011

Arrays

3/12/2014 100Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 101: Kaust rhino workshop 2011

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 101Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 102: Kaust rhino workshop 2011

Arrays

In RhinoScript, coordinates are defined as arrays of h b l d hthree numbers. Element 0 corresponds with x, element 1 with y and element 2 with z.

3/12/2014 102Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 103: Kaust rhino workshop 2011

Plane

3/12/2014 103Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 104: Kaust rhino workshop 2011

SurfaceandPolysurface Methods

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 105: Kaust rhino workshop 2011

Basic 3D Geometry: Static Creation

Add Sphere 

Add Box

Add Cylinder Add Cylinder

Add Cone

Add Torus 

3/12/2014 105Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 106: Kaust rhino workshop 2011

Basic 3D Geometry: Dynamic Creation

Add Sphere Exercise

Add Box

Add CylinderPlease write a script 

b

Exercise

Add Cylinder

Add Coneto 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 106Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 107: Kaust rhino workshop 2011

ShowCases

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 108: Kaust rhino workshop 2011

Example 1: Tree Example

3/12/2014 108Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 109: Kaust rhino workshop 2011

Example 1: Tree Example

3/12/2014 109Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 110: Kaust rhino workshop 2011

Example 1: Tree Example

3/12/2014 110Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 111: Kaust rhino workshop 2011

Example 2: Mesh Generation

3/12/2014 111Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 112: Kaust rhino workshop 2011

Example 2: Mesh Generation

3/12/2014 112Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 113: Kaust rhino workshop 2011

Example 2: Mesh Generation

3/12/2014 113Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 114: Kaust rhino workshop 2011

GrasshopperOverview

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 115: Kaust rhino workshop 2011

UsefulResources

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Page 116: Kaust rhino workshop 2011

Useful Resources http://wiki.mcneel.com/developer/rhinoscript

Everything about RhinoScript

http://www.rhino3d.com/developer.htm#vbs

Useful Resources about rhino scripting and Grasshopper

htt // hi i t / t t http://www.rhinoscript.org/start

Important community for RhinoScripting and 

3/12/2014 116Khaled Abd El Gawad – All rights reserved – www.khaledarch.com

Grasshopper

Page 117: Kaust rhino workshop 2011

Ihopeyouhaveenjoyedit!

Khaled Abd El GawadKhaledAbdElGawadwww.khaledarch.com

Khaled Abd El Gawad – All rights reserved – www.khaledarch.com