From 16144b663036cbce530f77091a4ab8217f22450f Mon Sep 17 00:00:00 2001 From: Simon Walton Date: Wed, 5 Dec 2018 17:14:40 -0800 Subject: [PATCH] Move the previously deleted FST reader down to the ScriptableAvatar class --- .../src/avatars/ScriptableAvatar.cpp | 96 +++++++++++++++++++ .../src/avatars/ScriptableAvatar.h | 32 +++++++ 2 files changed, 128 insertions(+) diff --git a/assignment-client/src/avatars/ScriptableAvatar.cpp b/assignment-client/src/avatars/ScriptableAvatar.cpp index 392e9960e0..29a66b44fc 100644 --- a/assignment-client/src/avatars/ScriptableAvatar.cpp +++ b/assignment-client/src/avatars/ScriptableAvatar.cpp @@ -19,6 +19,9 @@ #include #include #include +#include +#include + ScriptableAvatar::ScriptableAvatar() { _clientTraitsHandler = std::unique_ptr(new ClientTraitsHandler(this)); @@ -62,11 +65,28 @@ AnimationDetails ScriptableAvatar::getAnimationDetails() { return _animationDetails; } +int ScriptableAvatar::getJointIndex(const QString& name) const { + // Faux joints: + int result = AvatarData::getJointIndex(name); + if (result != -1) { + return result; + } + QReadLocker readLock(&_jointDataLock); + return _fstJointIndices.value(name) - 1; +} + +QStringList ScriptableAvatar::getJointNames() const { + QReadLocker readLock(&_jointDataLock); + return _fstJointNames; + return QStringList(); +} + void ScriptableAvatar::setSkeletonModelURL(const QUrl& skeletonModelURL) { _bind.reset(); _animSkeleton.reset(); AvatarData::setSkeletonModelURL(skeletonModelURL); + updateJointMappings(); } static AnimPose composeAnimPose(const HFMJoint& joint, const glm::quat rotation, const glm::vec3 translation) { @@ -142,6 +162,82 @@ void ScriptableAvatar::update(float deltatime) { _clientTraitsHandler->sendChangedTraitsToMixer(); } +void ScriptableAvatar::updateJointMappings() { + { + QWriteLocker writeLock(&_jointDataLock); + _fstJointIndices.clear(); + _fstJointNames.clear(); + _jointData.clear(); + } + + if (_skeletonModelURL.fileName().toLower().endsWith(".fst")) { + //// + // TODO: Should we rely upon HTTPResourceRequest for ResourceRequestObserver instead? + // HTTPResourceRequest::doSend() covers all of the following and + // then some. It doesn't cover the connect() call, so we may + // want to add a HTTPResourceRequest::doSend() method that does + // connects. + QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance(); + QNetworkRequest networkRequest = QNetworkRequest(_skeletonModelURL); + networkRequest.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true); + networkRequest.setHeader(QNetworkRequest::UserAgentHeader, HIGH_FIDELITY_USER_AGENT); + DependencyManager::get()->update( + _skeletonModelURL, -1, "AvatarData::updateJointMappings"); + QNetworkReply* networkReply = networkAccessManager.get(networkRequest); + // + //// + connect(networkReply, &QNetworkReply::finished, this, &ScriptableAvatar::setJointMappingsFromNetworkReply); + } +} + +void ScriptableAvatar::setJointMappingsFromNetworkReply() { + QNetworkReply* networkReply = static_cast(sender()); + // before we process this update, make sure that the skeleton model URL hasn't changed + // since we made the FST request + if (networkReply->url() != _skeletonModelURL) { + qCDebug(avatars) << "Refusing to set joint mappings for FST URL that does not match the current URL"; + networkReply->deleteLater(); + return; + } + { + QWriteLocker writeLock(&_jointDataLock); + QByteArray line; + while (!(line = networkReply->readLine()).isEmpty()) { + line = line.trimmed(); + if (line.startsWith("filename")) { + int filenameIndex = line.indexOf('=') + 1; + if (filenameIndex > 0) { + _skeletonFBXURL = _skeletonModelURL.resolved(QString(line.mid(filenameIndex).trimmed())); + } + } + if (!line.startsWith("jointIndex")) { + continue; + } + int jointNameIndex = line.indexOf('=') + 1; + if (jointNameIndex == 0) { + continue; + } + int secondSeparatorIndex = line.indexOf('=', jointNameIndex); + if (secondSeparatorIndex == -1) { + continue; + } + QString jointName = line.mid(jointNameIndex, secondSeparatorIndex - jointNameIndex).trimmed(); + bool ok; + int jointIndex = line.mid(secondSeparatorIndex + 1).trimmed().toInt(&ok); + if (ok) { + while (_fstJointNames.size() < jointIndex + 1) { + _fstJointNames.append(QString()); + } + _fstJointNames[jointIndex] = jointName; + } + } + for (int i = 0; i < _fstJointNames.size(); i++) { + _fstJointIndices.insert(_fstJointNames.at(i), i + 1); + } + } + networkReply->deleteLater(); +} + void ScriptableAvatar::setHasProceduralBlinkFaceMovement(bool hasProceduralBlinkFaceMovement) { _headData->setHasProceduralBlinkFaceMovement(hasProceduralBlinkFaceMovement); } diff --git a/assignment-client/src/avatars/ScriptableAvatar.h b/assignment-client/src/avatars/ScriptableAvatar.h index 578bd84a8f..66b0b5ae3f 100644 --- a/assignment-client/src/avatars/ScriptableAvatar.h +++ b/assignment-client/src/avatars/ScriptableAvatar.h @@ -153,6 +153,27 @@ public: */ Q_INVOKABLE AnimationDetails getAnimationDetails(); + /**jsdoc + * Get the names of all the joints in the current avatar. + * @function MyAvatar.getJointNames + * @returns {string[]} The joint names. + * @example Report the names of all the joints in your current avatar. + * print(JSON.stringify(MyAvatar.getJointNames())); + */ + Q_INVOKABLE virtual QStringList getJointNames() const override; + + /**jsdoc + * Get the joint index for a named joint. The joint index value is the position of the joint in the array returned by + * {@link MyAvatar.getJointNames} or {@link Avatar.getJointNames}. + * @function MyAvatar.getJointIndex + * @param {string} name - The name of the joint. + * @returns {number} The index of the joint. + * @example Report the index of your avatar's left arm joint. + * print(JSON.stringify(MyAvatar.getJointIndex("LeftArm")); + */ + /// Returns the index of the joint with the specified name, or -1 if not found/unknown. + Q_INVOKABLE virtual int getJointIndex(const QString& name) const override; + virtual void setSkeletonModelURL(const QUrl& skeletonModelURL) override; virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking = false) override; @@ -167,12 +188,23 @@ public: public slots: void update(float deltatime); + /**jsdoc + * @function MyAvatar.setJointMappingsFromNetworkReply + */ + void setJointMappingsFromNetworkReply(); + private: AnimationPointer _animation; AnimationDetails _animationDetails; QStringList _maskedJoints; AnimationPointer _bind; // a sleazy way to get the skeleton, given the various library/cmake dependencies std::shared_ptr _animSkeleton; + QHash _fstJointIndices; ///< 1-based, since zero is returned for missing keys + QStringList _fstJointNames; ///< in order of depth-first traversal + QUrl _skeletonFBXURL; + + /// Loads the joint indices, names from the FST file (if any) + void updateJointMappings(); }; #endif // hifi_ScriptableAvatar_h