# Houdini, Algorithms and Python

## Recommended Posts

Haven't posted my work here in a long time, but here's a hip file in which I used Python to implement

the divide and conquer closest pair of points algorithm. Obviously not nearly as fast as a pcopen lookup,

but I wanted to learn about the divide and conquer paradigm and had a lot of fun figuring it out!

Code runs in O(n*log(n)) time, as explained in the following video:

And no, I don't script in C++, no need to tell me that it's faster

divide_conquer_find_closest.hipnc

• 5

##### Share on other sites

Very Nice Manuel!!!

Thanks for the example file!!

Cheers!

• 1

##### Share on other sites

Looks cool.
Did you also try something like that using VEX?
To find he closest matching values or positions.

##### Share on other sites

Thanks guys!

I actually didn't bother to try this in VEX since I would use a point cloud lookup for this anyway, limiting the search to only 2 points. The speed is hard to beat,

since point clouds in Houdini are highly optimized. I did this in Python because I wanted to learn the concept of divide and conquer, which you then can apply to any programming language really!

Since VEX has arrays, this would be totally doable, why don't you give it a go?

##### Share on other sites

I'll give it a try and let you know how it goes.

could be a nice challenge.

##### Share on other sites

I wanted to share my new project with the community, it's called the 'Node Color Laboratory'.

Intended to be used as a shelf tool, this tool has been written in Python and PySide.

I've seen a bunch of people using various ways to enforce color coding, however none of the approaches was using an interface,

so I decided to build one to make organizing colors most intuitive.

Here's the link to the vimeo file:

Attached are the files you're going to need to run the tool, to be placed in the \$HOME/houdini14.0/scripts folder.

You will have to create a new shelf tool and paste the script found in the 'ColorLab_v1.0.py' file in there.

This tool has been developed in Windows, I haven't had a chance yet to check if the same directory structures work in Linux/Max.

Feedback always appreciated,

cheers!

colorLab.rar

• 3

##### Share on other sites

Hi Manuel!

I'm trying to achieve this since 1 week ahah!

For now I have smothing a bit similar but not with a shelf interface (PyQt is to hard for me now).

By the way, I tested your shelf with H15 on Mac and it works perfectly!

Great job, thanks for sharing.

Cheers,

Paul

ps: I'll be in Method Vancouver soon. Hope to see you there!

##### Share on other sites

H20 You could try using QT designer to create the layout if you don't do so already.

Manuel, interesting project once again, looks a bit like a shelf tool I created a while ago, that also colours nodes but using node names. (posted in work in progress)

Was a nice project to learn PySide.

I'm interested to see what you create next.
(Also still working on the vex version of divide and conquer, as side project )

##### Share on other sites

Hi guys,

Thanks for the feedback and glad you like it.

I didn't use qtdesigner on purpose since I wanted to learn Pyside from scratch, as it helped me a lot to understand how it works.

It's awesome that the tool works on Mac, too. As for linux, I tested the tool at work yesterday and it turns out I had to change the following:

```#Add dynamic path to houdini home folder
BASE_PATH = hou.hscriptExpression("\$HOME").replace("/", "\\")
JSON_PATH = os.path.normpath("".join([BASE_PATH,"\houdini14.0",
"\scripts\color_config.json"]))

#Add dynamic path to houdini home folder
BASE_PATH = hou.hscriptExpression("\$HOME")
JSON_PATH = os.path.normpath("".join([BASE_PATH,"/houdini14.0",
"/scripts/color_config.json"]))
```

One major issue that I ran into (and I'd be interested if somebody found a fix for this) is that the drag & drop ONLY works if I have a python panel open (not to be confused with the Python shell) and if I drag the node over this panel and then onto my tool. That's quite strange and I haven't found a foolproof way to do this yet. Suggestions are most welcome!

Paul - See you soon in Vancouver!

##### Share on other sites

what would be nice to have a field where you can define what color defies the type, wheres at the moment Color_01 Color_02 ...

example:

Type / Color / Node

Camera / Color: Blue / camerasop

Light / Color: Yellow / hlight, arealight

Output / Color: Orange / outputsop , null

i haven test it but can you also use Bundles?

##### Share on other sites

Hi.

Sorry for the stupid question (I'm a newbie),  can you describe a little more how to install it?

According to the video is a very useful feature.

Thank you.

##### Share on other sites

No problem.

Go to a houdini shelf, right click and click "new tool". Give your new tool a name, i.e. colorLab. Paste the following code in the script section:

```import os

#Add dynamic path to houdini home folder
BASE_PATH = hou.hscriptExpression("\$HOME").replace("/", "\\")
PY_PATH = "".join([BASE_PATH,"\houdini14.0",
"\scripts\ColorLab_v1.0.py"])
execfile(PY_PATH)

```

If you're using Houdini 15, change the 14 to 15 in the above code.

Then go to your Documents/houdini folder and create a 'scripts' folder. Place the following files in that scripts folder:

color_config.json

ColorLab_v1.0.py

OnCreated.py

That's it

You can add an icon too if you want. (Options tab of the shelf tool)

• 1

##### Share on other sites

Here's an interesting snippet I came up with the other day.

If you want to copy/paste stuff with Python, but you're working on Linux and 'xsel' is not installed on your system,

you can abuse PySide to access the clipboard.

Following code takes the path of a selected node and copies it into the clipboard, so you can paste it with ctrl-v:

```if hou.selectedNodes():
import PySide.QtGui as qtg
app = qtg.QApplication.instance()
clipboard = app.clipboard()
string = hou.selectedNodes()[0].path()
clipboard.setText(string)
```
• 1

##### Share on other sites

Ugh, are you hinting that there is no copy/paste clipboard on Linux by default?

##### Share on other sites

Ugh, are you hinting that there is no copy/paste clipboard on Linux by default?

No Of course Linux does have copy/paste

What I wanted to do is store strings that are created by Python into the clipboard, that's not possible without using modules in Python.

Using PySide/PyQt, not only can you store strings, you can also store images as well as Mime data, which can be quite helpful!

##### Share on other sites

Here's an interesting snippet I came up with the other day.

If you want to copy/paste stuff with Python, but you're working on Linux and 'xsel' is not installed on your system,

you can abuse PySide to access the clipboard.

Following code takes the path of a selected node and copies it into the clipboard, so you can paste it with ctrl-v:

```if hou.selectedNodes():
import PySide.QtGui as qtg
app = qtg.QApplication.instance()
clipboard = app.clipboard()
string = hou.selectedNodes()[0].path()
clipboard.setText(string)
```

Thanks Manu, life is good again!

##### Share on other sites

Hey what's up community?

In order to keep this thread alive, although I'm hardly ever posting, here's my latest and greatest Python project!

Would be awesome to get some feedback on this, cheers!

• 4

## Create an account

Register a new account