MotionRes/avatarRes
directory in the SDK package after decompression. Like other animated effect materials, you need to copy them to the assets
directory of your project.
Face customization process | Photo-based face customization process |
| |
XMagicApi
, including the APIs for data loading, avatar customization, and photo-based avatar customization.loadAvatar
)public void loadAvatar(XmagicProperty<?> property, UpdatePropertyListener updatePropertyListener)
loadAvatar
API is equivalent to the updateProperty API.getAvatarConfig
)public static Map<String,List<AvatarData>> getAvatarConfig(String avatarResPath, String savedAvatarConfigs)
/data/data/com.tencent.pitumotiondemo.effects/files/xmagic/MotionRes/avatarRes/animoji_0624
.null
.key
corresponds to category
and value
corresponds to the data of the category. For details, see the AvatarCategory
class. After the application layer gets the map, display the data on the UI as needed.updateAvatar
)public void updateAvatar(List<AvatarData> avatarDataList, UpdateAvatarConfigListener upDataAvatarConfigListener)
AvatarData
object corresponds to a configuration (such as changing the hairstyle). You can pass multiple AvatarData
objects to an API call to edit multiple aspects of an avatar, for example, change the hairstyle and hair color. The API will validate the AvatarData
objects passed in. If an object is valid, it will be passed to the SDK; if not, a callback will be sent.AvatarData
object is passed in to change the hairstyle, but the hair model file (specified by value
in AvatarData
) cannot be found on the local device, the AvatarData
object will be considered invalid.AvatarData
object is passed in to change the iris image, but the image file (specified by value
in AvatarData
) cannot be found on the local device, the AvatarData
object will be considered invalid.exportAvatar
)public static String exportAvatar(List<AvatarData> avatarDataList)
selected
or shape change values in AvatarData
will change. After editing, a new AvatarData
list will be generated and can be exported as a JSON string. You can save it locally or upload it to the server.
The string is exported for the following purposes:loadAvatar
of XMagicApi
to load the avatar materials, you need to set customConfigs
of XMagicProperty
to the JSON string so that the preview will remember the avatar settings from the last time.getAllAvatarData
to update selected
and the shape values in the avatar source data.createAvatarByPhoto
)public void createAvatarByPhoto(String photoPath, List<String> avatarResPaths, boolean isMale,final FaceAttributeListener faceAttributeListener)
void onError(int errCode, String msg)
will be returned; if it succeeds, void onFinish(String matchedResPath, String srcData)
will be returned, in which the first parameter indicates the path of the matched avatar materials, and the second is the matching result (same as the return value of exportAvatar
).onError
are defined in FaceAttributeHelper.java
:public static final int ERROR_NO_AUTH = 1;// Insufficient permission.public static final int ERROR_RES_INVALID = 5;// The avatar material path passed in is invalid.public static final int ERROR_PHOTO_INVALID = 10;// Failed to read the photo.public static final int ERROR_NETWORK_REQUEST_FAILED = 20;// Failed to connect to the internet.public static final int ERROR_DATA_PARSE_FAILED = 30;// Failed to parse the data returned from the network.public static final int ERROR_ANALYZE_FAILED = 40;// Failed to recognize the face.public static final int ERROR_AVATAR_SOURCE_DATA_EMPTY = 50;// Failed to load the avatar source data.
addAvatarResource
)public static Pair<Integer, List<AvatarData>> addAvatarResource(String resourceRootPath, String category, String zipFilePath)
getAvatarConfig
the next time, the SDK will return the newly added data.
Parameters:/data/data/com.tencent.pitumotiondemo.effects/files/xmagic/MotionRes/avatarRes/animoji_0624
.Pair<Integer, List<AvatarData>>
, where pair.first
is the error code, and pair.second
is the newly added data.public interface AvatarActionErrorCode {int OK = 0;int ADD_AVATAR_RES_INVALID_PARAMS = 1000;int ADD_AVATAR_RES_ROOT_RESOURCE_NOT_EXIST = 1001;int ADD_AVATAR_RES_ZIP_FILE_NOT_EXIST = 1002;int ADD_AVATAR_RES_UNZIP_FILE_FAILED = 1003;int ADD_AVATAR_RES_COPY_FILE_FAILED = 1004;}
AvatarData
AvatarData
class is at the core of the above APIs. AvatarData
contains the following fields:public class AvatarData {/*** The selector data. For example, for glasses, different types of glasses are offered, and only one can be selected.*/public static final int TYPE_SELECTOR = 0;/*** The slider data, such as cheek width.*/public static final int TYPE_SLIDER = 1;// The category, such as face shape and eye adjustment. Standard categories are defined in `AvatarCategory.java`. If they cannot meet your requirements, you can customize a category (make sure it’s not the same as an existing one).// This field cannot be empty.public String category;// The ID of an avatar configuration item.// For example, each type of glasses has a unique ID. So does each adjustment item.// This field cannot be empty.public String id;// `TYPE_SELECTOR` or `TYPE_SLIDER`public int type;// If `type` is `TYPE_SELECTOR`, this field indicates whether the item is selected.public boolean selected = false;// Each icon or adjustment item has three aspects of configuration details:public String entityName;public String action;public JsonObject value;}
AvatarData
object is the smallest unit of configuration, for example, hairstyle change or cheek adjustment.Hairstyle change | Cheek adjustment |
| |
selected
in AvatarData
. For example, suppose there are four types of glasses: A, B, C, and D. If the user selects A, set selected
of A to true
and that of B, C, and D to false
. If the user selects B, set selected
of B to true
and that of A, C, and D to false
.value
in AvatarData
. The value
field is a JSON object that includes multiple key-value pairs. Set the value
of a key-value pair to the slider value.getAvatarAnimations
)public static List<AvatarAnimation> getAvatarAnimations(String avatarResPath)
/data/data/com.tencent.pitumotiondemo.effects/files/xmagic/MotionRes/avatarRes/animoji_0624
.AvatarAnimation
class.addAvatarAnimation
)public static Pair<Integer, List<AvatarAnimation>> addAvatarAnimation(String avatarResPath, String zipFilePath)
getAvatarAnimations
the next time, the SDK will return the newly added data.
Parameters:/data/data/com.tencent.pitumotiondemo.effects/files/xmagic/MotionRes/avatarRes/animoji_0624
.Pair<Integer, List<
AvatarAnimation>>
, where pair.first
is the error code, and pair.second
is the newly added data.public interface AvatarActionErrorCode {int OK = 0;int ADD_AVATAR_RES_INVALID_PARAMS = 1000;int ADD_AVATAR_RES_ROOT_RESOURCE_NOT_EXIST = 1001;int ADD_AVATAR_RES_ZIP_FILE_NOT_EXIST = 1002;int ADD_AVATAR_RES_UNZIP_FILE_FAILED = 1003;int ADD_AVATAR_RES_COPY_FILE_FAILED = 1004;int ADD_AVATAR_RES_PARSE_JSON_FILE_FAILED = 1005;}
playAvatarAnimation
)public void playAvatarAnimation(AnimationPlayConfig animationConfig)
public class AnimationPlayConfig {// The action description. It specifies whether to play back or pause the animation.public static final String ACTION_PLAY = "play";public static final String ACTION_PAUSE = "pause";public static final String ACTION_RESUME = "resume";public static final String ACTION_STOP = "stop";public String entityName;/** * The path of the animation folder. It can be the relative path to the material root directory, such as `custom_configs/animations/Waving`./** It can also be the relative path on the mobile phone, such as `/data/data/xxx/xxx/Waving`.*/public String animPath;// The value can be `ACTION_PLAY`, `ACTION_PAUSE`, `ACTION_RESUME`, or `ACTION_STOP`.public String action;/** * The animation name */public String animName;/** * The number of loops. `-1` indicates an infinite loop.*/public int loopCount = -1;/** * The playback start position of the animation in μs */public long startPositionUs = 0;}
AvatarData
AvatarData
are entityName
, action
, and value
, whose values are automatically entered when the SDK parses the configuration data. In most cases, you won’t need to deal with the details of these three fields. However, for slider data, you need to parse the key-value pairs in the value
field and configure them based on the slider values set on the UI.
Here, the AvatarData
element consists of the entityName, action, and value fields.entityName
entityName
specifies which part of an avatar is to be edited, for example, face, eyes, hair, shirt, or shoes.action
and value
action
field indicates the action to be performed on entityName
. Five action
options are defined in AvatarAction.java
in the SDK, which are detailed below:action | Description | Requirements for value |
changeColor | Changes the color of the current material. Color attributes include basic color and emission color. | This field is of JsonObject type and is required. It is generated automatically by the material customization tool. |
changeTexture | Modifies the maps of the current material, including color texture map, metal/roughness texture map, ambient occlusion (AO) map, normal map, and emission map. | This field is of JsonObject type and is required. It is generated automatically by the material customization tool. |
shapeValue | Modifies the shape change value of blendShape . This parameter is generally used for slight adjustment of detailed facial features. | This field is of JsonObject type and is required. Its key is the shape change name and value is of float type. It is generated automatically by the material customization tool. |
replace | Replaces a submodel, for example, glasses, hairstyles, or clothes. | value must be a JSON object that describes the 3D transformation information, model path, and material path of the new submodel. It is generated automatically by the material customization tool. To hide a submodel, set it to null . |
basicTransform | Adjusts the position, rotation, and scaling settings. This field is generally used to adjust the camera distance and angle so as to switch between the full and half-length views of the model. | This field is of JsonObject type and is required. It is generated automatically by the material customization tool. |
resources
folder (in material/custom_configs/resources
):
Was this page helpful?