Tuesday, 22 October 2024

Learning Python with Houdini Part 2

 This week we're focusing on functions and how to make them.

  • Function: re-callable code used to perform basic tasks
  • Module: a file that contains functions and classes
  • Package: multiple modules assembled together in one collection (like a directory folder)
Python only includes stuff it needs. So if you need something extra you have to import them yourself
import moduleName
moduleName.function(arguments)

This is an example of how to import and then call a function. Module name is called Name Space, it references which module it is from. We could also import just the functions we want from them.
from moduleName import identifier(, indentifierB, idenifierC)
from moduleName import *
    Identifier = function contained in module. That last line includes a wildcard (*), and what happens is that now it imports all those functions and says you do not need to include module name anymore when calling functions. 
    If you wanted to change the module name, you can do:
import math as foo
foo.cos(pi)
output: -1.0 (cosine of pi is -1.0)
    You can even change the individual function names as well
from math import fmod as snafu
snafu(5,12)
output: 5.0

Random Module

    We're going to be using the random module for this next assignment. 
import random 
print(random.random())
    If you call this script it will always give you a random number. If you want it to be a predictable random number, you need to set the seed first.
random.seed(84.76)
print(random.random())
    This will give you the same random number every time. (Guess it's not really random but this can be useful). If you want a random number inbetween a minimum and a maximum, you can use randuniform or randint (will only give integers).
random.uniform(-30,-10)
random.randint(2,20)
random.randrange(3, 9,2)
    randrange will give you every second value in between 3 and 9 (so only 3, 5, 7, 9)
random.choice('FUBAR')
    Choice will give a random value from the sequence you give it.

Declaring Functions

def function-name(parameter-list):
    statements
    return someValue
     The basic structure of a function. It doesn't always have to return a value.

Recreating the clamp function

def clam(inputValue, min, max):
    result = inputValue
    if result < min:
        result = min
    if result>max:
        result = max

    return result
 
Potentially this could work with strings.

Scope of variables

    If you make a variable inside a function, it exists only within the function that's within the scope of that function, this is a local variable.
    If you make a variable outside of a function, it is a global function and can be accessed anywhere.
    If you are inside of a function, you can specify if you want to use the global variable

    The variable inside of the b function will manipulate the global value of foo. 
    You can set default values in functions if the function is called and doesn't give any values
def volume (length=1, width=1, depth =1)
    return length*width*depth

print(volume())

    This will output 1. You can also change the order you put the variables in

print(volume(depth=5, length=3, width=2)


Recursion

    A recursive function is a function that calls itself either directly or indirectly. It must include a base case which is the simplest case to solve for (if it doesn't hit this base case then it will continue infinitely). Must also contain a recursive call to itself. An example is a factorial function:
def fact(number):
    if number <1:
        return 1
    else:
        return number *fact(number-1)
    This will call the factorial function, each time decrementing the number until it is less than 1.
    Here's a bonefinder function that searches for all of the bones in a houdini object.





 

No comments:

Post a Comment

Shader Tool Week 3 - Surface Mapping

 Texture Cubes     Also known as cube maps, its 6 2D texture maps corresponding to the faces of an axis-aligned cube. Used in lighting a sce...