Wb Products Wb Models  Wb Wallpaper  Wb Tutorials  Wb Software  

DAZ SCRIPTING MADE SIMPLE
Volume 2: MATHEMATICS and LOOPING


FREE $0.00
Scroll down to read this complete training tutorial online completely free.
OR
Get the complete 38-page PDF for free by clicking below.


Or click one of these links to watch module(s) online at YouTube for FREE.
     
 
     

Need portability or just want to financially support more tutorials like this one?
Then please consider purchasing one or more of the following low-priced versions.
    

End User License Agreement

(EULA)

 

This tutorial, herein referred to as the product , is provided as-is with no warranties either expressed or implied.  The publisher retains all copyright ownership and reserves all rights to this product.  This product is only available directly from the publisher or an authorized reseller.  If you obtained a copy from any other source, please visit the publisher's website to purchase a legal copy for yourself.

* You MAY NOT copy, share or distribute copies of this product in any form without prior written permission from the publisher.  You MAY NOT modify, change or create derivative works based upon this product in any form or fashion or by any method.  You MAY NOT sell this product or include this product in any package or collection that is for sell.  You MAY NOT claim authorship in any form to this product.

* You MAY create, copyright and sell models using the techniques contained within this product.


 

Copyright (c) 2017, Winterbrose Arts & Graphics.  All Rights Reserved.

 



TABLE OF CONTENTS

 

Preparation. 4

Review.. 6

Case Sensitivity. 6

End Of Line. 6

Scripts Folder 6

Commenting Your Code. 6

Naming Conventions. 6

Common Prefixes. 6

Basic Math. 7

Mathematical Symbols. 7

Assignments. 7

Strings. 8

Operations. 9

Fractions. 10

Combining Strings. 11

Incrementing and Decrementing. 13

Combining Operation with Assignment 15

Precedence of Operators. 16

Grouping with Parenthesis. 16

Comparing Values. 18

Blocking Code. 19

Conditional Statements. 20

If - Else. 20

Switch - Case. 23

Looping Statements 27

COUNTING.. 27

For 27

NON-COUNTING.. 30

While. 30

Do - While. 32

Parsing Errors. 35

Code Blocks. 35

Copy - Paste. 35

Answer Sheet 36

Conclusion. 38


 

Preparation

 

To use DAZ Scripting, you must first install the DAZ Studio application.  For the purposes of this tutorial, we used version 4.9.  The latest version is available free from DAZ 3D using the link below.

 

Get DAZ Studio

{https://www.daz3d.com/get_studio}

 

DS49_Splashscreen.png

 

 

 

NOTE:

The following Review section is an abbreviated refresher of material covered in:
SMS Vol-1: Intro To Daz Scripting.


 

 

Layout and Style

 

For this tutorial, we will use Layout "City Limits Lite" and Style "Darkside".

 

To change your layout, on menu select Window / Workspace / Select Layout. In popup window, select City Limits Lite in Layout field and click Accept button.

 

DS48_Layout-01DS48_Layout-02

 

To change your style, on menu select Window / Style / Select Style. In popup window, select Darkside in Style field and click the Accept button.

 

DS48_Style-01DS48_Style-02

 

 

Script IDE Pane(tab)

 

You will be using the built-in Script IDE pane in DAZ Studio for the scripts we will be writing.  If you do not see the Script IDE pane or tab anywhere on your workspace, you can open it up from the main menu using Window / Panes (Tabs) / Script IDE.


 

Review

 

Case Sensitivity

 

When writing DAZ Script, you must be aware that it is a case-sensitive language.  That means a small letter like "a" is treated differently than a capital letter like "A".

 

End Of Line

 

Each instruction line in Daz Script should end with an enter/carriage return (sometimes referred to as a new line).  If you want more than one instruction on a line, you will need to separate each with the semi-colon ";" character.

 

Scripts Folder

 

You will find that DAZ Studio has a default folder for scripts located under the DAZ Studio Formats in the My DAZ 3D Library section.

 

Commenting Your Code

 

There are two ways to insert comments into your code.  A single line comment begins with two forward slash marks "//".  Multi-line comments are encapsulated by the forward slash asterisk "/*" pair and asterisk forward slash "*/" pair.

 

Naming Conventions

 

Identifier names you create; 1) must begin with a letter or underscore, 2) consist of letters, numbers or underscores, 3) must not contain spaces, 4) cannot contain special characters or punctuation, and 5) cannot be the same as any reserved words or functions used by DAZ Script.

 

