Jump to content

Batch opening and saving *.obj files


konstantin magnus

Recommended Posts

The only way I have been able to solve that is to use a Python shelf tool to read all the OBJ files in a folder and create a subnet of objects that points to those files. The script creates an index based name so you can batch export them in the /out context (using a Geometry node), however it does not preserve the original name. The main goal of the script is to take a random series of file .objs and create a new set that could be indexed for instancing. However, you could store the original name in the comment field of the object then in the post write of the Geometry output driver rename the newly exported indexed file to back to the original name.

Untitled-1.thumb.jpg.6def2a48f160c36673e29efe1410b2c2.jpg

The script can be found here if you want to check it out.

 

The script also creates companion nodes after the File node which would fall into the category of your do_something criteria. The script also shows how to drop down a wrangle and python node and install inline code processing if you need that.

Untitled-1.jpg.645326053bf1d58de7837f5de5d4c021.jpg

Edited by Atom
Link to comment
Share on other sites

Thank you Atom, but I wouldnt want to bulge-import dozens of models ; )

I was rather thinking of a few lines such as:

  • List all OBJs from a directory
  • Open one of them at a time
  • Process in a subnetwork
  • Export again as OBJ

I would not mind doing this on a per frame-basis, albeit not ideal. Couldnt I pythonize just the file name slots in the Read and Write nodes?

Edited by konstantin magnus
Link to comment
Share on other sites

Ok, what about something like this...

Place this code in the Pre-Frame Script field of the rop_geometry1 node. Remember to switch code processing from the default of Hscript to Python.

It basically scans the input folder on every frame change and uses the frame number as an index into the retrieved list. It re-writes the file1 input path and the rop_geometry1 output path to match the current file being processed. The results are stored in a folder named results, so you will have to manually create that.

Quite a bit of hard coding, but it does seem to work. Just don't rename nodes and populate the input and output paths to match your folders.

 

USAGE: Click Render on the rop_geometry1 node.

# Process a series of .OBJ files.

import os

input_path = r"F:\Keep\Models\Rocks"
output_path = r"F:\Keep\Models\Rocks\result"

def returnFilesLike(passedFolderName, passedFileExtension = ".obj"):
    result = []
    for file in os.listdir(passedFolderName):
        if file.endswith(passedFileExtension):
            result.append(os.path.join(passedFolderName,file))
    return result

lst_files = returnFilesLike(input_path, ".obj")
l = len(lst_files)
if (l>0):
    frame = hou.intFrame()
    if frame <= l:
        # Frame is within range of object count in this folder.
        
        file_source = os.path.split(lst_files[frame])[1]
        file_node = hou.node("/obj/geo1/file1")
        file_node.parm("file").set(lst_files[frame])
        
        file_dest = os.path.join(output_path,file_source)
        rop_node = hou.node("/obj/geo1/rop_geometry1")
        rop_node.parm("sopoutput").set(file_dest)

Untitled-1.jpg.5e743c1dab3868127f426baa52ae0de3.jpg

ap_re_process_folder_of_OBJs.hiplc

Edited by Atom
  • Like 5
Link to comment
Share on other sites

  • 3 weeks later...
  • 8 months later...

Thanks for the hint, Alex! Seems like you can do batch jobs completely within a python node : )

This one for example scales all OBJ files from a folder and its subfolders and saves them to another location:

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)

 

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...