-2

hie i have a slight confusion as to why what writing is not working. in my MEL script editor I'm writing

string $Adistance = ("distanceDimensionShape1"+".distance");
expression -s (" $Bdistance = $Adistance; joint2.scaleX = $distance");

but I get this error

// Warning: line 1: Converting string "distanceDimensionShape1.distance" to a float value of 0. // 
  • I want joint2.scaleX to copy distanceDimensionShape1.distance float
3
  • SORRY wrote it incorrectly this is the real problem float $distance = ("distanceDimensionShape1"+".distance"); expression -s (" $distance = $distance;");
    – Raspberry
    Feb 16, 2015 at 15:23
  • What exactly is it you're trying to achieve with this expression and MEL script? It's hard to discern the solution with an expression that only sets a variable. And when you need to clarify the question, it often makes more sense to edit the question itself rather than cram the change in a comment.
    – mhlester
    Feb 16, 2015 at 16:18
  • I want joint2.scaleX to copy distanceDimensionShape1.distance float
    – Raspberry
    Feb 16, 2015 at 16:48

2 Answers 2

1

Ok, so first I'm not a Mel programmer, I usually do my script in Python so there might be syntax errors in my code.

Your problem:

Correct me if I'm wrong, but I think you are trying to get the distance attribute of distanceDimensionShape1 into a variable and set it to scaleX attribute from joint2.

Your code:

string $Adistance = ("distanceDimensionShape1"+".distance");
expression -s (" $Bdistance = $Adistance; joint2.scaleX = $distance");

What you are doing in your 1st line: You are declaring a string variable containing "distanceDimensionShape1.distance", not getting the distance attribute of distanceDimensionShape1

What you should be doing in your 1st line: Use the getAttr command provided in maya docs the retrieve the attribute of your shape.

What you are doing in your second line: You are trying to set joint2.scaleX which is a float value with a string value. I guess... because I don't know what's $distance as it appears only here in your code.

What you should be doing in your second line: Use setAttr to properly set your attribute.

My solution:

I hope this will help as we have only few informations on your current problem:

float $Adistance = `getAttr distanceDimensionShape1.distance`;
setAttr joint2.scaleX  $Adistance;

1st line retrieves properly the selected attributes and storages it to a float. 2nd line set your attribute with the retrieved value.

Note:

  • You can watch which methods are called when doing manipulation in maya by opening your script editor and checking History > Echo All Commands. This way you will be able to reproduce Maya's behaviour.
  • Always have an internet browser pointing to Maya's Mel/Python doc when scripting: link
  • Try to develop more when posting a question on SO:

    1. What are your trying to achieve and how you plan to do it

    2. A commented block of code (approx 10-15 lines is fine and give a good overview of your mel script)

    3. What's your error message

Hope this will help.

1

If you want to create an expression once then change what connects to it, create it the following way:

expression -s ("joint2.scaleX = .I[0]")

Then you can connect a specific attribute to that plug like this:

connectAttr distanceDimensionShape1.distance expression1.input[0]

This is assuming there's a legitimate reason you couldn't just write the expression once directly:

expression -s ("joint2.scaleX = distanceDimensionShape1.distance")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.