Common Prefixes

 

Here is the list of prefixes that are commonly used for Daz Scripting:

 

a (Array), b (Boolean), n (Number), o (Object), and s (String).


 

Basic Math

 

Mathematical Symbols

 

You will find that the way math symbols look in programming is not the way they are shown in the real world.  This is in part an attempt to use only the symbols available in the ASCII character set.  The math symbols we will be discussing fall into three categories:  Assignment, Operation and Comparison.  We will be using the variable nJacks to store number of jacks for our basic math examples.

 

Assignments

 

The character used to assign values is the equal sign = .  This symbol invokes the action of assigning a value to a variable, string or other type. For instance, to assign the variable nJacks a value of 3, and the string sName a value of Dazzy, you could do it when it is declared or when you change or define it later on:

 

                      Assign in Declaration                   Assign Later On

                       var nJacks = 3                              nJacks = 3

                       var sName = Dazzy                    sName = Dazzy

 

                    Assign-01.png          Assign-02.png

 

                    Assign-04.png          Assign-03.png


 

 

Strings

 

We all know what numbers are (1, 347, 20), however we need to quickly discuss String variables before we get into the rest of this tutorial because string variables can use many of the same operators and comparisons as numbers.  String variables consist of characters that are not just numbers. When assigning data to a string variable or string constant, the group of characters comprising the information for the string must be encapsulated in quotation marks.

 

var sGreeting = Hello World ;  OR  var sGreeting = Hello World ;

 

String variables can consist of upper and lowercase text characters (a-z, A-Z), some special characters (-, _), and all of the numeric characters (0-9).  When used in strings, numeric characters look the same as numbers used for variables containing values, but are treated as characters and not as values.  Even though the Results for both examples below look the same, the variables are not.

 

                   Strings-02.png         Strings-03.png

 

You can use either the single-quote ( ) or double-quotes ( ) to encapsulate your string.  The Script IDE will use the first character of the declaration as the encapsulating character.  In this manner, you can start with a single-quote and still use the double-quote as a character(s) within the string.

 

Strings-01.png

 

Please note that within the print command, you may find times that you have to alter which type of quotes you use for it to function properly.


 

 

Operations

 

Several characters are used to perform the mathematical operations of addition, subtraction, multiplication and division.

 

                             +       Use plus sign for Addition

 

                             -        Use minus sign for Subtraction

 

                             /         Use forward slash for Division

 

                             *        Use asterisk for Multiplication

 

You can see each of the operators used in there examples:

 

        nJacks = 3 + 7        nJacks = 3 - 2        nJacks = 3 / 2        nJacks = 3 * 6

 

RESULTS

                 10                          1                          1.5                        18

 

   Operations-01.png        Operations-02.png

 

   Operations-03.png        Operations-04.png


 

 

Fractions

 

Did you notice the previous results had an answer of 1.5 ?  Daz Script can perform calculations with results that are fractions.  Fractions are represented in their decimal equivalents.  When you need to use a fraction and do not know its decimal equivalent, simply divide the two numbers using the Script IDE or any Calculator.  For instance, to calculate 1/2 would be scripted as nMyFrac = 1 / 2 with a result of .5 .  Another example would be three-seventeenths or 3/17 calculated as nMyFrac = 3 / 17 with a result of 0.17647058823529413

 

Fractions-01.png

 

Don t let fractions scare you because Daz Script can handle them without any problems.  As you write more advanced scripts, you will learn to code for them.

 


 

 

Combining Strings

 

If you ever develop code that must interact with the user for information, you may find that you need to combine different string information into a single string variable.  You can use the addition operator to add more characters to an existing string or even combine strings.

 

In this example, we have defined our string as Hello and have decided to add the name Johnny to the string as well.

 

Strings-04.png

 

You can also add two strings together to make a new string variable.  In this example, we have a string defined as Hello and another string defined as Johnny which we want to leave unchanged so we will combine them in a new string variable.

 

                                  sGreeting = Hello

                                  sName = Johnny

                                  sSayIt = sGreeting + sName

 

Strings-05.png


 

 

