Jump to content

Make Safe Node Name?


Atom

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)

 

Link to comment
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

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...