Maya - How to Ungroup animation and keep animation? - maya

There are some old animations that I want to reuse, but the old animation use different axis (For example: old: Face negative Z, Y as up; new: Face Y, Negative Z as up). So I fix this by grouping the animation and rotate the group so that it faces the right axis. But when I ungroup, it's only work for current frame (I turn Auto key on).
I have search several forums:
Grouping animated objects, Scaling, Ungrouping
Need help maintaining offset during ungroup
ungrouping but keeping animation
Grouping animated objects, Scaling, Ungrouping
But nothing works.

Ok, it works for the current frame when you ungroup. So to make it work for the entire animation, you can group & un-group for all the frames. Of course I won't do it by hand but I'll do it with script.
proc GetPlaybackRange(string $bone, int $outStartEndTime[])
{
float $arrKey[] = `keyframe -q $bone`;
$arrKeyLength=size($arrKey);
$outStartEndTime[0] = floor($arrKey[0]);
$outStartEndTime[1] = ceil($arrKey[$arrKeyLength-1]);
}
proc UngroupAndGroupNextFrame(int $frame, string $groupName, float $transform[])
{
currentTime $frame ;
ungroup;
currentTime ($frame + 1) ;
group -n $groupName;
xform -worldSpace -matrix
$transform[0]
$transform[1]
$transform[2]
$transform[3]
$transform[4]
$transform[5]
$transform[6]
$transform[7]
$transform[8]
$transform[9]
$transform[10]
$transform[11]
$transform[12]
$transform[13]
$transform[14]
$transform[15]
$groupName;
}
proc UnGroupForAnimation()
{
string $sel[]= `ls -sl`;
string $groupName = $sel[0];
float $transform[];
$transform = `xform -q -worldSpace -matrix $groupName`;
string $bone[] = `listRelatives -children $groupName`;
int $startEndTime[];
GetPlaybackRange($bone[0], $startEndTime);
for($i = $startEndTime[0]; $i <= $startEndTime[1]; $i++)
{
UngroupAndGroupNextFrame($i, $groupName, $transform);
}
currentTime ($startEndTime[1] + 1) ;
ungroup;
timeSliderClearKey;
print ($bone[0] + " range "+$startEndTime[0]+" : "+$startEndTime[1]);
}
UnGroupForAnimation();
Usage of the script:
Step 1. Select the group (the children of the group should be the bone)
Step 2. Run the script.
And that's it.

Related

Repeat current poly reduce function on multiple objects that are selected?

I'm looping through multiple objects, but the loop stops before going to the next object.
Created a loop with condition. If condition is met, it calls a ReduceEdge() function. Problem is it will only iterate once and not go to the next object and repeat the procedure.
global proc ReduceEdge()
{
polySelectEdgesEveryN "edgeRing" 2;
polySelectEdgesEveryN "edgeLoop" 1;
polyDelEdge -cv on;
}
string $newSel[] = `ls -sl`;
for($i = 0; $i < size($newSel); $i++)
{
select $newSel[$i];
int $polyEval[] = `polyEvaluate -e $newSel[$i]`;
int $temp = $polyEval[0];
for($k = 0; $k < $temp; $k++)
{
string $polyInfo[] = `polyInfo -fn ($newSel[$i] + ".f[" + $k + "]")`;
$polyInfo = stringToStringArray($polyInfo[$i]," ");
float $vPosX = $polyInfo[2];
float $vPosY = $polyInfo[3];
float $vPosZ = $polyInfo[4];
if($vPosX == 0 && $vPosY == 0 && $vPosZ == 1.0)
{
select ($newSel[$i] + ".e[" + $k + "]");
ReduceEdge();
}
}
}
Expected results:
If I select 4 cylinders, all their edges will reduce by half the current amount.
Actual results:
When 4 cylinders are selected, only one reduces down to half the edges. The rest stay the same.
Since my comment did help you out, I'll try and give a more thorough explanation.
Your first loop (with $i) iterates over each object in your selection. This is fine.
Your second loop (with $k) iterates over the number of edges for the current object in the loop. So far, so good. Though, I'm wondering if it would be more correct to loop of the number of faces...
Now you ask for an array of all face normals of the face at index $k at object $i, with string $polyInfo[] = `polyInfo -fn ($newSel[$i] + ".f[" + $k + "]")`;.
If you try and print the size and values in $polyInfo, you'll realize you have an array with one element, which is the face normal of the particular face you queried just before. Therefore, it will always be element 0, and not $i, which would increases with every iteration.
I have made a Python/PyMEL version of the script, which may be nice for you to see.
import pymel.core as pm
import maya.mel as mel
def reduceEdge():
mel.eval('polySelectEdgesEveryN "edgeRing" 2;')
mel.eval('polySelectEdgesEveryN "edgeLoop" 1;')
pm.polyDelEdge(cv=True)
def reducePoly():
selection = pm.ls(sl=True)
for obj in selection:
for i, face in enumerate(obj.f):
normal = face.getNormal()
if (normal.x == 0.0 and normal.y == 0.0 and normal.z == 1.0):
pm.select(obj + '.e[' + str(i) + ']')
reduceEdge()
reducePoly()

