Tuesday, 22 October 2024

Learning Python with Houdini Part 1

Running Scripts Inside of Houdini

    You can either run python commands directly in the Windows > Python Shell. 


    Or you can create a new shelf to store your new scripts. Click on the plus in the top toolbar to create a new shelf, and then right click on the shelf to create a new tool. 

Getting Input

Note:
Houdini seems to have an issue if you try to get input from the issue that lasts multiple lines. To ensure that Houdini won't crash, just get all user input from one line. 
To get user input:
inputvar = input("Enter first and second Values:")

Once you have your input, you need to split up all of the inputs from that one input variable. To split up your values, use split()
first, second = input.split()

    This will get you the first input value into the variable 'first' and the second input value into the variable 'second'. Now to change the input string into integers to be used, use int()
first = int(first)

Strings

    You can delineate a variable as a string by using single quotes (') or double quotes (").

    var = "this is a string" or var = 'this is a string'

    If you want to include a single quote or a double quote when you're using them to contain the string, you have to use an escape character to tell the computer to use the raw ASCII value for it

print( 'you\'re reading this')

output:  you're reading this

    If you want to include a backwards slash you also have to include a backwards slash right before it. (This is called an escape character)

    When printing something you can format it to include a variable.

    var = 23

    print(f"this is a string with {var}")

outputs: this is a string with 23

You can also perform calculations within the curly brackets

    print(f"this is a string with {var*3}")

    outputs: this is a string with 69

You can also just put calculations without variables inside the curly brackets.

    print(f"this is a string with {4%6}")

    outputs: this is a string with 4

    Sometimes from old python videos they'll use the str.format to include variables inside print functions.

    foo = 'FUBAR'

    print("this is my str.format with {}and {}".format(foo, var))

    outputs: this is my str.format with FUBAR and 23

This can also be used to specify which order the variables appear in by putting numbers into the curly brackets.

    print("this is my str.format with {1} and {0}".format(foo, var))

    outputs: this is my str.format with 23 and FUBAR

Printing type of variables

You can specify that you want to print out what the variable type is in python.
obj = hou.node('/obj')
print(type(obj))
output: <class 'hou.OpNode'>
OpNode stands for Operator node

While Loops

    Use whiles if you know you're going to have brakes, however it's risky and can cause Houdini to crash if it gets stuck in an infinite loop.
    
    This code will run exactly 5 times.

For Loops

    For loops are useful for iterating over a certain amount of times.

    This was the script I had made for tech art's HW8 assignment. The user inputted the width, height, and length, and then based on that made a cube made up of the object type based on the user's input. 

Starting out Script in Houdini

Houdini works with old school folder structure. the Obj is the top folder level, the 'root'. When you create a Geometry Node, it's a container for more folders. 

By default the houdini library does not come with python, you have to specifically import it. (If you are running your script inside houdini's tools, it will automatically import the houdini library for you. Anywhere outside of that you need to import it yourself)

import hou

    When you're making scripts, you'll want to grab the root level. 

root = hou.node('/obj')

    We assigned the path '/obj' to the variable root. 

    Next we'll create a container geometry node.

container = root.createNode('geo', 'sphereGeo')

    The variable container has a pointer to the node named 'sphereGeo'. We made sure to put in 'geo' first as that is the type of node we want to create (a geometry node to hold our geometry). Now we want to create a sphere inside the container.

mysphere = container.createNode('sphere', 'classSphere')

    This will create a primitive sphere inside of our sphereGeo node. We're using the container node here because it is going to be the parent of the sphere object. (we want the sphere inside the container node).

    You can create a sphere node and then a color node, but to connect them, you need to use the setInput function.

colorNode.setInput(0, mysphere)

colorNode.setDisplayFlag(True)

    This connects the mysphere output to the colorNode's input, as well as setting the display flag on the colorNode to see it. 

    If you want to change parameters in Houdini, you need to use the .parm and .set function to do so.

child.parm('type').set(2)

    You specify which parameter you want to manipulate in the first parenthesizes, and then the second set is what you want to set it to. This sets the primitive Type of the sphere to Polygon Mesh. It is 2 because you can think of this as an array, we want the third value in the array. 


    For color, we need to use .parmTuple because there is more than one value in the color parameter

colorNode.parmTuple("color").set((1.0, 0.0, 0.0))

    This sets the color to a bright red. Note that you also need the double parenthesizes for the set or else it will not work.

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