Practicing with Classes again
When creating a class that has no parent class, you don't necessarily need the () after the class name, but it doesn't hurt it either.
As always the first thing to do when creating a class is to create the constructor or __init__ function.
Always good to start with having a name variable initialized, Next we can create a basic createGeo function.
Next we want to create an anchor node, or the last node at the end of our chain that we want to put our Display Flag on. We'll include it in our __init__ function. We know any instance of our class or child classes are going to use.
We can create an output Node next, which is a node in houdini specifically for outputting whatever is at the end of your chain.
This is just an example of a modular function to be used several times.
Say we want to set the color of the node to green? we can use a setColor node, pass through a hou.Color instance to change it to green (cause green means go).
Say we want to set the color of the node to green? we can use a setColor node, pass through a hou.Color instance to change it to green (cause green means go).
What it looks like so far |
Before we move onto creating child classes, we need one more final method that we now that all child classes will use, and that is one that connects nodes to the anchor node. So in future child classes we can call this function to hook it up to the anchor node.
We are not calling this function yet in the parent class, it will be accessed later in a child class |
Creating child classes
We're making a child class named Crag that uses the Geo class as a parent. Start off with the name of the class with the parent class in parenthesises
Next we create the constructor method. We can use the functions from the parent function by using the super function.
We can add onto the constructor class after calling the parent's __init__ method. Now with the createCrag method, we can call the container node to create Crag, and then the hookup method we made to connect it to the anchor nodes.
since the hookUp function already calls the layout function, it will already be laid out nicely when you check |
Saving the Module
So we can take this script we made in Houdini and save it as a .py file inside of our houdini20.5>python3.11libs folder. (this was in our documents folder, might be different for someone else).
Now I'm saving it with notepad, but it is recommended to use either Sublime Text or Visual Studio.
Because we were writing this script in Houdini's script editor, it automatically called the hou module for us. But if we are saving this in an external editor, we have to call the hou module ourselves.Our GeoModule |
For our CragModule, we also need to import our GeoModule. If we don't want to use the GeoModule namespace whenever we call it, we can instead call:
from GeoModule import Geo
We imported the hou module again just in case since the GeoModule already imported it |
remember * is to say we don't want to use namespaces |
If you've done everything correctly this should create a Crag object. You can even use this inside of the Houdini script editor.
So what if we wanted to translate our objects around. Well first we want to create a setattr method to make sure our users won't be making new random variables.
No comments:
Post a Comment