Maya Mel Script - Drop Pivot to Bottom of object?

I see someone asked this before (for example: here). So how to automatically set the pivot to the bottom of the model?
To do that:
Step 1: find the bottom point.
Step 2: Set the pivot to the bottom point.
Here is the mel script help you do that:
string $sel[]= `ls -sl`;
//$sel[0] != "" to check if the first item is empty, but `size $sel` == 1 already cover that
if(`size $sel` > 0)
{
int $vtxIdx;
int $vCount[];
float $lowestY = 2147483647.0;
float $crtY = 0.0;
float $pos[];
string $item;
for ($item in $sel)
{
$vCount = `polyEvaluate -vertex $item`; //Get vertex count
for ($vtxIdx = 0; $vtxIdx < $vCount[0]; $vtxIdx++)//Loop through vetex
{
$pos = `xform -q -ws -t ($item+".vtx["+$vtxIdx+"]")`;//Get vertex position
$crtY = $pos[1];
if($crtY < $lowestY)
{
$lowestY = $crtY;//Get the lowest Y
}
}
$pos = `xform -q -ws -t ($item)`;
xform -ws -a -piv $pos[0] $lowestY $pos[2] ($item);
print ($lowestY);
}
}
Usage:
step 1: select the objects that need to set pivot to bottom
step 2: execute the script
The pivot point should be set like this
If your requirement is: the pivot point must be inside the model, then you should edit this script a little bit.
Thank you! I used your code above, 123iamking but I found I wanted to drop pivot to world y0 instead of bounding box.
so I changed some stuff around.
Works on Maya 2020.
string $sel[]= `ls -sl`;
//$sel[0] != "" to check if the first item is empty, but `size $sel` == 1 already cover that
if(`size $sel` > 0)
{
float $pos[];
string $item;
for ($item in $sel)
{
xform -ws -a -cp;
CenterPivot;
$pos = `xform -q -ws -a -piv ($item)`;//Get piv position
xform -ws -a -piv $pos[0] 0 $pos[2] ($item); //set pivot to 0 in world y
}
};

Conversion of MetaTrader4 to NinjaTrader

I am trying to write an indicator originally from MT4 into NT7.
I have the following calculations in MT4:
dayi = iBarShift(Symbol(), myPeriod, Time[i], false);
Q = (iHigh(Symbol(), myPeriod,dayi+1) - iLow(Symbol(),myPeriod,dayi+1));
L = iLow(NULL,myPeriod,dayi+1);
H = iHigh(NULL,myPeriod,dayi+1);
O = iOpen(NULL,myPeriod,dayi+1);
C = iClose(NULL,myPeriod,dayi+1);
myperiod is a variable where I place the period in minutes (1440 = 1day).
What are the equivalent functions in NT7 to iBarShift, iHigh and so on?
Thanks in advance
For NinjaTrader:
iLow = Low or Lows for multi-time frame
iHigh = High or Highs
iOpen = Open or Opens
iClose = Close or Closes
So an example would be
double low = Low[0]; // Gets the low of the bar at index 0, or the last fully formed bar (If CalculateOnBarClose = true)
In order to make sure you are working on the 1440 minute time frame, you will need to add the following in the Initialize() method:
Add(PeriodType.Minute, 1440);
If there are no Add statements prior to this one, it will place it at index 1 (O being the chart default index) in a 2 dimensional array. So to access the low of the 1440 minute bar at index 0 would be:
double low = Lows[1][0];
For iBarShift look at
int barIndex = Bars.GetBar(time);
which will give you the index of the bar with the matching time. If you need to use this function on the 1440 bars (or other ones), use the BarsArray property to access the correct Bar object and then use the GetBar method on it. For example:
int barIndex = BarsArray[1].GetBar(time);
Hope that helps.

