MEL Expression doesn't update/evaluate on changing attribute.

MEL Expression doesn't update/evaluate on changing attribute.

Anonymous
Not applicable
2,490 Views
3 Replies
Message 1 of 4

MEL Expression doesn't update/evaluate on changing attribute.

Anonymous
Not applicable

Hello, I'm currently digging into more advanced rigging techniques, just started scritping and I've a probably easy mel scripting question. I want to have a single Enum attribute "slider" that changes the translation of an object (to later on snap locators to each others and switch through different systems). I'll demonstrate what I want to achieve through an example:
By the way I'm by no means a coder, I've almost no experience in programming.

little gif, easy enough so far:
IRhwWP6.gif

 

  -Super basic expression code:

 

if (pCube2.State == 0){
	pCube2.translateX = -2;
}
else if (pCube2.State == 1){
	pCube2.translateX = 0;
}
else {
	pCube2.translateX = 2;
}

Bad thing is now the actual values of the cube are locked for the user, and it just feels messy.

So I found through some autodesk educational video the command "xform" with which I can actually catch and instert the local or world positions of objects into each other, and values stay unlocked, awesome!... well no.... now the enum attribute doesn't do anything anymore, or doesn't update, I have to hit "edit" in the expression editor to get a change. Thats aboult how far I am able to code lol, no idea why this doesn't work, I looked up some mel videos and maya docs but can't figure this out.

gif and code of my current state:

IRhwWP7.gif

 

  -Code:

 

if (pCube2.State == 0){
 //(pCube2.State <= 0)

	//pCube2.translateX = -4;	

	$StateA = `xform -query -objectSpace -translation pSphere1`;
	xform -objectSpace -translation $StateA[0] $StateA[1] $StateA[2] pCube2;

}

else if (pCube2.State == 1){
      //(pCube2.State >= 1 && pCube2.State <= 1.9)

	//pCube2.translateX = 0;

	xform -objectSpace -translation 0 0 0 pCube2;

}

else {

	//pCube2.translateX = 4;

	$StateB = `xform -query -objectSpace -translation pSphere2`;
	xform -objectSpace -translation $StateB[0] $StateB[1] $StateB[2] pCube2;

}

 

I even tried to clamp the if conditions because I know the enum attribute isn't really a list in maya and uptades crazy if you middle mouse button drag within the viewport in values with 2 decimals like:

setAttr "pCube2.State" 0.13;
setAttr "pCube2.State" 0.11;
setAttr "pCube2.State" 0.3;
setAttr "pCube2.State" 0.24;
setAttr "pCube2.State" 0.54;
setAttr "pCube2.State" 1.81;

Didn't do anything 😕

Thanks for the read! Thanks for every comment to this and have a nice day!
 - Martin
0 Likes
Accepted solutions (1)
2,491 Views
3 Replies
Replies (3)
Message 2 of 4

rajasekaransurjen
Collaborator
Collaborator

Hi,

Try this...

if (pCube2.State == 0)
{
    float $StateA[] = `xform -query -objectSpace -translation pSphere1`;
	pCube2.translateX = $StateA[0] ;
	pCube2.translateY = $StateA[1];
	pCube2.translateZ = $StateA[2];
}

else if (pCube2.State == 1)
{
    pCube2.translateX = 0;
	pCube2.translateY = 0;
	pCube2.translateZ = 0;
}

else 
{
    float $StateB[] = `xform -query -objectSpace -translation pSphere2`;
	pCube2.translateX = $StateB[0] ;
	pCube2.translateY = $StateB[1];
	pCube2.translateZ = $StateB[2];
}
0 Likes
Message 3 of 4

Anonymous
Not applicable

Thanks for the reply! This does solve one problem. It updates the position of the cube accordingly to the spheres position but the inital problem of the locked attributes is present again. In the end the idea is to input values interactively into a controller from an animated constraint but to also be able to edit this values manually. I can't use just a parent group higher in the hierarchy because I need the changing object position values in the controller. Sounds probably a bit weird, anyway that's another problem, back to the script.

Something very strange is happening, discovered this by accident.
To test this I animated both spheres along an orbit around the cube, blue Sphere1 (StateA) and red Sphere2 (StateB).
But this whole test "scene" isn't placed in the origin of the scene, all objects (cube and both spheres) are childs of the world and have frozen, zero'd out, values. I animated a rotation into the red StateB Sphere (pSphere2) just for visuals that the parented "B" letter looks better. And now our little script does something weird. Despite all we do is input translationX,Y,Z values the cube rotates around the origin of the world. I think we broke the xform command.

 

Current expression script:

if (pCube2.State == 0){

	float $StateA[] = `xform -query -objectSpace -translation pSphere1`;
		pCube2.translateX = $StateA[0];
		pCube2.translateY = $StateA[1];
		pCube2.translateZ = $StateA[2];

}

else if (pCube2.State == 1){

	pCube2.translateX = 0;
	pCube2.translateY = 0;
	pCube2.translateZ = 0;

}

else {

	float $StateB[] = `xform -query -objectSpace -translation pSphere2`;
		pCube2.translateX = $StateB[0];
		pCube2.translateY = $StateB[1]; 
		pCube2.translateZ = $StateB[2];

}

Did furhter testing, the script indeed behaves like the sphere has an origin in the scene origin. Something with worldspace/objectspace doesn't add up I guess:
5TClAH8 - Imgur.gif


Longer video:

 

 

0 Likes
Message 4 of 4

mspeer
Consultant
Consultant
Accepted solution

Hi!

"the idea is to input values interactively into a controller from an animated constraint but to also be able to edit this values manually"

This is not possible.

0 Likes