Did you notice in the previous examples that there was no spacing between the two parts that were combined?  For both, the results showed HelloJohnny .  Spacing may be required when combining two or more strings to make the string more readable.  You can include a space character within the quotes either before or after your characters in the strings you are combining, or you can simply add the space character with combining strings with the addition operator.

 

                                    var sGreeting = "Hello"

                                    var sName = "Johnny"

                                    var sSayIt = sGreeting + " " + sName

                                    print (sSayIt)

 

Strings-06.png

 

Not all operators work with string variables.  For instance, you cannot simply subtract a portion of your string using the subtraction (-) operator.  When your results display NaN , that means that the result of the calculation was Not a Number and therefore failed.

 

Strings-07.png

 

There are more advanced scripting commands and functions available to manipulate strings in Daz Studio.  Those will be covered more in depth in a later volume for more advanced programming concepts in Daz Script.


 

 

Incrementing and Decrementing

 

There will be times when you are using a variable and you simply want to add or subtract one from its value.  Just to be clear, incrementing is adding one (1) and decrementing is subtracting one (1).  Let s start by declaring our variable:

 

var nGoldCoins = 5;

 

Here is how you might normally be inclined to code adding or subtracting one:

 

nGoldCoins = nGoldCoins + 1;

print (nGoldCoins);

or

nGoldCoins = nGoldCoins - 1

print (nGoldCoins);

 

Inc-Dec-01.png

 

That all looks simple enough and it works; our variable nGoldCoins ended with a value of 6 .  However, if you are coding a very large project, using incrementing and decrementing will shorten the length of your code and perhaps make it easier for yourself and others to follow (or debug later on).


 

 

In order to simplify incrementing and decrementing, Daz Script implements two methods for both addition and subtraction called prefixing and suffixing.

 

Prefix .means increment/decrement variable before using variable

Postfix means increment/decrement variable after using variable

 

Here are the four ways you would code to add or subtract one after and before:

 

Change value after Print ing

                             print (nGoldCoins ++);             print (nGoldCoins --);

                             print (nGoldCoins);                  print (nGoldCoins);

 

Change value before Print ing

                             print (++ nGoldCoins);             print (-- nGoldCoins);

                             print (nGoldCoins);                  print (nGoldCoins);

 

Just take a look at the two screenshots below depicting incrementing our variable before and after using it in the Print operation.

 

      Inc-Dec-02.png      Inc-Dec-03.png

 

In the examples above, you can clearly see that the value of nGoldCoins was incremented on Line 2 both after and before the Print method was invoked by comparing results for the value of the variable as Print ed for Line 3 of the code.


 

 

Combining Operation with Assignment

 

To shorten the amount of typing required, you can use combinations of the operations and assignment character when the variable being operated is the same variable where you want to store the result.  For instance, let s say we want to increase the current value (whatever it is) of the variable nJacks by 3.  The legacy method was to assign the value of nJacks plus three to itself.  With the combined method you can save some typing as shown below.

 

                                  Legacy method     nJacks = nJacks + 3

                             Combined method     nJacks += 3

 

Combined-01.png

 

For the combined method to work properly, you must make sure that the operator symbol precedes the equal sign symbol.  The combined method works with all four basic mathematics operators.

 

              + =    Use this combo to add value then assign back into variable

              - =     Use this combo to subtract value then assign back in variable

              * =     Use this combo to multiply value then assign back into variable

              / =      Use this combo to divide by value then assign back into var

 


 

 

Precedence of Operators

 

There may be times when you find it necessary to perform several operations on a single line.  For example, say we want to add the numbers 1 and 5 and then multiply the result by 3.

 

                                     print (1+5*3)          result is 16

 

You might naturally write the code as shown above and expect a result of 18.  For instance, 1 + 5 = 6 and then 6 * 3 = 18.  The order of precedence used for mathematical operators is listed below.  Since Daz Script gives multiplication a higher order of precedence than addition, the values are multiplied first and then the 1 is added to the result.

 

                                     ( )        grouping

                                      *         multiplication

                                      /         division

                                     +        addition

                                      -         subtraction

 

Grouping with Parenthesis

 

Because Daz Script assigns precedence levels to each type of operator, you may want to group your calculations using the parenthesis characters ( ) .  If you cannot remember the order of precedence for operations, you should use a set of parenthesis to control the order by which operators are calculated by grouping them together.  This forces Daz Studio to calculate the group(s) first and then calculate based upon order of precedence.

 

                                   print ((1+5)*3)          result is 18

 


 

 

