Home Technical Talk

Mel script question need help with syntax and setAttr.

polycount sponsor
Offline / Send Message
malcolm polycount sponsor
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.

ConvertSelectionToFaces;
polyProjection -ch 1 -type Planar -ibd on -kir  -md y;
$selectedObject = `ls -sl`;
print $selectedObject;
setAttr "$selectedObject.projectionHeight" 400;


Replies

  • Panupat
    Offline / Send Message
    Panupat polycounter lvl 15
    You will need to learn string operation. When  you encase your variable in double quotes, Maya is using it as is without evaluating it's value.

    It's been too long since I code in MEL, let me try this in Python.

    type="Planar", ibd=True, kir=True, md="y")[0]<br>    cmds.setAttr('{}.projectionHeight'.format(myProjection), 400)from maya import cmds<br><br>selections = cmds.ls(sl=1)<br>for s in selections:<br>    myFaces = cmds.polyListComponentConversion(s, toFace=1)[0]<br>    cmds.select(myFaces)<br>    myProjection = cmds.polyProjection(myFaces, 

    Explanation:

    </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
  • throttlekitty
    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>
  • malcolm
    Offline / Send Message
    malcolm polycount sponsor
    Hi guys, thanks for your replies very helpful. My friend at work was also able to help me solve it by attacking the problem in a different way.

    ConvertSelectionToFaces;

    $selectedObject = `polyProjection -ch 1 -type Planar -ibd on -kir  -md y`;

    setAttr ($selectedObject [0] + ".projectionHeight") 400;

    setAttr ($selectedObject [0] + ".projectionWidth") 400;


Sign In or Register to comment.