Creating "3D Type" in Maya using a python command/script?

Creating "3D Type" in Maya using a python command/script?

Anonymous
Not applicable
9,282 Views
7 Replies
Message 1 of 8

Creating "3D Type" in Maya using a python command/script?

Anonymous
Not applicable

I'm looking for the command to create text in python with the new type tool, I have tried echoing all commands but I get a huge list of output, the script editor just shows "sets -e -forceElement typeBlinn10SG;
// Result: typeBlinn10SG //" which as far as I can tell if converted to python does not create text.

0 Likes
9,283 Views
7 Replies
Replies (7)
Message 2 of 8

Anonymous
Not applicable

Hi connormccann6

 

Looking at the Type tool menu item it is calling this MEL script:

 

typeCreateText

 

Which can be found here:

 

./Program Files/Autodesk/Maya<version>/scripts/others/typeCreateText.mel

 

Looking in that script it looks like most of the work is actually being done in Python anyway, so you could just pull the script apart and get the bits you need from it. I guess it depends on what you are trying to do.

 

The easiest thing to do would be just to call that script from python like this:

 

import maya.mel
maya.mel.eval("typeCreateText")

However if you are going to take the script apart it looks like you need to initialise the tool before starting it. Take a look in this file to see the initialisation:

 

./Program Files/Autodesk/Maya<version>/scripts/others/typeInitPlugin.mel

 

Hope that helps

 

Cheers

 

Mike

Message 3 of 8

Anonymous
Not applicable

Hello, I was also wondering how to go about setting dynamic text and this thread helped me get started. I'm writing a program that takes in data from a file and displays it as a graph in maya and I'm using the text to display the numbers for the hash marks along the side and bottom of the graph. It's convoluted but I was able to write procedures to convert the numbers to hex and then use them to alter the text objects. However, there are two questions I have:

 

1) When creating these text objects they create multiple errors and I'm assuming it has something to do with python. I'm not entirely sure how the font object creation works but I use the mel command CreatePolygonType; to create the type and then a python command to edit the type of each type object. However, the process seems to be CreatePolygonType;>Creates Text Object>Errors occur while the program selects each text object again. I'm not sure what maya is doing when it goes over each text object but it seems the timing of this can conflict with my setting the text values and if maya is not freshly restarted it can interfere and cause text objects to be missing in the final graph. To give an idea here is some example code to spawn 10 text objects and set their numbers. Run the code once and it should spawn 10 text objects with different numbers. Delete all objects and run the script again and look at each object and there ought to be some text missing from one of them.

 

 

{
	python "import maya.cmds as cmds";
	int $num = 30;
	for($x=0;$x<10;$x++){
		CreatePolygonType;
		$txtCmd = "python \"cmds.setAttr('type"+($x+1)+".textInput', u'"+$num+" "+$num+"', type='string')\"";
		eval $txtCmd;
		$num++;
	}

}

I'm not sure if having the program wait would help but the command 

 

pause -sec 10;

Only seems to pause maya entirely and the text errors happen and so does the missing text objects. The good news is the text doesn't seem to mess up when maya has been restarted, only after running the script multiple times. 

 rgZjchR

Here you can see the number 120 is missing after running my chart code twice, the selection of 600 is from it going over each text object after they were created. I think that I'm changing the contents of the text objects before maya has finished doing its text creation process and so having the program wait until it is done, then change things like font size and text would work better. If anyone knows a way to do this or understands more about how the type tool works please let me know.

 

2) My other question is how to set the alignment of text. I set the font size using

setAttr "type1.fontSize" 10;

Except in my program I use an expression and it gets the font size from a locator control's attributes and allows for dynamic font changing via code.

string $fontCmd = "expression -s \"type"+($i+1)+".fontSize = locator_C.FontSize;\"  -o typeMesh1 -n \"fontsize"+($i+1)+"\" -ae 1 -uc all ;";
eval $fontCmd;

I'd like to do the same to make each number right aligned but unfortunately unlike changing the font the alignment gives the same command every time in the script editor window:

import maya.app.type.AEtypeTemplate; maya.app.type.AEtypeTemplate.textAlignmentChange("type1")

