Batch processing files with Python

calendar_today

29.03.2022

label

Python, Automation

mouse

Houdini 17.0

1 Introduction

Although there are more elaborate ways to process files in TOPs by now, here is an example on how to open, scale and save a bunch of OBJ-files from a folder right in SOPs.

2 Python SOP

Create a python node inside a geometry container with three parameters: a float named scale and two strings: dirimport and dir_export.

3 Code

import os

geo = hou.pwd().geometry()
scale = hou.evalParm('scale')
dir_imp = hou.evalParm('dir_import')
dir_exp = hou.evalParm('dir_export')

for root, dirs, files in os.walk(dir_imp):
    for file in files:
        if file.endswith('.obj'):
            path_import = os.path.join(root, file)
            path_export = dir_exp + file
            geo.loadFromFile(path_import)
            for point in geo.points():
                pos = point.position() * scale
                point.setPosition(pos)
            geo.saveToFile(path_export)

import os loads a module to work on the underlying operating system, in our case reading from and writing to files on our hard disk.

hou.pwd().geometry() makes the geometry of our current node accessible. hou.evalParm evaluates the parameters values.

The nested for loops iterate through folders and files using os.walk on the directory its given to.if file.endswith is a condition that filters files by their ending such as *obj.

After defining import and export paths, we read the mesh files from the import folder using geo.loadFromFile and iterate through the points to scale our geometry.

Lastly geo.saveToFile writes our scaled geometry back to the export path.

comments

comments

Leave a comment

Available formatting commands

Use Markdown commands or their HTML equivalents to add simple formatting to your comment:

Text markup
*italic*, **bold**, ~~strikethrough~~, `code` and <mark>marked text</mark>.
Lists
- Unordered item 1
- Unordered list item 2
1. Ordered list item 1
2. Ordered list item 2
Quotations
> Quoted text
Code blocks
```
// A simple code block
```
```php
// Some PHP code
phpinfo();
```
Links
[Link text](https://example.com)
Full URLs are automatically converted into links.