TB1 Week 5


Cmds.move needs to be investigated

If you create an object then use the move function it will automatically select it, otherwise you have to manually select it with cmds.select

import maya.cmds

def Create_Animation(Animation_Range):

    cube = cmds.polyCube()

    for i in range(Animation_Range):

        cmds.currentTime(  (i+1) *10, edit=True  )

        cmds.setKeyframe(cube, v=i, at="translateX")

        

Range = 10

if cmds.window(  "my_Window", exists = True):

    cmds.deleteUI("my_Window")

    

window = cmds.window("my_Window", title="Long Name", iconName='Short Name', widthHeight=(200,55)  )

cmds.columnLayout( adjustableColumn=True)

cmds.button( label='Create Animation', c = "Create_Animation(Range)" )

cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )

cmds.setParent( '..' )

cmds.showWindow( window )

^^This code creates a window in maya and also calls a function which will create a cube and move and set keyframes for it in increments of 10, it does this using for loops to repeat commands and move things.

import maya.cmds

def Create_Custom_Cube_Animation(Animation_Range, Attribute_Name,Width,Height,Depth):

    cube = cmds.polyCube(w = Width, h = Height, d = Depth)

    for i in range(Animation_Range):

        cmds.currentTime(  (i+1) *10, edit=True  )

        cmds.setKeyframe(cube, v=i, at=Attribute_Name)

def Create_Cube_Animation(Animation_Range, Attribute_Name):

    cube = cmds.polyCube()

    for i in range(Animation_Range):

        cmds.currentTime(  (i+1) *10, edit=True  )

        cmds.setKeyframe(cube, v=i, at=Attribute_Name)

        

def Create_Animation(Animation_Range, Attribute_Name):

    for i in range(Animation_Range):

        cmds.currentTime(  (i+1) *10, edit=True  )

        cmds.setKeyframe(cube, v=i, at=Attribute_Name)

        

Range = 10

Attribute = "translateY"

Width = 10

Height = 10

Depth = 10

if cmds.window(  "my_Window", exists = True):

    cmds.deleteUI("my_Window")

    

window = cmds.window("my_Window", title="Long Name", iconName='Short Name', widthHeight=(200,55)  )

cmds.columnLayout( adjustableColumn=True)

cmds.button( label='Animate Cube', c = "Create_Cube_Animation(Range,Attribute)" )

cmds.button( label='Custom Animated Cube', c = "Create_Custom_Cube_Animation(Animation_Range, Attribute,Width,Height,Depth)" )

cmds.button( label='Create Animation', c = "Create_Animation(Range,Attribute)" )

cmds.button( label='Close', command=('cmds.deleteUI(\"' + window + '\", window=True)') )

cmds.setParent( '..' )

cmds.showWindow( window )

^^This code creates a window in maya that allows you to automatically a) create a cube and animate it b) create a cube of custom dimensions and animate it and c) animate the selected object. This is done by creating different buttons that call different functions, each of which have common and different attributes assigned to them.

Make a while loop that counts from 1 to 10 in threes.

i = -2

while i < 10:

  i += 3

  print(i)

Creating a function which converts celsius to fahrenheit.

def temp_conversion(celsius):

  fahrenheit = celsius * 1.8000 + 32

  print(celsius, fahrenheit)

        

temp_conversion(200)

Create a while-loop that runs indefinitely and each time it executes it asks the user to input a figure in C to be converted to F degrees, then whenever the user inputs a particular value, your function should be called to perform the conversion from C to F degrees.

def temp_conversion(celsius):

  fahrenheit = celsius * 1.8000 + 32

  print(fahrenheit)

while input("Do you want to convert more values Y/N?") == "Y":

  #allows the loop to stop

  celsius = float(input("Put celsius in to convert into fahrenheit: "))

  #input is a float value as the function contains a float and it will also convert string value to float

  temp_conversion(celsius)

The next step is to test and make sure that the script works under different condition, as the input is read as a float, it works whether someone inputs an integer or a float, however string inputs are an issue as they will cause an error.

My solution was the following code which uses a list of accepted characters to make sure that people are only writing characters that are used to define numbers, after the check has been passed it would call the function, otherwise it would warn the user and break the loop, starting it over again.

def temp_conversion(celsius):

  fahrenheit = celsius * 1.8000 + 32

  print(fahrenheit)

