Skip to content

Commit

Permalink
recorder cleanup. record characters
Browse files Browse the repository at this point in the history
  • Loading branch information
IlgarLunin committed Mar 10, 2020
1 parent 499da66 commit dc85cbc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 94 deletions.
30 changes: 27 additions & 3 deletions src/animations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <maya/MObject.h>
#include <maya/MSelectionList.h>
#include <maya/MEulerRotation.h>
#include <maya/MObjectArray.h>
#include <maya/MFnTransform.h>
#include <maya/MFnIkJoint.h>
Expand Down Expand Up @@ -134,7 +135,6 @@ void _Animations::applyAnimationsToMappedObjects()

// this functions will be called later
Recorder::get()->recordFace(timestamp, [object, weight, weightPlug](int frame) {
MFnBlendShapeDeformer faceFn(object);
Recorder::get()->keyframeNumericAttribute(frame, weightPlug, weight);
});
}
Expand Down Expand Up @@ -209,6 +209,22 @@ void _Animations::applyAnimationsToMappedObjects()
boneQuat *= referenceQuat;
boneQuat.normalizeIt();
fnTr.setRotation(boneQuat, MSpace::kWorld);

if(recordingEnabled) {
MVector recordLocation = fnTr.getTranslation(MSpace::kTransform);
MQuaternion recordRotation;
fnTr.getRotation(recordRotation);
Recorder::get()->recordBone(timestamp, [recordLocation, jointPath, recordRotation](int frame) {
Recorder::get()->keyframeNumericAttribute("tx", frame, jointPath, recordLocation.x);
Recorder::get()->keyframeNumericAttribute("ty", frame, jointPath, recordLocation.y);
Recorder::get()->keyframeNumericAttribute("tz", frame, jointPath, recordLocation.z);

MEulerRotation euAngle = recordRotation.asEulerRotation();
Recorder::get()->keyframeNumericAttribute("rx", frame, jointPath, euAngle.x);
Recorder::get()->keyframeNumericAttribute("ry", frame, jointPath, euAngle.y);
Recorder::get()->keyframeNumericAttribute("rz", frame, jointPath, euAngle.z);
});
}
}

jIt.next();
Expand Down Expand Up @@ -274,8 +290,16 @@ void _Animations::animatePropOrTracker(QJsonObject obj, const MDagPath &dagPath)
fn.set(finalTransform);

if(recordingEnabled) {
// if timestamp value not in storage - record
Recorder::get()->recordPropOrTracker(timestamp, dagPath, mayaPosition, mayaRotation);
Recorder::get()->recordPropOrTracker(timestamp, [mayaPosition, mayaRotation, dagPath](int frame){
Recorder::get()->keyframeNumericAttribute("tx", frame, dagPath, mayaPosition.x);
Recorder::get()->keyframeNumericAttribute("ty", frame, dagPath, mayaPosition.y);
Recorder::get()->keyframeNumericAttribute("tz", frame, dagPath, mayaPosition.z);

MEulerRotation euAngle = mayaRotation.asEulerRotation();
Recorder::get()->keyframeNumericAttribute("rx", frame, dagPath, euAngle.x);
Recorder::get()->keyframeNumericAttribute("ry", frame, dagPath, euAngle.y);
Recorder::get()->keyframeNumericAttribute("rz", frame, dagPath, euAngle.z);
});
}
}

Expand Down
80 changes: 11 additions & 69 deletions src/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,19 @@ _Recorder::_Recorder()

}

void _Recorder::recordPropOrTracker(float timestamp, MDagPath path, MVector location, MQuaternion rotation)
void _Recorder::recordPropOrTracker(float timestamp, std::function<void(int)> foo)
{
// create frame object if not exists
if(!recordedData.contains(timestamp)) {
recordedData[timestamp] = FrameData();
}

// put transform for objects
recordedData[timestamp].objects.insert(path.fullPathName().asChar(), { location, rotation });

recordedData[timestamp].append(foo);
}

