Jump to content

Make Safe Node Name?

Recommended Posts

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)


Share this post

Link to post
Share on other sites

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


Share this post

Link to post
Share on other sites

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)


  • Like 1

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