while input("Do you want to convert more values Y/N?") == "Y":

  #allows the loop to stop

  celsius = input("put celsius in to convert into fahrenheit: ")

  #input is a float value as the function contains a float and it will also convert string value to flaot

  Is_word = True

  Accepted_list = ["0","1","2","3","4","5","6","7","8","9","-","."]

  #checks that it is a character used for numbers

  for i in celsius:

        if i not in Accepted_list:

          Is_word = False

        if Is_word == True:

      #for loop that checks if an input is using acceptable characters

          temp_conversion(float(celsius))

          #converts the list iteration into a float

        else:

          print("Enter a number please: ")

          break

          #stops the loop from continuing

To practice my use of functions I went back and did one of the previous exercises using the module marks, and turned each calculation into different functions.

module1 = 40

module2 = 50

module3 = 70

module4 = 90

def sum(module1, module2, module3, module4):

  total_marks = module1 + module2 + module3 + module4

  print("Overall marks across all module are " + str(total_marks))

  #adds together all marks

sum(40, 50, 70, 90)

def Units_Two_And_Three(module2, module3):

  quarter_mark = (module2 + module3)/4

  print(" 25% of the overall mark achieved in modules two and three is " + str(quarter_mark))

  #adds module 2 and 3 marks and divides by 4 for the quarter

Units_Two_And_Three(50,70)

def Average_Overall(module1, module2, module3, module4):

  Average = (module1 + module2 + module3 + module4)/4

  print("The average overall mark achieved across all four modules in year 1 equals " + str(Average))

  #adds up all marks and divides by 4 for average overall

Average_Overall(40,50,70,90)

def Weighted_Average(module1, module2, module3, module4):

  Weight_Average = (module1*0.2)+(module2*0.2)+(module3*0.2)+(module4*0.4)

  print("the weighted average overall mark achieved across all modules in year 1 is " + str(Weight_Average))

  #weights the average marks by the worth of each module

Weighted_Average(40,50,70,90)

Function checklist!

1. Did you start your function definition with def?

2. Does your function name have only characters and _ (underscore) characters?

3. Did you put an open parenthesis ( right after the function name)?

4. Did you put your arguments after the parenthesis ( separated by commas)?

5. Did you make each argument unique (meaning there was no duplicated names)?

6. Did you put a close parenthesis ) and a colon : after the arguments?

7. Did you indent by four spaces all lines of code you want in the function? No more, no less?

8. Did you “end” your function by going back to writing with no indent?

 

And when you run (“use” or “call”) a function, check these things:

 

1. Did you call/use/run this function by typing its name?

2. Did you put the (-character after the name to run it?

3. Did you put the values you want into the parentheses separated by commas?

4. Did you end the function call with a )-character?

Create a function that sorts three float values and prints the result.

def myOrder(numbers):

  numbers.sort()

  print(numbers)

Unorderednumbers = [3.5, 2.3, 7.8]

myOrder(Unorderednumbers)

In python you can use randomised elements for various different purposes but you must not forget to import random first otherwise no command will work

print(random.random()) will print a random float between 0.0 and 1

print(random.uniform(1, 10)) will print a random float between 1 and 10

print(random.randrange(10)) will print a random integer between 0 and 9

print(random.randrange(0, 101, 2)) will print a random even integer between 0 and 100

print(random.choice('abcdefghij')) will print a random element from the list

items = [1, 2, 3, 4, 5, 6, 7]

random.shuffle(items)

print(items) will shuffle the order of the items in the list and print them

print(random.sample([1, 2, 3, 4, 5], 3)) will print 3 random samples from the list

A for loop that generates random integers between 10 and 590.

import random

for x in range(101):

  print(random.randrange(10, 590))

Finally, let us recap on previous experiences by writing a Python script to do the following:

(i) first declare an integer variable and

(ii) set its initial value to 0, then

(iii) ask the user to enter a number, then

(iv) store the entered number in the declared variable and finally

(v) tell the user what their entered number was.

Can you write a Python script using a function to do exactly this? You can call it Enter_Number. Make sure that you test that your script is working correctly.

def Enter_Number(var):

  var = 0

  var = input()

  print(str("your number is " + var))

Enter_Number(0)

Leave a comment

Log in with itch.io to leave a comment.