Tuesday, 29 October 2024

Learning Python with Houdini Part 4

 Classes

    Classes are just a collection of data and methods to operate on that data.
    Object Oriented Programming encapsulates data (attributes_ and functions (behvaiors) into components called classes. An object is a instance of a data class. Classes hide data while exposing an interface to the outside world. Invoking classes creates a new object. 
    Python functions stuck inside a class are called methods.
    Blueprints in Unreal and Prefabs in Unity are classes. Digital Assets in Houdini are also the same. 
    Format of a class-
Class className:
    '''if you put triple quotes here it'll auto document this class'''
    def __init__(self):           #Constructor Method
        code_block
    def method (self, anArgument=Default):
        codeBlock
    def anotherMethod(self, arg1 = default2, arg2 = default3)
        codeBlock

    Self is a pointer/memory address that points to the class. The init function will fire off first when the class is first run. The first argument inside of every method is self.
    Tip: the triple quotes there will show up when you're writing it in the python shell, it will pop up when you're writing the function.
    To create an instance of a class, just assign the class/ constructor to a variable
myInstance = myCustomClass()
    You should be putting default values in all of you definitions, that way you can call the class without any arguments.
    When defining the constructor (the init function), it's good to define all the variables that you're going to use for the class.
    Try starting out with a function that prints out a statement to test if it works.
Note: If you forget to put the self in the function parameter inside of a class, it's not going to be able to find it. Without .self, it creates a local variable, but none of the functions in the rest of the class will be able to access it. It needs the self. to point to the place in memory the class resides, to make the variable an attribute of the class to be accessed later. 



    We put :02 right after the variable to format it so it is padded to 2 digits (will write out 01 instead of just 1).
    Now test the class by calling it.
    What if you wanted to set variables inside the function when you create an instance of the class, you can put in arguments in the init function
    Still good practice to give default values to the arguments. We can go further than this by creating a function that sets the values of our variables that we can call to change them on the fly.

    So we can use this function within the class, but we can also call upon that function outside of that function. 

    Public Vs. Private

    All attributes are available to everyone. There is a way of making something kind "Private"
self.__onlyMe = "Foobar eh, Feeterbeeb?"
    Naming an attribute with a "__" (double underscore) prefex scrambles the name. This is called a Dunder (derived from monty python jokes, just like how this entirely language is named after). Used to create private/exclusive classes)
    
    If you run this function normally it works fine and prints and manipulates the variables in the setTime function fine. But the trouble only comes when you try to call that specific variable. 


    This is going to throw an error and say there's no attribute named that. if you really want to access a 'private variable', you have to write it like this:
    
What if you wanted a variable that all instances of a class shares, but none of them control? Well you can do that by defining it outside of the init function. 


    This is only in the class, not in the instance, this is useful for creating multiple instances that share one variable and all of them have access to it, they just wouldn't be in charge of it. So by running this:
    You get this:


    Count printed 2 because you've created 2 instances of the class and it incremented every time you created it. You could have asked to print tiktok.count and it would have the same value.

Downloading and using Python libraries
    Find the houdini folder in your documents folder. Mine was named houdini20.5. Jump inside of it, and if there is not alread a folder named python3.11libs, create one. Then download the py file that you want. We tested it with Time.py file given to us. Once you've done that restart Houdini, and open up the Python Shell. You'll notice you can import the file without errors and use all the functions from that library.


    

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