Bake rotation pivot runs extremely slow, any other options?

Bake rotation pivot runs extremely slow, any other options?

malcolm_341
Collaborator Collaborator
2,962 Views
14 Replies
Message 1 of 15

Bake rotation pivot runs extremely slow, any other options?

malcolm_341
Collaborator
Collaborator

In a couple of my Mel scripts I use BakeCustomPivot; to regenerate the rotation pivot on objects that have had their transform frozen, today I realized this command runs unacceptably slow on anything over 40,000 verts. Does know another way I can regenerate the x, y, z rotation of an object other than this command. Or, I've been trying to figure out a way to transfer that rotation pivot from one object to another so I can run it on an object with a single face, but I can't figure out how to get that working either. Any suggestions?

0 Likes
Accepted solutions (2)
2,963 Views
14 Replies
Replies (14)
Message 2 of 15

brentmc
Autodesk
Autodesk

Hi Malcolm,

 

This is a bug in the -preserveGeometryPosition flag on the transform commands so I've opened an issue for this.

 

MAYA-109930 - Bake Pivot runs unacceptably slow on large meshes

 

Now, for workarounds.

When baking/freezing a pivot the basic operation is to apply some transform and then apply a compensating transform to the geometry so it remains in the same place.

 

So, for example, if I do something like:

 

1) rotate a mesh by 90 in Z

2) freeze rotation

3) rotate -90 in Z

 

I now have my geometry in the same place but my Z rotation is now -90 so I have baked the rotation change into the geometry.

 

So, depending on your needs, you might be able to achieve the result your are after using freeze which doesn't have the performance issue.

 

Also, BakeCustomToolPivot is implemented as a Mel script that is in your Maya installation so you can open it and inspect how the command works which would be useful when trying to find a workaround.

--

Brent

Brent McPherson
Principle Software Developer
0 Likes
Message 3 of 15

malcolm_341
Collaborator
Collaborator

Thanks a lot, I spent about 5 hours on this problem yesterday, after much failure I figured out a could duplicate a single face and bake pivot on that since it's only one face it's instant and then parent my mesh under that mesh and then un-rotate it using the parent. I'll report back if I can get this working.

0 Likes
Message 4 of 15

brentmc
Autodesk
Autodesk

Hi Malcolm,

 

Here is a little Python script that shows how to bake a rotation using freeze as I described before. This (combined with the logic in bakeCustomToolPivot) should hopefully be enough to workaround the bug in your scenario.

It does the following 3 steps:
1) rotate the transform by the inverse rotation

2) freeze the rotation

3) rotate the transform by the desired rotation

 

 

# Bake rotation example using freeze
import math
from maya import cmds
from maya.api import OpenMaya as om

# This is the desired world-space rotation
rot = om.MEulerRotation(0, 0, math.radians(-90))

# Generate an inverse rotation
invRot = rot.inverse().reorder(om.MEulerRotation.kXYZ)

# Rotate the transform by the inverse rotation
cmds.rotate(math.degrees(invRot[0]), math.degrees(invRot[1]), math.degrees(invRot[2]), absolute=1, worldSpace=1, forceOrderXYZ=1)

# Freeze the rotation
cmds.makeIdentity(apply=1, rotate=1, preserveNormals=1)

# Rotate to the desired/final rotation
cmds.rotate(math.degrees(rot[0]), math.degrees(rot[1]), math.degrees(rot[2]), absolute=1, worldSpace=1, forceOrderXYZ=1)

 

 --

Brent

Brent McPherson
Principle Software Developer
0 Likes
Message 5 of 15

malcolm_341
Collaborator
Collaborator

Thanks for you reply, in my situation the transform has already been frozen at an arbitrary angle so I need to first regenerate the transform based on a single user face selection which is what I need the bake pivot for.

 

I've found if you select a face and change the move tool to component you can capture the angle of the move tool and then feed that into the rotate pivot and then bake the pivot to effectively rebuild the frozen transform with no knowledge of the object's original rotation before freezing, because bake pivot runs so slow I need to write a hack to bake pivot on a different mesh and then apply the rotation back to my originally object. Pretty messy, but I got something working last night which I'll share here for reference once I test it a bit more.

0 Likes
Message 6 of 15

malcolm_341
Collaborator
Collaborator
Accepted solution

So here's my workaround, I'm basically tricking the system into running bakeCustomToolPivot 1 1; on a single face and then pasting that rotation information back to my object that has thousands of faces. This is a lot of code, maybe there's a simpler way to do, but it does get around the slowness bug. Thanks for your help Brent, it was your explanation of rotating the object freezing it and then rotating it back which led me to this solution. To use the script below select a single face and run it. It will magically regenerate transform based on that face and will avoid the slowness of the bake pivot bug.

 

 

 

 

global proc m341_duplicateFace()
{
	string $selected_Faces[] = `ls -sl -fl`;
	toggleSelMode;
	toggleSelMode;
	selectMode -object;
	makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 -pn 1;
	
	$selected_Obj_1 = `ls -sl`;
	duplicate -rr;
	$selected_Obj_2 = `ls -sl`;
	
	
	//Loop through array and swap names
	string $newComponentSel[] = {};
	for ($item in $selected_Faces)
	{
		string $comp = `match "\\..+" $item`;
		$newComponentSel[size($newComponentSel)] = ($selected_Obj_2[0] + $comp);
	}

	SelectFacetMask;
	select $newComponentSel;
	InvertSelection;
	Delete;
	toggleSelMode;
	toggleSelMode;
	selectMode -object;
	rename duplicated_1;
}

