Maya Type Script

Maya Type Script

MattTidridge
Participant Participant
3,856 Views
5 Replies
Message 1 of 6

Maya Type Script

MattTidridge
Participant
Participant

I'm working on a project where I need to render 2000+ numbers along a path. I'm using MASH to populate the models along the path and that's working really great! The issue I'm having is trying to write a script that will generate the 2000+ number assets (in either MEL or Python). 

 

In MEL I'm able to create the typeMesh objects, and then I believe I can use setAttr to change thing like font and size. The problem is I'm having a hard time finding the actual text-entry attribute so I can tell the typeMesh what number to be. Any help or insight is appreciated! 

 

 

0 Likes
Accepted solutions (1)
3,857 Views
5 Replies
Replies (5)
Message 2 of 6

RFlannery1
Collaborator
Collaborator
Accepted solution

The "type" node has an attribute called "textInput".  You can use this to query and set the value of the text.  The weird part is that it doesn't work directly with your string.  For example, if the text is "test", then querying the attribute gets the following result:

cmds.getAttr('type1.textInput')
# Result: u'74 65 73 74' # 

Similarly, you can set the attribute.

# Set the text to "test3"
cmds.setAttr('type1.textInput', u'74 65 73 74 33', type='string')

So it looks like you have to convert your string into the format that attribute wants.  Here is a script I came up with.  No idea if it this is the most efficient way, but it works:

def setTextVal(typeNode, textString):
    hexValues = []
    for character in textString:
        hexValues.append(character.encode('hex'))
    attrVal = ' '.join(hexValues)
    cmds.setAttr(typeNode+'.textInput', attrVal, type='string') setTextVal('type1', 'hello there 2005')
Message 3 of 6

MattTidridge
Participant
Participant

That's great. Thanks so much for the code rflannery! I finally found that attribute in the node editor and generated the Hx codes with a loop and some if statements. I'll have to try your method in the future for more complicated strings. How do you typically find these attributes? It seems strange to me that they don't show up in the channel editor but do show up in the node editor. I feel like Maya has all these different editors with just slightly different overlap! It doesn't help that there doesn't seem to be much documentation on specific MEL commands. 

Message 4 of 6

RFlannery1
Collaborator
Collaborator

Normally I look at the Maya documentation for different node types.  However in this case, I didn't see the "type" node in the documentation.  So I did what you did and looked in the Node Editor.

Message 5 of 6

wynandlens
Enthusiast
Enthusiast

Thanks So Much! This worked great for Maya 2020. Unfortunately this doesn't work for Maya 2022 and Python 3.

Do you have a solution for that? 
I'm trying binascii, but that shoots out errors.

 

0 Likes
Message 6 of 6

wynandlens
Enthusiast
Enthusiast

I have a solution for Maya 2022 (python 3)

 

def text_to_spaced_hex(txt = ''):
    '''This converts text to hexidecimal values with spaces'''      
    # Create the return variable
    hex_with_spaces=''   
    # Convert txt to hex
    for t in txt:
        # Convert each letter in txt to hex
        hx = t.encode("utf-8").hex()
        # Add a space between each hex value
        if hex_with_spaces:
                hex_with_spaces = hex_with_spaces + ' ' + hx
        if not hex_with_spaces:
                hex_with_spaces = hex_with_spaces + hx 
    return hex_with_spaces

hex_with_spaces = text_to_spaced_hex('Type This Out')

type_node = 'type1'
cmds.setAttr( type_node  + '.textInput', hex_with_spaces, type="string")

 

3)

0 Likes