Why is NSLayoutAttributeCenterX an 'Invalid pairing' with NSLayoutAttributeWidth

I am trying to have the auto layout manager adjust the center point of a view, based on the width of the super view. I don't understand why that is an 'Invalid Pairing' of the attributes (as told by the crash and NSInvalidArgumentException)
UIView *ac;
NSLayoutConstraint *cXloc = [NSLayoutConstraint constraintWithItem:ac
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:ac.superview
attribute:NSLayoutAttributeWidth
multiplier:.1
constant:x*ac.superview.frame.size.width*.2];
[ac.superview addConstraint:cXloc];
Could someone explain why this is an 'Invalid paring' and how I should approach this?
Thanks
It is the limitation of the current implementation of Auto Layout. However, you can easily work around it since all the constrains are linear and NSLayoutAttributes are correlated. For example, say, the constraint you want is:
subview.centerX = m * superview.width + c;
You can express it as a relationship between tow centerXs:
// Since width == 2 * centerX
subview.centerX = m * 2 * superview.centerX + c;
If you relate ac's AttributeCenterX to its superview's AttributeCenterX, AttributeLeading, or AttributeTrailing, you should be able to express your desired constraint using the multiplier and constraint. Keep in mind that the constant is evaluated only when the constraint is created, and your example's constant wouldn't update as ac.superview's width changes.
If you can express in words how you'd like ac positioned relative to its superview, we can suggest a constraint.
Edit
Here's an example with 5 NSButtons. They themselves and the space between them expand so that the spaces are 30% as wide as the buttons, all the buttons have the same width, and all the spaces have the same width. Creating 4 invisible NSViews just for spacing is pretty cumbersome, especially considering you've got it working outside of autolayout. But in case you're curious:
// Assuming these NSViews and NSButtons exist,
//NSView* superview ;
//NSButton *buttonOne, *buttonTwo, *buttonThree, *buttonFour, *buttonFive ;
[superView removeConstraints:superView.constraints] ;
// Create empty NSViews to fill the space between the 5 buttons.
NSView* spaceOne = [NSView new] ;
NSView* spaceTwo = [NSView new] ;
NSView* spaceThree = [NSView new] ;
NSView* spaceFour = [NSView new] ;
spaceOne.translatesAutoresizingMaskIntoConstraints = NO ;
spaceTwo.translatesAutoresizingMaskIntoConstraints = NO ;
spaceThree.translatesAutoresizingMaskIntoConstraints = NO ;
spaceFour.translatesAutoresizingMaskIntoConstraints = NO ;
[superView addSubview:spaceOne] ;
[superView addSubview:spaceTwo] ;
[superView addSubview:spaceThree] ;
[superView addSubview:spaceFour] ;
NSDictionary* views = NSDictionaryOfVariableBindings(superView,buttonOne,buttonTwo,buttonThree,buttonFour,buttonFive,spaceOne,spaceTwo,spaceThree,spaceFour) ;
// Vertically align buttonOne to its superview however you like.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[buttonOne]" options:0 metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths equal and >= 10. Make the buttons' widths equal.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[buttonOne][spaceOne(>=10)][buttonTwo(==buttonOne)][spaceTwo(==spaceOne)][buttonThree(==buttonOne)][spaceThree(==spaceOne)][buttonFour(==buttonOne)][spaceFour(==spaceOne)][buttonFive(==buttonOne)]|" options: NSLayoutFormatAlignAllCenterY metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths 30% of the NSButtons' widths.
[superView addConstraint: [NSLayoutConstraint constraintWithItem: spaceOne
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: buttonOne
attribute: NSLayoutAttributeWidth
multiplier: 0.3
constant: 0 ] ] ;
Based on an0's answer, and assuming you have an NSArray containing your buttons, the following should space the buttons equally within the superview:
NSUInteger currentButton = 1;
for (UIButton *button in self.buttons)
{
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.0 * (CGFloat) currentButton / (CGFloat) (self.buttons.count + 1) constant:0.0]];
currentButton++;
}
If you're looking to have your programmatically generated views to fit its superview's width. You can use the constraint pairing of NSLayoutAttributeLeading and attribute:NSLayoutAttributeCenterX
You have to make the proper calculation in order to get the right multiplier. Calculation involves the total count of the views to be laid out and the index of the current view.
//Caculate constraint multiplier from parentView CenterX.
//This sets the width of the button relative to parentView.
// A value of 2 = Full width.
CGFloat multiplier = 2/(arr.count/counter);
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:btn
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual toItem:parentView
attribute:NSLayoutAttributeCenterX multiplier:multiplier constant:0]];
This will distribute the views width to fill its superview.

