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 {AttachmentData[]} attachmentData - Information on the avatar's attachments.
71  * <p class="important">Deprecated: This property is deprecated and will be removed. Use avatar entities instead.</p>
72  * @property {string[]} jointNames - The list of joints in the current avatar model. <em>Read-only.</em>
73  * @property {Uuid} sessionUUID - Unique ID of the avatar in the domain. <em>Read-only.</em>
74  * @property {Mat4} sensorToWorldMatrix - The scale, rotation, and translation transform from the user's real world to the
75  * avatar's size, orientation, and position in the virtual world. <em>Read-only.</em>
76  * @property {Mat4} controllerLeftHandMatrix - The rotation and translation of the left hand controller relative to the
77  * avatar. <em>Read-only.</em>
78  * @property {Mat4} controllerRightHandMatrix - The rotation and translation of the right hand controller relative to the
79  * avatar. <em>Read-only.</em>
80  * @property {number} sensorToWorldScale - The scale that transforms dimensions in the user's real world to the avatar's
81  * size in the virtual world. <em>Read-only.</em>
82  * @property {boolean} hasPriority - <code>true</code> if the avatar is in a "hero" zone, <code>false</code> if it isn't.
83  * <em>Read-only.</em>
84  * @property {boolean} hasScriptedBlendshapes=false - <code>true</code> if blend shapes are controlled by scripted actions,
85  * otherwise <code>false</code>. Set this to <code>true</code> before using the {@link MyAvatar.setBlendshape} method,
86  * and set back to <code>false</code> after you no longer want scripted control over the blend shapes.
87  * <p><strong>Note:</strong> This property will automatically be set to true if the Controller system has valid facial
88  * blend shape actions.</p>
89  * @property {boolean} hasProceduralBlinkFaceMovement=true - <code>true</code> if avatars blink automatically by animating
90  * facial blend shapes, <code>false</code> if automatic blinking is disabled. Set to <code>false</code> to fully control
91  * the blink facial blend shapes via the {@link MyAvatar.setBlendshape} method.
92  * @property {boolean} hasProceduralEyeFaceMovement=true - <code>true</code> if the facial blend shapes for an avatar's eyes
93  * adjust automatically as the eyes move, <code>false</code> if this automatic movement is disabled. Set this property
94  * to <code>true</code> to prevent the iris from being obscured by the upper or lower lids. Set to <code>false</code> to
95  * fully control the eye blend shapes via the {@link MyAvatar.setBlendshape} method.
96  * @property {boolean} hasAudioEnabledFaceMovement=true - <code>true</code> if the avatar's mouth blend shapes animate
97  * automatically based on detected microphone input, <code>false</code> if this automatic movement is disabled. Set
98  * this property to <code>false</code> to fully control the mouth facial blend shapes via the
99  * {@link MyAvatar.setBlendshape} method.
100  *
101  * @example <caption>Create a scriptable avatar.</caption>
102  * (function () {
103  * Agent.setIsAvatar(true);
104  * print("Position: " + JSON.stringify(Avatar.position)); // 0, 0, 0
105  * }());
106  */
107 
108 class ScriptableAvatar : public AvatarData, public Dependency {
109  Q_OBJECT
110 
111  using Clock = std::chrono::system_clock;
112  using TimePoint = Clock::time_point;
113 
114 public:
115 
116  ScriptableAvatar();
117 
118  /*@jsdoc
119  * Starts playing an animation on the avatar.
120  * @function Avatar.startAnimation
121  * @param {string} url - The animation file's URL. Animation files need to be in glTF or FBX format but only need to
122  * contain the avatar skeleton and animation data. glTF models may be in JSON or binary format (".gltf" or ".glb" URLs
123  * respectively).
124  * <p><strong>Warning:</strong> glTF animations currently do not always animate correctly.</p>
125  * @param {number} [fps=30] - The frames per second (FPS) rate for the animation playback. 30 FPS is normal speed.
126  * @param {number} [priority=1] - <em>Not used.</em>
127  * @param {boolean} [loop=false] - <code>true</code> if the animation should loop, <code>false</code> if it shouldn't.
128  * @param {boolean} [hold=false] - <em>Not used.</em>
129  * @param {number} [firstFrame=0] - The frame at which the animation starts.
130  * @param {number} [lastFrame=3.403e+38] - The frame at which the animation stops.
131  * @param {string[]} [maskedJoints=[]] - The names of joints that should not be animated.
132  */
134  Q_INVOKABLE void startAnimation(const QString& url, float fps = 30.0f, float priority = 1.0f, bool loop = false,
135  bool hold = false, float firstFrame = 0.0f, float lastFrame = FLT_MAX,
136  const QStringList& maskedJoints = QStringList());
137 
138  /*@jsdoc
139  * Stops playing the current animation.
140  * @function Avatar.stopAnimation
141  */
142  Q_INVOKABLE void stopAnimation();
143 
144  /*@jsdoc
145  * Gets the details of the current avatar animation that is being or was recently played.
146  * @function Avatar.getAnimationDetails
147  * @returns {Avatar.AnimationDetails} The current or recent avatar animation.
148  * @example <caption>Report the current animation details.</caption>
149  * var animationDetails = Avatar.getAnimationDetails();
150  * print("Animation details: " + JSON.stringify(animationDetails));
151  */
152  Q_INVOKABLE AnimationDetails getAnimationDetails();
153 
154  /*@jsdoc
155  * @comment Uses the base class's JSDoc.
156  */
157  Q_INVOKABLE virtual QStringList getJointNames() const override;
158 
159  /*@jsdoc
160  * @comment Uses the base class's JSDoc.
161  */
163  Q_INVOKABLE virtual int getJointIndex(const QString& name) const override;
164 
165  /*@jsdoc
166  * @comment Uses the base class's JSDoc.
167  */
168  Q_INVOKABLE virtual void setSkeletonModelURL(const QUrl& skeletonModelURL) override;
169 
170  /*@jsdoc
171  * @comment Uses the base class's JSDoc.
172  */
173  int sendAvatarDataPacket(bool sendAll = false) override;
174 
175  virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking = false) override;
176 
177  /*@jsdoc
178  * Gets details of all avatar entities.
179  * <p><strong>Warning:</strong> Potentially an expensive call. Do not use if possible.</p>
180  * @function Avatar.getAvatarEntityData
181  * @returns {AvatarEntityMap} Details of all avatar entities.
182  * @example <caption>Report the current avatar entities.</caption>
183  * var avatarEntityData = Avatar.getAvatarEntityData();
184  * print("Avatar entities: " + JSON.stringify(avatarEntityData));
185  */
186  Q_INVOKABLE AvatarEntityMap getAvatarEntityData() const override;
187 
188  AvatarEntityMap getAvatarEntityDataNonDefault() const override;
189 
190  AvatarEntityMap getAvatarEntityDataInternal(bool allProperties) const;
191 
192  /*@jsdoc
193  * Sets all avatar entities from an object.
194  * <p><strong>Warning:</strong> Potentially an expensive call. Do not use if possible.</p>
195  * @function Avatar.setAvatarEntityData
196  * @param {AvatarEntityMap} avatarEntityData - Details of the avatar entities.
197  */
198  Q_INVOKABLE void setAvatarEntityData(const AvatarEntityMap& avatarEntityData) override;
199 
200  /*@jsdoc
201  * @comment Uses the base class's JSDoc.
202  */
203  Q_INVOKABLE void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData) override;
204 
205 public slots:
206  /*@jsdoc
207  * @function Avatar.update
208  * @param {number} deltaTime - Delta time.
209  * @deprecated This function is deprecated and will be removed.
210  */
211  void update(float deltatime);
212 
213  /*@jsdoc
214  * @function Avatar.setJointMappingsFromNetworkReply
215  * @deprecated This function is deprecated and will be removed.
216  */
217  void setJointMappingsFromNetworkReply();
218 
219 private:
220  AnimationPointer _animation;
221  AnimationDetails _animationDetails;
222  QStringList _maskedJoints;
223  GeometryResource::Pointer _geometryResource;
224  Rig _rig;
225  bool _isRigValid{false};
226  Rig _animationRig;
227  bool _isAnimationRigValid{false};
228  std::shared_ptr<AnimSkeleton> _avatarAnimSkeleton;
229  QHash<QString, int> _fstJointIndices;
230  QStringList _fstJointNames;
231  QUrl _skeletonModelFilenameURL; // This contains URL from filename field in fst file
232  mutable HelperScriptEngine _helperScriptEngine;
233  std::map<QUuid, EntityItemPointer> _entities;
234 
236  void updateJointMappings();
237 
238  quint64 _lastSendAvatarDataTime { 0 };
239 
240  TimePoint _nextTraitsSendWindow;
241 };
242 
243 #endif // hifi_ScriptableAvatar_h
Provides a wrapper around script engine that does not have ScriptManager.
Definition: HelperScriptEngine.h:31