Building cities with a click of the mouse with Houdini and Python



In the development of games, pipelines, algorithms and small processes are constantly created and applied, which save a lot of time and effort. Often these solutions are simple but elegant.

This article is about this. The author came up with a method of building virtual cities, which in the future he wants to turn into a full-fledged tool of a level designer.

In this article I will go over the basic settings of the City Builder tool in Houdini, which can build cities with simple mouse clicks. Note that it only works inside Houdini using the Python Viewer States.

Reference


The biggest inspiration for me was Oscar Stolberg . His work was impressive, and made me wonder if it is possible to repeat this in Houdini. My attempt can also be considered as proof that it is quite realistic to reproduce any idea in another software.



Initial setup


The first thing to do is the grid or plane on which the houses will be built. Theoretically, it could be anything, for example, triangles, hexagons, and so on. The easiest way I've found is to use quad mesher.

Start with the circle node and place an instant mesh node under it (it's from the Labs tools, so make sure you have them installed).



After that place the extrude node - later it will be used to extrude the primitives selected by the mouse.



The setup will be minimized in HDA, in it you will create a parameter for working with Python code. In this case, you need to control the group parameter to extend and fill the primitives that the mouse clicks on.



Python Viewer States


Now you can go to the Python code. If you are new to viewer states, then I previously wrote a basic intro to it.

Create a new viewer state and start with an empty template. This will only generate the piece of code that is really needed.



There are a few things to add. You can divide them into 2 parts:

  • Getting geometry to view with mouse clicks.
  • Getting primitive with a mouse click.

To get started, in the init part, add a new variable for the geometry called self.geometry = None. Then add the OnEnter function - this method is called when the state is activated by the user creating / selecting the node and pressing Enter in the viewport.

This is where the current geometry is stored in self.geometry itself. Done using node.geometry ().



Now there is a connection with geometry.

The next part is getting a primitive number based on the position of the mouse on click. To do this, create the OnMouseEvent function - this is the processing of mouse events (such as clicks or mouse position).

At the beginning of the mouse event, create variables to work. Then get the intersection with the geometry using the GeometryIntersector function and use self.geometry.

From this intersection you will get the information you need, find out and print a primitive number.



If everything is done correctly, it will turn out this:



The last setting is to use a primitive number in the group parameter, which you did at the beginning. Replace the string with a new one, call the group parameter and set the primitive number as the value. Note that it is also converted to a string, because group is a string parameter.



It will turn out this:



This is the main idea with which I began to create a tool. From now on, you will store information, not rewrite it. This setup will provide a basis for starting and further experiments.

Detail


Once you have these cubes, you can do anything with them. If you need a city, then create a house generator - there are many tutorials on this topic on the Internet. In the meantime, my example.

A modular wall is copied on each side of the cube.



A border that is beveled and unfolded with UV.



Performance is very important here. In order for the tool to work quickly and when you press the house instantly appears, you will have to do optimization. To do this, you can try to use the minimum number of loop nodes and boolean nodes on the network.

Instrument future


I want to go further and make it a tool for creating levels. The idea is that you can quickly prototype finished levels with a couple of clicks. I was inspired by the Hob .



Finally, a small demo of what I got at the moment:


All Articles