-
Notifications
You must be signed in to change notification settings - Fork 131
/
dnacalib_clear_blend_shapes.py
144 lines (106 loc) · 5.75 KB
/
dnacalib_clear_blend_shapes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
This example demonstrates how to remove all blend shape data from a DNA.
IMPORTANT: You have to setup the environment before running this example. Please refer to the 'Environment setup' section in README.md.
- usage in command line:
python dnacalib_clear_blend_shapes.py
mayapy dnacalib_clear_blend_shapes.py
- usage in Maya:
1. copy whole content of this file to Maya Script Editor
2. change value of ROOT_DIR to absolute path of dna_calibration, e.g. `c:/dna_calibration` in Windows or `/home/user/dna_calibration`. Important:
Use `/` (forward slash), because Maya uses forward slashes in path.
- customization:
- change CHARACTER_NAME to Taro, or the name of a custom DNA file placed in /data/dna_files
Expected: Script will generate Ada_output.dna in OUTPUT_DIR from original Ada.dna.
NOTE: If OUTPUT_DIR does not exist, it will be created.
"""
from os import makedirs
from os import path as ospath
# if you use Maya, use absolute path
ROOT_DIR = f"{ospath.dirname(ospath.abspath(__file__))}/..".replace("\\", "/")
OUTPUT_DIR = f"{ROOT_DIR}/output"
CHARACTER_NAME = "Ada"
DATA_DIR = f"{ROOT_DIR}/data"
CHARACTER_DNA = f"{DATA_DIR}/dna_files/{CHARACTER_NAME}.dna"
OUTPUT_DNA = f"{OUTPUT_DIR}/{CHARACTER_NAME}_output.dna"
from dna import DataLayer_All, FileStream, Status, BinaryStreamReader, BinaryStreamWriter
from dnacalib import (
DNACalibDNAReader,
ClearBlendShapesCommand
)
def load_dna(path):
stream = FileStream(path, FileStream.AccessMode_Read, FileStream.OpenMode_Binary)
reader = BinaryStreamReader(stream, DataLayer_All)
reader.read()
if not Status.isOk():
status = Status.get()
raise RuntimeError(f"Error loading DNA: {status.message}")
return reader
def save_dna(reader, path):
stream = FileStream(path, FileStream.AccessMode_Write, FileStream.OpenMode_Binary)
writer = BinaryStreamWriter(stream)
writer.setFrom(reader)
writer.write()
if not Status.isOk():
status = Status.get()
raise RuntimeError(f"Error saving DNA: {status.message}")
def validate_geometry(dna):
mesh_count = dna.getMeshCount()
for mesh_index in range(mesh_count):
bs_tgt_count = dna.getBlendShapeTargetCount(mesh_index)
for bs_tgt_index in range(bs_tgt_count):
bs_tgt_delta_count = dna.getBlendShapeTargetDeltaCount(mesh_index, bs_tgt_index)
if bs_tgt_delta_count != 0:
raise RuntimeError("Blend shape target deltas not removed properly!")
def validate_animation_data(dna):
bs_channel_lods = dna.getBlendShapeChannelLODs()
bs_channel_input_indices = dna.getBlendShapeChannelInputIndices()
bs_channel_output_indices = dna.getBlendShapeChannelOutputIndices()
if len(bs_channel_lods) != dna.getLODCount():
raise RuntimeError("Blend shape animation data not removed properly! Number of blend shape LODs does not match LOD count!")
for lod in bs_channel_lods:
if lod != 0:
raise RuntimeError("Blend shape animation data not removed properly!")
if (len(bs_channel_input_indices) != 0) or (len(bs_channel_output_indices) != 0):
raise RuntimeError("Blend shape animation data not removed properly!")
def calibrate_dna(input_path, output_path):
dna = load_dna(input_path)
# Copies DNA contents and will serve as input/output parameter to commands
calibrated = DNACalibDNAReader(dna)
mesh_count = calibrated.getMeshCount()
print(f"Number of meshes: {mesh_count}")
for mesh_index in range(mesh_count):
bs_tgt_count = calibrated.getBlendShapeTargetCount(mesh_index)
print(f"Number of blendshape targets for mesh {calibrated.getMeshName(mesh_index)}({mesh_index}): {bs_tgt_count}")
for bs_tgt_index in range(bs_tgt_count):
bs_tgt_delta_count = calibrated.getBlendShapeTargetDeltaCount(mesh_index, bs_tgt_index)
print(f"Number of blendshape target deltas for mesh {calibrated.getMeshName(mesh_index)}({mesh_index}), blend shape target {bs_tgt_index}: {bs_tgt_delta_count}")
print(f"Blend shape channel LODs: {calibrated.getBlendShapeChannelLODs()}")
print(f"Blend shape channel input indices: {calibrated.getBlendShapeChannelInputIndices()}")
print(f"Blend shape channel output indices: {calibrated.getBlendShapeChannelOutputIndices()}")
# Clears all blend shapes
command = ClearBlendShapesCommand()
print("\n\nClearing blend shape data...\n\n")
# Modifies calibrated DNA in-place
command.run(calibrated)
validate_geometry(calibrated)
validate_animation_data(calibrated)
print(f"Number of meshes: {mesh_count}")
for mesh_index in range(mesh_count):
bs_tgt_count = calibrated.getBlendShapeTargetCount(mesh_index)
print(f"Number of blendshape targets for mesh {calibrated.getMeshName(mesh_index)}({mesh_index}): {bs_tgt_count}")
for bs_tgt_index in range(bs_tgt_count):
bs_tgt_delta_count = calibrated.getBlendShapeTargetDeltaCount(mesh_index, bs_tgt_index)
print(f"Number of blendshape target deltas for mesh {calibrated.getMeshName(mesh_index)}({mesh_index}), blend shape target {bs_tgt_index}: {bs_tgt_delta_count}")
bs_channel_lods = dna.getBlendShapeChannelLODs()
bs_channel_input_indices = dna.getBlendShapeChannelInputIndices()
bs_channel_output_indices = dna.getBlendShapeChannelOutputIndices()
print(f"Blend shape channel LODs: {bs_channel_lods}")
print(f"Blend shape channel input indices: {bs_channel_input_indices}")
print(f"Blend shape channel output indices: {bs_channel_output_indices}")
print("\n\nSuccessfully cleared blend shape data.")
print("Saving DNA...")
save_dna(calibrated, output_path)
print("Done.")
if __name__ == "__main__":
makedirs(OUTPUT_DIR, exist_ok=True)
calibrate_dna(CHARACTER_DNA, OUTPUT_DNA)