void _Recorder::recordFace(float timestamp, std::function<void(int)> foo)
{
// create frame object if not exists
if(!recordedData.contains(timestamp)) {
recordedData[timestamp] = FrameData();
}
recordedData[timestamp].append(foo);
}

// put weight data for each blend shape node
recordedData[timestamp].setFaceKeyframDelegates.append(foo);
void _Recorder::recordBone(float timestamp, std::function<void (int)> foo)
{
recordedData[timestamp].append(foo);
}

void _Recorder::finalizeRecording()
Expand All @@ -60,64 +52,14 @@ void _Recorder::finalizeRecording()
index++;
int frame = index;

FrameData fData = recordedData[ts];

// bake props or trackers
if(fData.objects.count() > 0)
{
for(QString path : fData.objects.keys())
{
MSelectionList ls;
ls.add(path.toStdString().c_str(), false);
MDagPath dagPath;
ls.getDagPath(0, dagPath);

auto kfData = fData.objects[path];
keyframeNumericAttribute("tx", frame, dagPath, kfData.first.x);
keyframeNumericAttribute("ty", frame, dagPath, kfData.first.y);
keyframeNumericAttribute("tz", frame, dagPath, kfData.first.z);

MEulerRotation euRot = kfData.second.asEulerRotation();
keyframeNumericAttribute("rx", frame, dagPath, euRot.x);
keyframeNumericAttribute("ry", frame, dagPath, euRot.y);
keyframeNumericAttribute("rz", frame, dagPath, euRot.z);
}
}

// bake faces
for(auto foo : fData.setFaceKeyframDelegates) {
foo(frame);
}
}
auto delegates = recordedData[ts];

recordedData.clear();
}
// bake
for (auto foo : delegates) foo(frame);

int _Recorder::getFrame(const QList<float> &timestamps, int frameNumber)
{
return int(round((timestamps[frameNumber] - timestamps[0]) * RECEIVER_FPS));
}

int _Recorder::getCorrectedFrameNumber(const QList<float> &timestamps, int frameIndex)
{
int currFrame = getFrame(timestamps, frameIndex);
if (0 < frameIndex < timestamps.count() - 1)
{
int prev_frame = getFrame(timestamps, frameIndex - 1);
int next_frame = getFrame(timestamps, frameIndex + 1);
if (prev_frame == currFrame && next_frame == currFrame + 2)
currFrame += 1;
if (next_frame == currFrame && prev_frame == currFrame - 2)
currFrame -= 1;
}

if (frameIndex == timestamps.count() - 1)
{
int prev_frame = getFrame(timestamps, frameIndex - 1);
if (prev_frame == currFrame)
currFrame += 1;
}
return currFrame;
recordedData.clear();
}

void _Recorder::recordingToggled(bool enabled)
Expand Down
26 changes: 4 additions & 22 deletions src/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,17 @@
#include <maya/MFnBlendShapeDeformer.h>



class FrameData {

public:
// objects
// object name - TR
QHash<QString, QPair<MVector, MQuaternion>> objects;

//faces
QList<std::function<void(int)>> setFaceKeyframDelegates;

// characters
//...
};


class _Recorder
{
public:
_Recorder();

void recordPropOrTracker(float timestamp, MDagPath path, MVector location, MQuaternion rotation);
void recordPropOrTracker(float timestamp, std::function<void(int)> foo);
void recordFace(float timestamp, std::function<void(int)> foo);
void recordBone(float timestamp, std::function<void(int)> foo);

void finalizeRecording();

static int getFrame(const QList<float> &timestamps, int frameNumber);

static int getCorrectedFrameNumber(const QList<float> &timestamps, int frameIndex);

void recordingToggled(bool enabled);

void keyframeNumericAttribute(MString attrName, int frame, MDagPath dagPath, double value);
Expand All @@ -57,7 +38,8 @@ class _Recorder

QList<float> sortedTimeStamps();
private:
QHash<float, FrameData> recordedData;
// timestamp - set keyframe function
QHash<float, QList<std::function<void(int)>>> recordedData;
float mRecordingStartTime;
bool isRecording = false;
};
Expand Down

0 comments on commit dc85cbc

Please sign in to comment.