VEX String: keep last section until "/"

   12767   6   0
User Avatar
Member
1195 posts
Joined: April 2017
Offline
Hi!

I've got a long string path and I would like to only keep the last part. For exemple:

/mainfolder/assetFolder/ProjectName_vegetation_big_Tree_eucalyptus_a.abc

I would like to end up with

ProjectName_vegetation_big_Tree_eucalyptus_a.abc

...or at least, have a way to remove the last 30 digits instead of looking for a "/".

-Olivier
User Avatar
Member
729 posts
Joined: Sept. 2013
Offline
Not tested, but in VEX the splitpath function seems to do this.
https://www.sidefx.com/docs/houdini/vex/functions/splitpath.html [www.sidefx.com]

Otherwise possibly just split:
https://www.sidefx.com/docs/houdini/vex/functions/split.html [www.sidefx.com]
https://procegen.konstantinmagnus.de/ [procegen.konstantinmagnus.de]
User Avatar
Member
21 posts
Joined: Jan. 2018
Offline
Python can do that .
# path test in detail wrangle : 
# s@path = "/mainfolder/assetFolder/ProjectName_vegetation_big_Tree_eucalyptus_a.abc";
# s@abc = ""; // Create an empty string to modify with setAttrib in python sop

node = hou.pwd()
geo = node.geometry()
path = geo.attribValue("path")[::-1]
path_final = ""
for element in range(0,len(path)):
if ( (path[element]) != "/" ):

path_final += path[element]

elif ( (path[element]) == "/" ):

break



print path_final[::-1]
geo.setGlobalAttribValue("abc",path_final[::-1])
# String return : ProjectName_vegetation_big_Tree_eucalyptus_a.abc
Edited by Alexander_Nguyen - May 27, 2021 01:54:34
User Avatar
Member
32 posts
Joined: Aug. 2011
Offline
So as Konstantin pointed out, both of those functions work. You can check them out below:

s@my_string = "/mainfolder/assetFolder/ProjectName_vegetation_big_Tree_eucalyptus_a.abc";

// Grab the value after the last split
s@last_split = split(s@my_string, "/")[-1];

// Grab the last 48 digits
s@last_48 = s@my_string[-48:];

// Define attributes, and then use those in the function to grab those values
string dir, name;
splitpath(s@my_string, dir, name);

// Set the attributes on the points from the values
// You could also define these in the function directly if you'd like
s@dir = dir;
s@name = name;
User Avatar
Member
1195 posts
Joined: April 2017
Offline
Thanks for all the replies. I know nothing about python so I'd just stay with vex.

Thanks gnisbet, those examples look good. I'll play around with each to better understand.

-Olivier
User Avatar
Member
1195 posts
Joined: April 2017
Offline
hey gnisbet!

I just tried the
split(s@my_string, "/")[-1]
and it works great. But I don't fully understand it. What does the
[-1]
do exactly? I tried without it and I get an error yet, in the documentation, it doesn't say I should add anything outside the ()

-Olivier
Edited by olivierth - May 27, 2021 09:27:44
User Avatar
Member
729 posts
Joined: Sept. 2013
Offline
[-1]

extracts the last entry of an array:

https://www.sidefx.com/docs/houdini/vex/arrays.html#slicing-arrays [www.sidefx.com]
https://procegen.konstantinmagnus.de/ [procegen.konstantinmagnus.de]
  • Quick Links