Use MEL to use one object to control Smooth Mesh Preview of another?

Use MEL to use one object to control Smooth Mesh Preview of another?

Anonymous
Not applicable
1,188 Views
3 Replies
Message 1 of 4

Use MEL to use one object to control Smooth Mesh Preview of another?

Anonymous
Not applicable
I want to add a custom attribute to an object that let's me control the smooth mesh preview of a different object. Simplified version like this:

-Already have two meshes named "object1" and "object2".

-Add attribute to the object1.
-Integer, min 0, max 2, default 0
-When the new attribute is 0, make smooth mesh preview "off" (same as pressing 1) on object2.
-When the new attribute is 1, make smooth mesh preview "1" (same as pressing 2) on object2.
-When the new attribute is 2, make smooth mesh preview "2" (same as pressing 3) on object2.

I seem to have trouble getting this to always work... Any ideas?

This code works when I run it from the Script Editor, but it doesn't say valid after that. I was not able to use an Expression because the smooth mesh preview does not like to be connected that way. But I could very well be doing something wrong...


string $obj1 = "object1";
string $obj2 = "object2";

addAttr -ln "smoothLevel" -at long -min 0 -max 2 -dv 0 $obj1;
setAttr -e-keyable true ($obj1 + ".smoothLevel");

int $value = `getAttr ($obj1 + ".smoothLevel")`;

if ($value == 0){
setAttr ($obj2 + ".displaySmoothMesh") 0;
}
if ($value == 1){
setAttr ($obj2 + ".displaySmoothMesh") 1;
}
if ($value == 2){
setAttr ($obj2 + ".displaySmoothMesh") 2;
}
0 Likes
1,189 Views
3 Replies
Replies (3)
Message 2 of 4

lee.dunham
Collaborator
Collaborator
im assuming its due to the way expressions work.

as an expression, try something more dynamic like (although it does require the attribute to have already been created);

object2.displaySmoothMesh = object1.smoothLevel ;


which simplified, would be exactly the same as using the connectionEditor to directly connect the two attributes (which would actually evaluate faster).
0 Likes
Message 3 of 4

Anonymous
Not applicable
Unfortunatley displaySmoothMesh is not an attribute that shows up in the connection editor. I wish it did. Maya just won't let me "connect" it to anything.

And running this also does not work for just simply tring to connect two Cube objects with one of them having a custom "smooth" attribute that is an int ranging from 0 - 2.


connectAttr -f pCube1.smooth pCubeShape2.displaySmoothMesh;


It says:

// Error: The attribute 'pCubeShape2.displaySmoothMesh' cannot be connected to 'pCube1.smooth'. //
0 Likes
Message 4 of 4

lee.dunham
Collaborator
Collaborator
hmm yeah, it doesnt seem as straight forward as that.
this works,
int $sj=`scriptJob -kws -ac pSphere1.smooth "setDisplaySmoothness `getAttr pSphere1.smooth`"` ;

to kill it:
scriptJob -kill $sj ;

but i dont recommend using a scriptJob (unless you parent it to a UI).
documentation on using attribute change flag says that by specifying a dependancy node and attribute (id assume recognised as pSphere1.smooth), on deletion of the dependancy node, the job is killed, which is untrue. the job is still present after the node is deleted, which is a pain.

so this does work, but comes with drawbacks of a scriptJob.

I suggest perhaps looking into how maya alters the smoothness looking at the "setDisplaySmoothness.mel" (which uses "displaySmoothness.mel"), which I dont have the time atm to check but will in a few hours.
0 Likes