Atom Posted September 21, 2016 Share Posted September 21, 2016 Hi All, I am passing a file name to createNode() and I am getting problems when the filename has spaces in it. I assume there are other invalid characters that might appear in a filename that are not allowed in a node name. Is there any built in python function that will convert a string into a valid node name? I have a basic function to trap for spaces but I don't know what all the illegal characters are and I still get an occasional failure? def returnValidHoudiniNodeName(passedItem): result = passedItem.replace(" ","_") return result safe_node_name = returnValidHoudiniNodeName(os_based_filename) new_node = some_geo_node.createNode("grid",safe_node_name) Quote Link to comment Share on other sites More sharing options...
fsimerey Posted September 21, 2016 Share Posted September 21, 2016 You can import regex library (import re) or if your string depends of encoding i use this: CHARS_FORBIDDEN = ['-', ' ', '!', '?', '.'] def cleanName(string): s = unicode(string) name = unicodedata.normalize('NFD', s).encode('ascii', 'ignore') name = name.translate(None, '_'.join(CHARS_FORBIDDEN)) return name Quote Link to comment Share on other sites More sharing options...
graham Posted September 21, 2016 Share Posted September 21, 2016 I would basically just do something with a regex cleanup function that converts anything you can't enter manually into _ like Houdini does. def cleanName(name): return re.sub("[^0-9a-zA-Z\.]+", "_", name) 1 Quote Link to comment Share on other sites More sharing options...
Atom Posted September 21, 2016 Author Share Posted September 21, 2016 That works great, thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.