It's python and it seems to make the text object center aligned but there's nothing I can change in the command except the name of the type object it is affecting, so unlike changing the font size I can't seem to specify left, center, or right align because its not an attribute of type. If anyone knows how to affect this process please let me know.

 

Thank you for taking the time to read my reply, again my questions are 1) How to set and edit text objects in a stable and predictable way so they don't get corrupted, and what that python error might be and 2) how to set text object alignment with code.

 

Thanks!

 

 

 

0 Likes
Message 4 of 8

Anonymous
Not applicable

Also in addition here is the error I get when each type object is created

# Error: line 2: RuntimeError: file /Applications/Autodesk/maya2017/Maya.app/Contents/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages/maya/app/type/AEtypeTemplate.py line 1114: Failed to disconnect signal currentIndexChanged(QString). #

It mentions something about disconnect and I noticed that creating text objects manually using the maya buttons and then typing stuff into the attribute editor will not change the default "3d type" text. In fact, the type tool "breaks" until you restart maya. 

 

0 Likes
Message 5 of 8

Anonymous
Not applicable

Hi Global

 

I am a bit short on time but as a work around for your issue you could modify the way your script works so that instead of creating a new type object on each iteration of the loop, just create one at the start, and modify its text and duplicate it in the loop.

 

I tested out your example and I can see what you mean about the timing. I think this has something to do with the python command being run in an evalDeferred call, which means MEL will run it after everything else has finished. I am sure you can come up with a proper solution to this, but for now you can just run your script in two parts like this:

 

Execute this bit first

 

// Run this bit first to create a type object
typeCreateText();

Then with the new object selected, run this bit

 

// Run this bit after the text in in the scene...
string $typeObject[] = `ls -sl`;
string $type3d[] = `listConnections -type "type"`;

if (size($type3d) == 0)
    error "No type created....";

int $num = 30;
for($x=0; $x<10; $x++)
{
    // Set the text
    setAttr -type "string" ($type3d[0] + ".textInput") ($num + " " + $num);

    // Duplicate it
    duplicate $typeObject[0];

    $num++;
}

Hope it helps

 

Cheers

 

Mike

Message 6 of 8

Anonymous
Not applicable

TechToast,

 

Thanks for the quick reply! That makes sense to create it once and then duplicate it for the other instances. I'll check out your code and see if it helps!

0 Likes
Message 7 of 8

Anonymous
Not applicable

To anyone who comes across this thread, I found a solution in another forum! spire2 posted solutions to my two biggest questions, that is how to set the font and alignment of a text object. The code is in python:

 

import maya.cmds as cmds
import maya.app.type.AEtypeTemplate;

# Set the font to Arial
cmds.setAttr('type1.currentFont', 'Arial', type='string')

# Set the text align to center
cmds.iconTextRadioButton("textAlignCentreAEReplacement", edit=True, sl=True)
maya.app.type.AEtypeTemplate.textAlignmentChange("type1")

# The other buttons are "textAlignLeftAEReplacement" and "textAlignRightAEReplacement"

I tested it myself and now I can finally set a font and alignment for text objects! Wanted to repost it here in case someone comes searching for the same thing.

 

TechToast, thanks again for your reply. I tried out your example and it does run a lot smoother than my method, but unfortunately has the same timing issue where you can't run the code all at once because maya needs time to instance the text before working on it. I'm looking into threading as a possible solution, will post if I figure out a good solution.

 

 

0 Likes
Message 8 of 8

Anonymous
Not applicable

To whom it may concern in the future: 

 

Setting the alignment mode can also be done like this:

 

mc.setAttr('type.alignmentMode', 2) # (centered)

this is part of my full code for auto-creating 3d type:

import maya.mel as mel
import maya.cmds as mc

def textToSpacedHex(text = 'test'): out = [] for c in text: hx = c.encode('hex') out.append(hx) return ' '.join(out) hx = textToSpacedHex(text) mel.eval('CreatePolygonType') t3d_trans = mc.ls(sl = True)[0] t3d_node = mc.listConnections('%s.message'%t3d_trans)[0] t3d_extrude = mc.listConnections('%s.outputMesh'%t3d_node)[0] mc.setAttr('%s.textInput'%t3d_node,hx, type = 'string')

It is beyond me why this isn't more accessible, since one of Maya's USPs is the scriptability...