//Store first object and faces and pivot
string $userSelectedFace[] = `ls -sl`;
toggleSelMode;
toggleSelMode;
selectMode -object;
CenterPivot;
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 -pn 1;
MoveTool;
float $moveToolPOS[] = `manipMoveContext -q -position "Move"`;
string $userSelectedObj[] = `ls -sl`;

//Duplicate face and rebuild transform on new object
SelectFacetMask;
select $userSelectedFace;
m341_duplicateFace();
string $rotatorObj[] = `ls -sl`;
ConvertSelectionToFaces;
string $rotatorFace[] = `ls -sl`;

//Store rebuilt rotation and translation in variable for later
//Switch to move tool component mode
MoveTool;
manipMoveContext -edit -mode 10 Move;

//Get move tool location
float $locatorTargetPOS[] = `manipMoveContext -q -position "Move"`;
//Get move tool angle and convert radius to degrees
float $locatorTargetAngle[] = `manipMoveContext -q -orientAxes "Move"`;
$locatorTargetAngle[0] = rad_to_deg ($locatorTargetAngle[0]);
$locatorTargetAngle[1] = rad_to_deg ($locatorTargetAngle[1]);
$locatorTargetAngle[2] = rad_to_deg ($locatorTargetAngle[2]);

//Adjust rotate pivot so it aligns with average component selection from above
manipPivot -o $locatorTargetAngle[0] $locatorTargetAngle[1] $locatorTargetAngle[2];
toggleSelMode;
toggleSelMode;
selectMode -object;
bakeCustomToolPivot 1 1;

//Parent object under face with rebuilt transform
parent $userSelectedObj $rotatorObj;

//Unrotate parent face so child gets rotated back to zero correctly
select $rotatorObj;
rotate -os -fo 0 0 0;

//Freeze transform of object and unparent
select $userSelectedObj;
makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 -pn 1;	
parent -w;
	
//Group for rotating
doGroup 0 1 1;
string $group[] = `ls -sl`;
xform -worldSpace -pivots $moveToolPOS[0] $moveToolPOS[1] $moveToolPOS[2];

//Rotate group back to original angle using variable from above and move back to where it was
rotate -r -os -fo $locatorTargetAngle[0] $locatorTargetAngle[1] $locatorTargetAngle[2];

//Clean up
ungroup;
delete $rotatorObj;

		
	

 

 

 

Message 7 of 15

brentmc
Autodesk
Autodesk
Accepted solution

Just an update to say that the Bake Pivot performance issue is resolved in Maya 2022.1

--

Brent

Brent McPherson
Principle Software Developer
0 Likes
Message 8 of 15

malcolm_341
Collaborator
Collaborator

That's great, thanks for updating the issue. Looking forward to the service pack.

0 Likes
Message 9 of 15

tom_melson
Contributor
Contributor

On the subject of BakeCustomPivot, it doesn't seem to work when I call it from within Maya batch. I'm just calling cmds.select to select the transforms then calling BakeCustomPivot. Is there a reason it doesn't seem to work in this context?

0 Likes
Message 10 of 15

brentmc
Autodesk
Autodesk

Hi,

What are you expecting to happen in batch mode?

Bake custom pivot takes the pivot information from the move/rotate/scale tool and bakes it into the transform so it doesn't really apply in batch mode where there are no interactive transform tools.

Your best bet is to look at the bakeCustomToolPivot.mel script in your Maya install and you will be able to see exactly what the script is doing (e.g. what underlying commands it is calling) and you might be able to adapt that to suit your needs.

Brent McPherson
Principle Software Developer
0 Likes
Message 11 of 15

tom_melson
Contributor
Contributor

Hi, thanks for the quick response.

 

To be honest, I expected it to function okay in batch mode, since afaik there's no actual user interaction, it's only adjusting attribute values of transform nodes (rotatePivot, scalePivot & translate).

 

For context, we run publish asset processes in the background in Maya batch, which does things like freeze transforms etc.

 

Anyway, I guess I need to go a bit lower into the code to find the actual cmds used. Thanks for the steer on that file, which I've found. I did try "whatIs BakeCustomPivot" but it just came back with "Run Time Command".

 

Actually, looking at the code, I can see it does do other stuff, specifically with the orientation of the manipulators etc. so that does make sense now. I guess I just need to implement a subset of what it's doing, namely adjusting the pivot positions, translations etc.

 

Thanks again

0 Likes
Message 12 of 15

brentmc
Autodesk
Autodesk

No problem.

The key commands in there are rotate/move commands with the -preserveChildPosition/-preserveGeometryPosition flags.

xform is also used to zero out the pivots.

Brent McPherson
Principle Software Developer
0 Likes
Message 13 of 15

tom_melson
Contributor
Contributor

Great, cheers!

0 Likes
Message 14 of 15

tom_melson
Contributor
Contributor

Thought I'd come back quickly on this; I got what I needed, thanks again. And just to say that, looking at the code, I think it could be changed so that:

 

    BakeCustomPivot($ori=0)

 

could be made to work in batch mode. Maybe not worth it but possible.

 

Cheers!

 

Message 15 of 15

brentmc
Autodesk
Autodesk

Thanks Tom,

Since the script has "Tool" in the name it is probably not worth it. (though I agree in theory the script could just detect batch mode and ignore orientation and issue  a warning)

Brent McPherson
Principle Software Developer