MEL Automated Constraint Switch Expression Running but Not Working


#1

Hello all,

I’m having trouble working on an expression I’ve been trying to create while I learn MEL. So forewarning… I don’t have much of a background in programming and I’m new to MEL.

The expression I’m trying to make is for a rubik’s cube rig (I wanted to do it my own way from the tutorials I’ve seen out there). It would turn on orient constraints when a control is selected so that the cubes on that side would then be manipulated. So what I’ve done is create two sets of locators (one for where each cube should be and one for where each cube actually is) and I’m checking their positions against each other so when the cube locators match particular position locators the cube at that location has its constraints turned on and all other constraints turned off. I’ve only been working on one side and I’ve got my expression to the point at which it SEEMS error free (no syntax or other errors thrown in the Script Editor finally)… but it does nothing, no constraints change. After having to go back and forth fixing scope errors and then printing various variables to see if they are operating I’ve found the for loops I’ve got running don’t even seem to be executing? maybe?

Anyway I’m expecting someone will probably show me a whole slew of problems in my concept and or my code and prove how much of an idiot I am but can anyone help?

here is some of my code… (I left out the arrays of objects that are loaded at the beginning)


//Query selection...
float $red_Ctrl[] = `xform -q -ws -rp red_ctrl1GRP`;
float $red_CtrlX = $red_Ctrl[0];
float $red_CtrlY = $red_Ctrl[1];
float $red_CtrlZ = $red_Ctrl[2];
string $sel_Ctrl[] = `ls -selection`;
float $CubePos[];
float $LocPos[];



//Check if red control is selected...
if (size($sel_Ctrl) >= 1){
    float $sel_Loc[] = `xform -ws -q -rp $sel_Ctrl`;
    float $sel_LocX = $sel_Loc[0];
	float $sel_LocY = $sel_Loc[1];
	float $sel_LocZ = $sel_Loc[2];
	if ($sel_LocX == $red_CtrlX){
	    if ($sel_LocY == $red_CtrlY){
	        if($sel_LocZ == $red_CtrlZ){
		        //cycle through obj_cubescheck locators to check location with red side locators
	    		for ($Cubes = 0; $Cubes < size($Obj_CubesCheck); ++$Cubes){
	    		    float $CubePos[] = `xform -q -ws -rp $Obj_CubesCheck[$Cubes]`;
			    	float $CubePosX = $CubePos[0];
		    		float $CubePosY = $CubePos[1];
	    			float $CubePosZ = $CubePos[2];
    				for ($Locs = 0; $Locs < size($Loc_Checks); ++$Locs){
					float $LocPos[] = `xform -q -ws -rp $Loc_Checks[$Locs]`;
				    	float $LocPosX = $LocPos[0];
		    			float $LocPosY = $LocPos[1];
	    				float $LocPosZ = $LocPos[2];
    					if ($CubePosX == $LocPosX){
						    if ($CubePosY == $LocPosY){
				    			if ($CubePosZ == $LocPosZ){
		    						//Change contraint to control to on
    								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.red_ctrl1W0") 1;
								    setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.green_ctrl1W1") 0;
									setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.orange_ctrl1W2") 0;
									setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.blue_ctrl1W3") 0;
									setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.white_ctrl1W4") 0;
									setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.Yellow_ctrl1W5") 0;
							    }
						    }
					    }
				    }
			    }
		    }
	    }
      }
}

EDITED: for code formatting due to newb ignorance.


#2

Hi,
unfortunately it is quite hard to read the code. Please use the “#” sign in the message composer window to enclose the code in code tags like this:


//Query selection...
float $red_Ctrl[] = `xform -q -ws -rp red_ctrl1GRP`;
float $red_CtrlX = $red_Ctrl[0];
float $red_CtrlY = $red_Ctrl[1];
float $red_CtrlZ = $red_Ctrl[2];
string $sel_Ctrl[] = `ls -selection`;
float $CubePos[];
float $LocPos[];



//Check if red control is selected...
if (size($sel_Ctrl) >= 1){
    float $sel_Loc[] = `xform -ws -q -rp $sel_Ctrl`;
    float $sel_LocX = $sel_Loc[0];
    float $sel_LocY = $sel_Loc[1];
    float $sel_LocZ = $sel_Loc[2];
    if ($sel_LocX == $red_CtrlX){
        if ($sel_LocY == $red_CtrlY){
	    if($sel_LocZ == $red_CtrlZ){
		        //cycle through obj_cubescheck locators to check location with red side locators
	    		 for ($Cubes = 0; $Cubes < size($Obj_CubesCheck); ++$Cubes){
	    		     float $CubePos[] = `xform -q -ws -rp $Obj_CubesCheck[$Cubes]`;
			     float $CubePosX = $CubePos[0];
		    	     float $CubePosY = $CubePos[1];
	    		     float $CubePosZ = $CubePos[2];
    				for ($Locs = 0; $Locs < size($Loc_Checks); ++$Locs){
					float $LocPos[] = ds`xform -q -ws -rp $Loc_Checks[$Locs]`;
				    	float $LocPosX = $LocPos[0];
		    			float $LocPosY = $LocPos[1];
	    				float $LocPosZ = $LocPos[2];
    				 	if ($CubePosX == $LocPosX){
						     if ($CubePosY == $LocPosY){
				    			 if ($CubePosZ == $LocPosZ){
		    					 	//Change contraint to control to on
    								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.red_ctrl1W0") 1;
							        setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.green_ctrl1W1") 0;
								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.orange_ctrl1W2") 0;
								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.blue_ctrl1W3") 0;
								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.white_ctrl1W4") 0;
								setAttr ($Obj_Cubes[$Cubes] + "_orientConstraint1.Yellow_ctrl1W5") 0;
							    }
						    }
					    }
				    }
			    }
		    }
	    }
      }
}

I have no idea at the moment, but you can check if some of the conditions are true by printing a message. e.g. you are comparing the rotation pivot position what can be critical unless you are sure that the other objects will have exact the same position at some point. Often you only need to know it e.g. the x postion is greater than the other.


#3

Ah ok apologies… haven’t posted much on the forums so I wasn’t sure what the method was to post code… though I should have looked it up. Thanks for the heads up.

Anyway, I should clarify on that I guess. The point of two locator sets is that each individual cube has it’s rotation set to the center of the rubik’s cube as a whole… and checking at the center would just greenlight all location checks and turn all constraints on all the time. The first set of locators are individually point constrained to each cube and checked against the second set of locators is for each resting location of the cubes when they are not be rotated and they have no constraints to anything but the global control. Because the first set of locators are moving and there are multiple locators in anyone axis direction I figured it had to be specific locations where the actions were performed (thus the second set of locators).

I’ve tried setting various print checks to see what’s working but I never get anything returned beyond the checking if red_ctrl is selected so it doesn’t even SEEM like the for loop is running in the first place… but I’ll run more checks.