Getting current position of one of the multiple objects in a figure?

I wrote a script that returns several text boxes in a figure. The text boxes are moveable (I can drag and drop them), and their positions are predetermined by the data in an input matrix (the data from the input matrix is applied to the respective positions of the boxes by nested for loop). I want to create a matrix which is initially a copy of the input matrix, but is UPDATED as I change the positions of the boxes by dragging them around. How would I update their positions? Here's the entire script
function drag_drop=drag_drop(tsinput,infoinput)
[x,~]=size(tsinput);
dragging = [];
orPos = [];
fig = figure('Name','Docker Tool','WindowButtonUpFcn',#dropObject,...
'units','centimeters','WindowButtonMotionFcn',#moveObject,...
'OuterPosition',[0 0 25 30]);
% Setting variables to zero for the loop
plat_qty=0;
time_qty=0;
k=0;
a=0;
% Start loop
z=1:2
for idx=1:x
if tsinput(idx,4)==1
color='red';
else
color='blue';
end
a=tsinput(idx,z);
b=a/100;
c=floor(b); % hours
d=c*100;
e=a-d; % minutes
time=c*60+e; % time quantity to be used in 'position'
time_qty=time/15;
plat_qty=tsinput(idx,3)*2;
box=annotation('textbox','units','centimeters','position',...
[time_qty plat_qty 1.5 1.5],'String',infoinput(idx,z),...
'ButtonDownFcn',#dragObject,'BackgroundColor',color);
% need to new=get(box,'Position'), fill out matrix OUT of loop
end
fillmenu=uicontextmenu;
hcb1 = 'set(gco, ''BackgroundColor'', ''red'')';
hcb2 = 'set(gco, ''BackgroundColor'', ''blue'')';
item1 = uimenu(fillmenu, 'Label', 'Train Full', 'Callback', hcb1);
item2 = uimenu(fillmenu, 'Label', 'Train Empty', 'Callback', hcb2);
hbox=findall(fig,'Type','hggroup');
for jdx=1:x
set(hbox(jdx),'uicontextmenu',fillmenu);
end
end
new_arr=tsinput;
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata,box)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
set(dragging,'Position',get(dragging,'Position') + ...
[posDiff(1:2) 0 0]);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end
% Testing purpose input matrices:
% tsinput=[0345 0405 1 1 ; 0230 0300 2 0; 0540 0635 3 1; 0745 0800 4 1]
% infoinput={'AJ35 NOT' 'KL21 MAN' 'XPRES'; 'ZW31 MAN' 'KM37 NEW' 'VISTA';
% 'BC38 BIR' 'QU54 LON' 'XPRES'; 'XZ89 LEC' 'DE34 MSF' 'DERP'}
If I understand you correctly (and please post some code if I'm not), then all you need is indeed a set/get combination.
If boxHandle is a handle to the text-box object, then you get its current position by:
pos = get (boxHandle, 'position')
where pos is the output array of [x, y, width, height].
In order to set to a new position, you use:
set (boxHandle, 'position', newPos)
where newPos is the array of desired position (with the same structure as pos).
EDIT
Regarding to updating your matrix, since you have the handle of the object you move, you actually DO have access to the specific text box.
When you create each text box, set a property called 'UserData' with the associated indices of tsinput used for that box. In your nested for loop add this
set (box, 'UserData', [idx, z]);
after the box is created, and in your moveObject callback get the data by
udata = get(dragging,'UserData');
Then udata contains the indices of the elements you want to update.

Resources