Practice Exercise - 01

 

It is time for some hands-on practice. Below a couple of practice exercises that use techniques covered so far.  Suggested answers are located in the Answer Sheet section located near the end of this tutorial.

 

A. Create a variable and assign it a formula that calculates the operations below, and then prints out the result:

 

Add 2 and 7 ..Multiply that result by 3 ..Subtract 4 from the total

 

B. Create two strings to contain your first and last name.  Combine the two strings and then print the output in a readable format.

 


 

Comparing Values

 

There will be times when you need to compare two numbers, variables or some other values.  You will use the appropriate comparator character(s) between the two parts being compared.  Since you are performing a comparison test between two items, the returned value will be a Boolean which will represent a True or False result.

 

                                           = =    Is equal to

                                           ! =     Is not equal to

                                           <       Is less than

                                           <=     Is less than or equal to

                                           >       Is greater than

                                           >=     Is greater than or equal to

 

How you use comparators

 

                   Example 1                                Example 2

                   var nJacks = 3                          var nJacks = 21

                   print (nJacks >= 3)                   print (nJacks < 5)

                   print (nJacks != 6)                    print (nJacks == 19)

 

                   Compare-01.png          Compare-02.png

 


 

Blocking Code

 

Before we move on into the looping functions, we first need to cover how to group (otherwise known as blocking ) several code actions together.  To block together several lines or commands of code, we use the Open { and Close } Brackets. 

 

Don t worry if this doesn t make much sense right now, because as we move through more examples of scripting it will become more clear.

 

You can either block the code together on one line with semi-colons separating commands/functions, or you can spread it out over several lines which is usually more readable.  Here is the same example shown using both methods.

 

var nMyIncome = 21000

var nTaxRate = 7

var nMyTaxes = 0

if (nMyIncome>0)

{nMyTaxes=nMyIncome*(7/100);print(nMyTaxes)}

else

{print("You do not owe any taxes this year")}

 

var nMyIncome = 21000

var nTaxRate = 7

var nMyTaxes = 0

if (nMyIncome>0)

{

nMyTaxes=nMyIncome*(7/100)

print(nMyTaxes)

}

else

{

print("You do not owe any taxes this year")

}

 

You can see how the second version is much more readable.  This may not be as important with small scripts you write, but as you learn to write long and intensive scripts, you will find it much easier to modify and debug by using more lines.


 

Conditional Statements

 

The conditional portion used in certain functions simply is the method by which your code can decide to either execute or continue to execute a predetermined action.  In layman s terms, you could say only do this under certain conditions .  By using conditional functions, your code can decide to execute action(s) only while a condition is met, not met, or matches one of several choices.

 

Decisions, Decisions

The use of conditional statements in Daz Scripting
allows your code to make decisions while it is
executing without the need for human intervention.

 

 

If - Else

 

The If-Else statements are one of the easiest for people to relate to.  For a moment, just imagine you are talking to another person, and say If I tell you that it was the truth, then it was so believe me; otherwise it was not the truth, so believe the other person .  That is a rough example of how the If-Else statement makes its own decisions.  It starts with if and the condition which must be met to execute the listed action(s); followed by the else which will execute any alternative action(s).  Here is the overall structure of the IF-ELSE statement:

 

IF (condition) {do something} ELSE {do something else}

 

PREP - We are going to build a complete if statement and explain each piece of it as we write it.  This example will be determining if a person has reached a predetermined drinking age, and if so they can have an alcoholic beverage, otherwise they will be declined. In order to demonstrate this function, we will need some items we can use for our decision making.

 

var nMyAge = 28


 

 

FUNCTION Of course the conditional statement line will start with the if function name as that is what we are demonstrating.

 

var nMyAge = 28

if

 

CONDITION Now let s build the condition for this example.  We will be using 21 as the legal drinking age.  We need our code to decide on its own if this person has reached (or passed) the legal drinking age.

 

var nMyAge = 28

if (nMyAge >= 21)

 

DO SOMETHING Now we must list any action or actions that need to be executed if the condition is met.  In this case, if the person has reached the legal drinking age, they can be served a cold beverage.

 

var nMyAge = 28

if (nMyAge >= 21)

