| Final Effect |
This assignment is essentially an introduction to Python. Although I used Pygame to make a game three years ago, I'd forgotten most of the language (just didn't use it enough). After reviewing Python’s data types and basic syntax, I started this project.
Before writing any code, even though the assignment brief already provided quite a lot of APIs, I wanted to learn more—so the official documentation was my best source; it explains what I needed in detail:
https://www.sidefx.com/docs/houdini/hom/hou/ui.html?utm_source
For development, I felt Houdini's built-in editor was hard to use, so following tutorials and the official docs I configured PyCharm to work with Houdini (the setup is fairly involved, so I' ll skip the details here).
After the environment was set up, the next step was to clarify the program flow (this is very helpful for development).
Once I began actual development, I ran into a problem: input() isn't well supported in Houdini and can easily block execution, potentially crashing Houdini. To fix this, I looked up the relevant docs—the correct approach is to use Houdini's own UI input to avoid crashes: hou.ui.readInput(). This function pops up a dialog in Houdini so we can enter values there.
After solving the input issue, here's a quick recap of the requirements:
-
Write a Houdini Python script that does the following:
-
Let the user choose one of four shapes (e.g., tube, torus, sphere, box).
-
Let the user input the scale of each object, from 0.1 to 10.
-
Let the user input how many objects to place along the cube array's length, width, and height.
-
Generate a cube array based on the chosen shape and scale using those counts.
-
Assign a unique color to each object.
-
The core logic wasn't difficult, so I finished it quickly; to push it further, I made a series of improvements:
-
Used
hou.ui.readMultiInput()andhou.ui.selectFromList()to implement a single-shape picker and one-shot input of the cube array’s length/width/height. -
Wrapped logic into functions to improve readability.
-
Added safety checks to ensure all inputs are valid.
-
Ensured the generated array’s geometric center is at (0, 0, 0).
During generation I found another issue: when creating via nodes vs. via code, the default radii for tube, torus, and sphere were inconsistent (Houdini 19.5). I therefore adjusted the defaults (using a 1×1×1 box as the baseline):
if name == "tube":
shape.parmTuple("rad").set((0.5, 0.5))
elif name == "torus":
shape.parmTuple("rad").set((0.35, 0.15))
elif name == "sphere":
shape.parmTuple("rad").set((0.5, 0.5, 0.5))
With that, the implementation was complete.
You can find the source code in my perforce!
没有评论:
发表评论