TB1 Week 3
Conditions and if statements.
An if statement checks whether a condition has been filled before executing its statement.
E.g.
a = 33
b = 200
if b > a:
print("b is greater than a")
It is important that there is the correct amount of white space on the line of the print statement, as it would otherwise cause a syntax error. Additionally it is important to remember the colon : symbol after finishing the if statement.
An elif statement checks for an if statement if the first if statement is not true, as a means of “if this isn’t true check this” course of action.
E.g.
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
An else statement is used to catch the results that were not considered in the previous conditions.
E.g.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
#note that the else statement does not require an elif
Else and if statements can also be written shorthand in one line as long as they do not have more than one statement.
E.g.
a = 330
b = 330
print("A") if a > b else print("=") if a == b else print("B")
There are also additional logical operators that can be used during statements to modify the way they are checked. These are ‘and’ and ‘or’ keywords. They are used to combine conditional statements in different ways.
E.g.
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Some additional information for if statements is that they can be placed inside other if statements, this is known as a ‘nested if’, also, if an if statement is to be left blank then it must have ‘pass’ written as its statement or it will cause a syntax error.
E.g.
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
#this is an example of a nested if statement
a = 33
b = 200
if b > a:
pass
#this shows how an if statement can be left empty using the pass keyword
!= means does not equal
Test code to create a window in maya with different buttons with different functionality
E.g.
import maya.cmds
if cmds.window("My_Window_New", exists = True):
cmds.deleteUI("My_Window_New")
def createCube():
cmds.polyCube(sx=10, sy=15, sz=5, h=20)
#function for creating a cube
My_window = cmds.window("My_Window_New", title="Long Name", iconName='Short Name', widthHeight=(200, 55) )
cmds.columnLayout( adjustableColumn=True)
cmds.button( label='Do Nothing')
#button that does nothing
cmds.button( label='Close', command=('cmds.deleteUI(\"My_Window_New", window=True)') )
#creates a button which will close the window
cmds.button( label='Cube', command= "createCube()")
#creates a button which will spawn the createCube cube
cmds.setParent('..')
cmds.showWindow( "My_Window_New" )
Programming for Animation Devblog
Status | Prototype |
Category | Other |
Author | Pomanchocho |
More posts
- TB1 Week 10Dec 05, 2022
- TB1 Week 9Dec 05, 2022
- TB1 Week 8Dec 05, 2022
- TB1 Week 7Dec 05, 2022
- TB1 Week 6Dec 05, 2022
- TB1 Week 5Dec 05, 2022
- TB1 Week 4Dec 05, 2022
- TB1 Week 2Dec 05, 2022
Leave a comment
Log in with itch.io to leave a comment.