Hi there, I'm trying to use setAttr, but insert my own variable name from earlier in my script. I keep getting a syntax error, can anyone see what I'm doing wrong here. I'm trying to apply a planar map to a polygon object and then set the projection height to 400, but I can't figure out why my variable name is causing a syntax error. I must be missing some formatting.
</code>myFaces = cmds.polyListComponentConversion(s, toFace=1)[0]</pre>This command returns the list of components as array, added [0] to only grab the first value returned. <br></div><div><br></div><div>Then we select those faces with cmds.select</div><div><br></div><div><pre class="CodeBlock"><code>myProjection = cmds.polyProjection(myFaces, blah blah)[0]
Same thing here, the polyProjection command returns a list. Added [0] to only store the first value into variable myProjection
'{}.projectionHeight'.format(myProjection)
This is string formatting in Python. It replaced the brackets { } with value stored in myProjection
Here's the mel version, I modified it to work with multiple objects just in case. I didn't know about the curly braces thing with python, that's neat! Though in this case doing (myProjection = '.projectionHeight') would also work.
$objs = `ls -sl`;
for ($obj in $objs) {
select $obj;
ConvertSelectionToFaces;
polyProjection -ch 1 -type Planar -ibd on -kir -md y;
$selectedObject = `ls -sl`;
print $selectedObject;
// $selectedObject is an array containing the whole face selection, then the name of the projection node, like so.
print $selectedObject[1];
// We only want the projection node name, so we'll make a new string and concatenate with the attribute.
$projNode = $selectedObject[1];
setAttr ($projNode + ".projectionHeight") 400;
}<br>
Replies
I didn't know about the curly braces thing with python, that's neat! Though in this case doing (myProjection = '.projectionHeight') would also work.
ConvertSelectionToFaces;
$selectedObject = `polyProjection -ch 1 -type Planar -ibd on -kir -md y`;
setAttr ($selectedObject [0] + ".projectionHeight") 400;
setAttr ($selectedObject [0] + ".projectionWidth") 400;