{print( You are old enough for a cold beer! )}

 

At this point, we have enough code to execute the script.

 

If-Else-01.png

 

Wait a minute!  What happened to the else portion of our conditional statement? Don t worry, Daz Script works with or without the else portion because there are many times when you just want to take action for a condition that is met, and do not need to do anything if it is not met. However, we will continue on with building our example.


 

 

OTHERWISE - Here is where we will use the else portion of the if function, but only if we require action(s) to take place for a condition that is not met.

 

var nMyAge = 28

if (nMyAge >= 21)

{print( You are old enough for a cold beer! )}

else

 

DO SOMETHING ELSE -  If you use the else statement, then you must provide a set of open and close brackets { } whether you choose to have set of alternate action(s) or not.  We wrap up this example

 

var nMyAge = 28

if (nMyAge >= 21)

{print( You are old enough for a cold beer! )}

else

{print( Sorry, but you are not old enough. )}

 

SUCCESS!  The script executed without any errors, so I guess this person will get a drink today to relax after a hard day or work or play.

 

If-Else-02.png

 

 

More Than One Action Required?

To include multiple actions for any conditional response, be sure to write them between the brackets { } and use the semi-colon character (;) at the end of  each action s line.


 

 

Switch - Case

 

The Switch-Case statements are slightly more advanced than the If-Else functions you learned previously.  With this set of commands, you can have more than just two outcomes (decisions) based upon the result of a specified expression or condition.  This function also includes use of the break and default statements.  You can include as many case matches as needed, and use the default match if all other matches fail.  Once we complete the example code it will all make more sense. Here is the overall structure of the SWITCH-CASE statement:

 

SWITCH ( expression )
          {
          CASE value1:
                   do something
                   BREAK;
              ----------
                    (
                     )
              ----------
          CASE valueX:
                   do something
                   BREAK;
          DEFAULT:
                   do something
                   BREAK;
          }

 

If you look closely, instead of using brackets to block your code, everything is blocked together by the case and break statements for each match test.

 

PREP - We are going to build a complete switch-case statement and explain each piece of it as we write it.  This example will be determining the corresponding name for the specified day of the week based upon a number from 1 through 7.

 

var nDayOfWeek = 3

 


 

 

STATEMENT:  We will begin our switch-case with the switch statement and the expression that we will be evaluating containing our day of the week number. To simplify our example, we are going to go ahead and include the opening and closing brackets for the switch-case statements block.

 

var nDayOfWeek = 3

switch (nDayOfWeek)

{

}

 

MATCHING (DO SOMETHING):  We will now add our first matching test using the case and break statements.

 

var nDayOfWeek = 3

switch (nDayOfWeek)

{

          case 1:

                   print(nDayOfWeek)

                   print( Sunday )

                   break;

}

 

Since we know that there are 7 days in the week, we will go ahead and add all of them between the brackets with the same format as the first case statement.  Which day you choose to be the first and last day of the week is completely up to you.

 

          case 2:

                   print(nDayOfWeek)

                   print( Monday )

                   break;

 

through

 

          case 7:

                   print(nDayOfWeek)

                   print( Saturday )

                   break;


 

 

DEFAULT (DO SOMETHING):  Of course, if your code does not find any matches, you have the option of including a default so that something will still happen.  This is not always needed depending on the purpose of your script, but may prove useful until you complete the debugging and testing of your script.

 

          default:

                   print(nDayOfWeek)

                   print( No Match Found )

                   break;

 

SUCCESS!  The script executed properly as shown in this reduced size snippet.  You will find the complete script on the following page.

 

Switch-Case-01.png


 

 

FULL SCRIPT:  Here is the complete code for the Switch-Case Example.

 

var nDayOfWeek = 3

switch (nDayOfWeek)

{

          case 1:

                   print(nDayOfWeek)

                   print( Sunday )

                   break;

          case 2:

                   print(nDayOfWeek)

                   print( Monday )

                   break;

          case 3:

                   print(nDayOfWeek)

                   print( Tuesday )

                   break;

          case 4:

                   print(nDayOfWeek)

                   print( Wednesday )

                   break;

          case 5:

                   print(nDayOfWeek)

                   print( Thursday )

                   break;

          case 6:

                   print(nDayOfWeek)

                   print( Friday )

                   break;

          case 7:

                   print(nDayOfWeek)

                   print( Saturday )

                   break;

          default:

                   print(nDayOfWeek)

                   print( No Match Found )

                   break;

}


 

