Overte C++ Documentation
ScriptableAvatar.h
1 //
2 // ScriptableAvatar.h
3 // assignment-client/src/avatars
4 //
5 // Created by Clement on 7/22/14.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_ScriptableAvatar_h
15 #define hifi_ScriptableAvatar_h
16 
17 #include <AnimationCache.h>
18 #include <AnimSkeleton.h>
19 #include <AvatarData.h>
20 #include <ScriptEngine.h>
21 #include <EntityItem.h>
22 #include "model-networking/ModelCache.h"
23 #include "Rig.h"
24 #include <HelperScriptEngine.h>
25 
26 /*@jsdoc
27  * The <code>Avatar</code> API is used to manipulate scriptable avatars on the domain. This API is a subset of the
28  * {@link MyAvatar} API. To enable this API, set {@link Agent|Agent.isAvatar} to <code>true</code>.
29  *
30  * <p>For Interface, client entity, and avatar scripts, see {@link MyAvatar}.</p>
31  *
32  * @namespace Avatar
33  *
34  * @hifi-assignment-client
35  *
36  * @comment IMPORTANT: This group of properties is copied from AvatarData.h; they should NOT be edited here.
37  * @property {Vec3} position - The position of the avatar.
38  * @property {number} scale=1.0 - The scale of the avatar. The value can be set to anything between <code>0.005</code> and
39  * <code>1000.0</code>. When the scale value is fetched, it may temporarily be further limited by the domain's settings.
40  * @property {number} density - The density of the avatar in kg/m<sup>3</sup>. The density is used to work out its mass in
41  * the application of physics. <em>Read-only.</em>
42  * @property {Vec3} handPosition - A user-defined hand position, in world coordinates. The position moves with the avatar
43  * but is otherwise not used or changed by Interface.
44  * @property {number} bodyYaw - The left or right rotation about an axis running from the head to the feet of the avatar.
45  * Yaw is sometimes called "heading".
46  * @property {number} bodyPitch - The rotation about an axis running from shoulder to shoulder of the avatar. Pitch is
47  * sometimes called "elevation".
48  * @property {number} bodyRoll - The rotation about an axis running from the chest to the back of the avatar. Roll is
49  * sometimes called "bank".
50  * @property {Quat} orientation - The orientation of the avatar.
51  * @property {Quat} headOrientation - The orientation of the avatar's head.
52  * @property {number} headPitch - The rotation about an axis running from ear to ear of the avatar's head. Pitch is
53  * sometimes called "elevation".
54  * @property {number} headYaw - The rotation left or right about an axis running from the base to the crown of the avatar's
55  * head. Yaw is sometimes called "heading".
56  * @property {number} headRoll - The rotation about an axis running from the nose to the back of the avatar's head. Roll is
57  * sometimes called "bank".
58  * @property {Vec3} velocity - The current velocity of the avatar.
59  * @property {Vec3} angularVelocity - The current angular velocity of the avatar.
60  * @property {number} audioLoudness - The instantaneous loudness of the audio input that the avatar is injecting into the
61  * domain.
62  * @property {number} audioAverageLoudness - The rolling average loudness of the audio input that the avatar is injecting
63  * into the domain.
64  * @property {string} displayName - The avatar's display name.
65  * @property {string} sessionDisplayName - <code>displayName's</code> sanitized and default version defined by the avatar mixer
66  * rather than Interface clients. The result is unique among all avatars present in the domain at the time.
67  * @property {boolean} lookAtSnappingEnabled=true - <code>true</code> if the avatar's eyes snap to look at another avatar's
68  * eyes when the other avatar is in the line of sight and also has <code>lookAtSnappingEnabled == true</code>.
69  * @property {string} skeletonModelURL - The avatar's FST file.
70  * @property {string[]} jointNames - The list of joints in the current avatar model. <em>Read-only.</em>
71  * @property {Uuid} sessionUUID - Unique ID of the avatar in the domain. <em>Read-only.</em>
72  * @property {Mat4} sensorToWorldMatrix - The scale, rotation, and translation transform from the user's real world to the
73  * avatar's size, orientation, and position in the virtual world. <em>Read-only.</em>
74  * @property {Mat4} controllerLeftHandMatrix - The rotation and translation of the left hand controller relative to the
75  * avatar. <em>Read-only.</em>
76  * @property {Mat4} controllerRightHandMatrix - The rotation and translation of the right hand controller relative to the
77  * avatar. <em>Read-only.</em>
78  * @property {number} sensorToWorldScale - The scale that transforms dimensions in the user's real world to the avatar's
79  * size in the virtual world. <em>Read-only.</em>
80  * @property {boolean} hasPriority - <code>true</code> if the avatar is in a "hero" zone, <code>false</code> if it isn't.
81  * <em>Read-only.</em>
82  * @property {boolean} hasScriptedBlendshapes=false - <code>true</code> if blend shapes are controlled by scripted actions,
83  * otherwise <code>false</code>. Set this to <code>true</code> before using the {@link MyAvatar.setBlendshape} method,
84  * and set back to <code>false</code> after you no longer want scripted control over the blend shapes.
85  * <p><strong>Note:</strong> This property will automatically be set to true if the Controller system has valid facial
86  * blend shape actions.</p>
87  * @property {boolean} hasProceduralBlinkFaceMovement=true - <code>true</code> if avatars blink automatically by animating
88  * facial blend shapes, <code>false</code> if automatic blinking is disabled. Set to <code>false</code> to fully control
89  * the blink facial blend shapes via the {@link MyAvatar.setBlendshape} method.
90  * @property {boolean} hasProceduralEyeFaceMovement=true - <code>true</code> if the facial blend shapes for an avatar's eyes
91  * adjust automatically as the eyes move, <code>false</code> if this automatic movement is disabled. Set this property
92  * to <code>true</code> to prevent the iris from being obscured by the upper or lower lids. Set to <code>false</code> to
93  * fully control the eye blend shapes via the {@link MyAvatar.setBlendshape} method.
94  * @property {boolean} hasAudioEnabledFaceMovement=true - <code>true</code> if the avatar's mouth blend shapes animate
95  * automatically based on detected microphone input, <code>false</code> if this automatic movement is disabled. Set
96  * this property to <code>false</code> to fully control the mouth facial blend shapes via the
97  * {@link MyAvatar.setBlendshape} method.
98  *
99  * @example <caption>Create a scriptable avatar.</caption>
100  * (function () {
101  * Agent.setIsAvatar(true);
102  * print("Position: " + JSON.stringify(Avatar.position)); // 0, 0, 0
103  * }());
104  */
105 
106 class ScriptableAvatar : public AvatarData, public Dependency {
107  Q_OBJECT
108 
109  using Clock = std::chrono::system_clock;
110  using TimePoint = Clock::time_point;
111 
112 public:
113 
114  ScriptableAvatar();
115 
116  /*@jsdoc
117  * Starts playing an animation on the avatar.
118  * @function Avatar.startAnimation
119  * @param {string} url - The animation file's URL. Animation files need to be in glTF or FBX format but only need to
120  * contain the avatar skeleton and animation data. glTF models may be in JSON or binary format (".gltf" or ".glb" URLs
121  * respectively).
122  * <p><strong>Warning:</strong> glTF animations currently do not always animate correctly.</p>
123  * @param {number} [fps=30] - The frames per second (FPS) rate for the animation playback. 30 FPS is normal speed.
124  * @param {number} [priority=1] - <em>Not used.</em>
125  * @param {boolean} [loop=false] - <code>true</code> if the animation should loop, <code>false</code> if it shouldn't.
126  * @param {boolean} [hold=false] - <em>Not used.</em>
127  * @param {number} [firstFrame=0] - The frame at which the animation starts.
128  * @param {number} [lastFrame=3.403e+38] - The frame at which the animation stops.
129  * @param {string[]} [maskedJoints=[]] - The names of joints that should not be animated.
130  */
132  Q_INVOKABLE void startAnimation(const QString& url, float fps = 30.0f, float priority = 1.0f, bool loop = false,
133  bool hold = false, float firstFrame = 0.0f, float lastFrame = FLT_MAX,
134  const QStringList& maskedJoints = QStringList());
135 
136  /*@jsdoc
137  * Stops playing the current animation.
138  * @function Avatar.stopAnimation
139  */
140  Q_INVOKABLE void stopAnimation();
141 
142  /*@jsdoc
143  * Gets the details of the current avatar animation that is being or was recently played.
144  * @function Avatar.getAnimationDetails
145  * @returns {Avatar.AnimationDetails} The current or recent avatar animation.
146  * @example <caption>Report the current animation details.</caption>
147  * var animationDetails = Avatar.getAnimationDetails();
148  * print("Animation details: " + JSON.stringify(animationDetails));
149  */
150  Q_INVOKABLE AnimationDetails getAnimationDetails();
151 
152  /*@jsdoc
153  * @comment Uses the base class's JSDoc.
154  */
155  Q_INVOKABLE virtual QStringList getJointNames() const override;
156 
157  /*@jsdoc
158  * @comment Uses the base class's JSDoc.
159  */
161  Q_INVOKABLE virtual int getJointIndex(const QString& name) const override;
162 
163  /*@jsdoc
164  * @comment Uses the base class's JSDoc.
165  */
166  Q_INVOKABLE virtual void setSkeletonModelURL(const QUrl& skeletonModelURL) override;
167 
168  /*@jsdoc
169  * @comment Uses the base class's JSDoc.
170  */
171  int sendAvatarDataPacket(bool sendAll = false) override;
172 
173  virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking = false) override;
174 
175  /*@jsdoc
176  * Gets details of all avatar entities.
177  * <p><strong>Warning:</strong> Potentially an expensive call. Do not use if possible.</p>
178  * @function Avatar.getAvatarEntityData
179  * @returns {AvatarEntityMap} Details of all avatar entities.
180  * @example <caption>Report the current avatar entities.</caption>
181  * var avatarEntityData = Avatar.getAvatarEntityData();
182  * print("Avatar entities: " + JSON.stringify(avatarEntityData));
183  */
184  Q_INVOKABLE AvatarEntityMap getAvatarEntityData() const override;
185 
186  AvatarEntityMap getAvatarEntityDataNonDefault() const override;
187 
188  AvatarEntityMap getAvatarEntityDataInternal(bool allProperties) const;
189 
190  /*@jsdoc
191  * Sets all avatar entities from an object.
192  * <p><strong>Warning:</strong> Potentially an expensive call. Do not use if possible.</p>
193  * @function Avatar.setAvatarEntityData
194  * @param {AvatarEntityMap} avatarEntityData - Details of the avatar entities.
195  */
196  Q_INVOKABLE void setAvatarEntityData(const AvatarEntityMap& avatarEntityData) override;
197 
198  /*@jsdoc
199  * @comment Uses the base class's JSDoc.
200  */
201  Q_INVOKABLE void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) override;
202 
203 public slots:
204  /*@jsdoc
205  * @function Avatar.update
206  * @param {number} deltaTime - Delta time.
207  * @deprecated This function is deprecated and will be removed.
208  */
209  void update(float deltatime);
210 
211  /*@jsdoc
212  * @function Avatar.setJointMappingsFromNetworkReply
213  * @deprecated This function is deprecated and will be removed.
214  */
215  void setJointMappingsFromNetworkReply();
216 
217 private:
218  AnimationPointer _animation;
219  AnimationDetails _animationDetails;
220  QStringList _maskedJoints;
221  GeometryResource::Pointer _geometryResource;
222  Rig _rig;
223  bool _isRigValid{false};
224  Rig _animationRig;
225  bool _isAnimationRigValid{false};
226  std::shared_ptr<AnimSkeleton> _avatarAnimSkeleton;
227  QHash<QString, int> _fstJointIndices;
228  QStringList _fstJointNames;
229  QUrl _skeletonModelFilenameURL; // This contains URL from filename field in fst file
230  mutable HelperScriptEngine _helperScriptEngine;
231  std::map<QUuid, EntityItemPointer> _entities;
232 
234  void updateJointMappings();
235 
236  quint64 _lastSendAvatarDataTime { 0 };
237 
238  TimePoint _nextTraitsSendWindow;
239 };
240 
241 #endif // hifi_ScriptableAvatar_h
Provides a wrapper around script engine that does not have ScriptManager.
Definition: HelperScriptEngine.h:31