Basic Python for copy/pasting files in windows

   2934   4   0
User Avatar
Member
1177 posts
Joined: April 2017
Offline
Hi!

I've got a few SpeeTree assets that I modifed with my HDA. The last thing I can't do is copy/paste all the textures into a specific folder hierarchy and rename those copied files. I can already get the path to each texture files in a Detail Wrangle so I would need python to get those string attributes to know where to get the textures.

The best would be to have a button on my HDA that would run a python script to copy/paste the textures.

I'm just starting to dip my toes in python and I'd like to get tips about how to do this because I'm currently clueless! Also, if there are greate tutorials for this sort of thing, that would be great!

-Olivier
User Avatar
Member
247 posts
Joined: May 2017
Offline
Hi,
to run a script through your hda, you can use the python module - it's quite easy to set up.

First go to the type properties of your hda, under parameters create a button and a file dictionary from which we will read the textures.

Then go to the Scripts tab, at the bottom left you will find the drop down menu for Event Handler, select the Python module, it is now selectable above.

Once you have selected the module you can start coding. For example:
def foo():
    import os
    # Read the texture folder path from node parameters (pwd is shortcut for hou.node(".") (current node))
    folder_path = hou.pwd().parm("txt_fld").eval()
    # Insert all texture paths contained in the folder into a list
    file_list = [os.path.join(folder_path, file) for file in os.listdir(folder_path)]

    print(file_list)   
this will return the paths of the contents specified by the parm file dictionary.

The last thing you need to do is to bind the foo() function to the button. Go back to the parameters tab, select your button, and under Callback Script, paste this snippet:
hou.phm().foo()

hou.phm() [www.sidefx.com]

Save, specify the texture folder and press the button. The files that are in the folder will be printed out.

The hip file is below.
Image Not Found
Edited by vikus - July 26, 2023 06:38:04

Attachments:
01.png (31.5 KB)
02.png (13.1 KB)
03.png (33.2 KB)
Load_Texture_Paths.hiplc (104.4 KB)

User Avatar
Member
1177 posts
Joined: April 2017
Offline
Wow, thanks vik_lc!

I'll try to understand the whole thing. I really apreciate the time you took for me!

-Olivier
User Avatar
Member
1177 posts
Joined: April 2017
Offline
I'm running into some problems. I'm trying to access the string array "listAlbedo" on the output of the HDA but I get an error. What am i doing wrong?
Edited by olivierth - July 26, 2023 15:52:19

Attachments:
Houdini_python_error_01.JPG (66.6 KB)
Houdini_python_error_02.JPG (98.7 KB)

User Avatar
Member
313 posts
Joined: Oct. 2016
Offline
Hi!

Python is good for learning because it usually provide great error reports.

"local variable 'listAlbedo' referenced before assignment.

Your code is not using a defined variable. The method you are calling expect a string as an input.

To get rid of that error message you either define listAlbedo e.g. on a line above listAlbedo = 'listAlbedo' or you simply add quotes around your call. Like this: listAlbedo = hou.pwd().parm('listAlbedo').eval().

Cheers!
Interested in character concepts, modeling, rigging, and animation. Related tool dev with Py and VEX.
  • Quick Links