Looping Statements

 

COUNTING

 

If you continue to code using Daz Script, you are eventually going to need a way to count up, down, or off using a defined set of numbers.  You can do this easily enough one number at a time.

 

For

 

For those times when you need to count a series of numbers, say count up from one to ten, you can use the for function.  In many languages, the FOR statement requires an ending Next statement to close the loop.  However, in Daz Script we contain the loop using the open { and close } brackets.  You can refine how accurate the counting will be, but for most cases you will either increment or decrement by one.  Here is the overall structure of the FOR statement:

 

FOR (var;condition;change) {do something}

 

VAR - We are going to build a complete for statement and explain each piece of it as we write it.  Let s start with the var which is simply a variable which we will be using to count in the function.  You can declare any valid variable name you desire as long as you haven t declared it elsewhere in your code.  We will name our variable a .

 

for (var a = 1;

 

CONDITION - The next part of the for statement will be the condition.  This can sometimes be the tricky part as you may need to think on it. Let s say we are going to count from 1 to 10.  So basically keep counting while a is less than eleven or not equal to 10.  We will start by trying the function using less than eleven.

 

for (var a = 1; a < 11;


 

 

CHANGE - The change will be how you want your variable counter to either increase or decrease.  For this part, you will use the increment and decrement methods discussed earlier.  Be sure that you code it so that the change occurs after the variable is used.

 

for ( var a = 1; a < 11; a++)

 

DO SOMETHING This is the fun part; anything you place in this portion will be executed and repeated as many times as the loop runs for the Condition you set.  What you want to be performed must be contained within a set of open and close brackets { } .  You can write it all on one properly formatted line, or use several lines to make it more readable.  Obviously the latter makes more sense if you have more things that you want to happen.  Here is the function shown in both ways.

 

for ( var a = 1; a < 11; a++) {print(a)}

 

for ( var a = 1; a < 11; a++)

{

print(a)

}

 

SUCCESS!  The script executed without any errors and that is almost always a good thing.  More importantly, it counted from 1 to 10 which is what we desired.

 

For-Next-01.png


 

 

Practice Exercise - 02

 

A. Try changing the age of our person in the if-else example.  Was the outcome different, or can you tell? Or, try adjusting the drinking age in line 2.

 

var nMyAge = 18

if (nMyAge >= 21)

{print( You are old enough for a cold beer! )}

else

{print( Sorry, but you are not old enough. )}

 

 

B. What do you think will happen if you go back and use a = 10 in the Condition portion of the script?  Do you think the results will be the same?  Try out this revised version of the for example which looks like it makes sense.

 

for ( var a = 1; a = 10; a++)

{

print(a)

}

 

Or how about changing the Change portion of the for example.

 

for ( var a = 1; a = 10; ++a)

{

print(a)

}

 


 

 

NON-COUNTING

 

While this module is titled Looping , the commands and techniques used do not always have a predetermined range or number of loops like we previously used with the for function for counting.

 

The WHILE and DO-WHILE functions incorporate conditional statements to control when the loop terminates based on something other than a count.  These functions do not make any changes in the statement line, therefore the code block may have to change the condition tested in order to exit the loop.  While both functions look very similar, there is one distinct difference that sets them apart.

 

While

 

This statement executes one or more lines of code while a condition is true.  Because of the way it is designed, the code block may not execute at all.  If the condition is false when it begins, then any action(s) listed will be skipped.  Here is the overall structure of the WHILE statement.

 

WHILE (condition) {do something}

 

The condition must be properly set before calling this statement.  Let s build a complete while example and explain each piece of it as we go.

 

PREP - We have a very large pile of apples, but only need one dozen (12) for the jelly we are about to make.  So let s start by declaring the number of apples in the pile we have.

 

var nApples = 17

 

STATEMENT - Obviously, we will begin our code with the while statement.

 

var nApples = 17

while

 


 

 

CONDITION - As stated earlier, we only want 12 apples for our jelly.  So we have to see if we have more apples than we need.

 

var nApples = 17

while (nApples > 12)

 

DO SOMETHING - If there are more apples than we need, we have to remove them one at a time.

 

var nApples = 17

while (nApples > 12)

{

print("I said only one dozen, please remove an apple")

nApples --

print("we now only have " + nApples + " in the pile")

}

 

SUCCESS!  The script executed without any errors and we ended up with only the one dozen apples we need to make our jelly.

 

While-01.png

 


 

 

Do - While

 

This statement executes one or more lines of code while a condition is true, and is designed so that the code will execute at least once regardless of the condition.  This is because the code is executed before the condition is checked.  Here is the overall structure of the DO - WHILE statement.

 

DO {something} WHILE (condition)

 

PREP - We are at a shooting range and have been instructed to only fire our weapon until we get that perfect shot.  This means we must fire our gun at least once.

 

var nShotsFired = 0

var bBullsEye = false

 

STATEMENT - We will begin our code with the do statement. We have to take at least one shot, so this statement works great for this.

 

var nShotsFired = 0

var bBullsEye = false

do

 

DO SOMETHING - Since we are very skilled, we fire our weapon the first time and manage to get a bull s eye.

 

var nShotsFired = 0

var bBullsEye = false

do

{

     print("Fire your weapon")

     nShotsFired ++

     bBullsEye = true

}

 


 

 

CONDITION - We must keep firing our weapon as long as we have not gotten a bull s eye on the target.  Of course we already have, so the loop should exit.

 

var nShotsFired = 0

var bBullsEye = false

do

{

     print("Fire your weapon")

     nShotsFired ++

     bBullsEye = true

}

while (bBullsEye = false)

 

SUCCESS!  The script executed without any errors for our shooting practice.  We wrapped up the do - while statement with a report on how well we did.

 

Do-While-01.png

 


 

 

Practice Exercise - 03

 

Here are a few more practice exercises to develop your skills.  Suggested answers are located in the Answer Sheet section located near the end of this tutorial.

 

A. For our WHILE example, change the beginning number of apples to 12 and see what happens to the code to be executed.  Before you run the script, what do you think will happen?

 

 

B. For our DO-WHILE example, change the code so that it takes 5 shots to get the bull s eye and exit the loop.

 

 

 

 

 


 

Parsing Errors

 

When testing your code, you may run into errors that require debugging your script.  You can get parsing errors if you do not properly encapsulate blocks of code, or if you use copy-and-paste functions to insert coded into the Script IDE.

 

Code Blocks

 

You should know by now that we block code and conditionals using either the parenthesis () and brackets {}.  When you are developing your scripts, be sure that you use an equal number of parenthesis and brackets.  For every open parenthesis/bracket, there should be one close parenthesis/bracket.

 

If your code has blocks or conditional statements, then check the number of open and close parenthesis/brackets you have used.

 

Copy - Paste

 

Sometimes when pasting code into the Script IDE from other sources such as a word processor or PDF reader, the paste may include unseen characters that cannot be processed by the Script IDE.

 

To correct these types of problems, there are two methods you can use:

 

1. Use copy-paste in ASCII only text mode without special control characters.

 

2. Delete all special characters that may have been pasted into Script IDE.


You can do this by moving the cursor to beginning of problem line, and then cursor to the right one character at a time.  If the cursor did not move when you hit the arrow-key to move it right, you have just passed an unseen special character and should press backspace to remove it.  Continue moving to the right until you have removed all unseen characters in this fashion.

 


 

 

Answer Sheet

 

EXERCISE 01-A

 

Exercise-01-A.png

 

EXERCISE 01-B

 

Exercise-01-B.png

 

EXERCISE 03-A

 

Exercise-03-A.png


 

 

EXERCISE 03-B

 

Exercise-03-B.png

 


 

Conclusion

 

We hope that you have enjoyed this tutorial and have found some inspiration to further develop your scripting experience to control the Daz Studio environment. Please check our website for more volumes in the Scripting Made Simple series to build and advance your scripting skills in such topics as advanced mathematics, controlling objects in your scene, and creating your own thumbnails for scripts.

 

To financially support more tutorials like this one, please consider purchasing one or more
of the following reasonably priced versions of this product in complete PDF, MP4 or WMV.
PDF just $2.00 and/or VID just $5.00
      

 

WAG-Be-Yourself-and-Succeed_2016.png

 


Best viewed at
1024 x 768
or higher.
copyright Thanks for Visiting
WINTERBROSE
Arts & Graphics