Jump to content
Sign in to follow this  
konstantin magnus

Batch opening and saving *.obj files

Recommended Posts

How do I batch open randomly named OBJ-files from a folder on my harddisk

and how can I batch save those in a different folder while keeping their file names?

houd_sshot_io.jpeg.ee6a94c1d0aef830c8531dc0767aa6e2.jpeg

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites
Posted (edited)

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

Share this post


Link to post
Share on other sites
Posted (edited)

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 2

Share this post


Link to post
Share on other sites
geo = hou.pwd().geometry()

for f in random_file_list:
  geo.loadFromFile(f)
  

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×