Python SOP: How to access file relative to $HIP?

   1444   4   3
User Avatar
Member
132 posts
Joined: Aug. 2014
Offline
I'm using numpy to read a CSV file and assign the values to variables like this:

wavelengths, intensities = np.loadtxt(filename, delimiter=',', unpack=True)
Which works if filename contains the full path to the file.

However, I'd like to be able to define the file path relative to $HIP

How would you do this in a Python SOP, ideally using a file parameter?

Thanks
Edited by jlapre - April 21, 2023 20:25:03
Jeroen
User Avatar
Member
132 posts
Joined: Aug. 2014
Offline
I found the answer here:

http://www.ytini.com/tutorials/tutorial_pythonSOP.html [www.ytini.com]
dfile = node.parm("datafile").eval() # data file to read in - Parameter type: File
i.e. add a parameter to the Python SOP called "datafile" of type File.
You can use the file browser to define the path to a file, relative to $HIP
e.g.
$HIP/../Hydrogen_Wavelength_Intensty_NoHeader.csv
Then
node.parm("datafile").eval()
will return the full path.
Edited by jlapre - April 21, 2023 20:39:22
Jeroen
User Avatar
Member
67 posts
Joined: Oct. 2018
Offline
Hi!

What about:
hou.hipFile.path()
gives you your current File.
os.getenv("HIPFILE")
os.getenv("HIP") are also possible.
User Avatar
Member
159 posts
Joined: July 2010
Offline
GOgraphR
Hi!

What about:
hou.hipFile.path()
gives you your current File.
os.getenv("HIPFILE")
os.getenv("HIP") are also possible.

Thanks, GOgraphR, it's good to know there are a number of ways to do this.
As I mentioned, I went with a File parameter of type File added to the Python SOP.
It works well in my context.
Thanks

Attachments:
Screenshot 2023-04-24 at 4.22.49 PM.png (200.1 KB)

User Avatar
Member
67 posts
Joined: Oct. 2018
Offline
However, I'd like to be able to define the file path relative to $HIP
This is what triggered me...

I did it your way on some other project.

My approach: If I cannot do something in VEX, or in Python, but can do it in the other context I use detail attributes to transfer the information, or I also like to _abuse_ parameter fields...

At least the job has do be tone...
  • Quick Links