-
Notifications
You must be signed in to change notification settings - Fork 120
/
dna_demo.py
101 lines (76 loc) · 3.53 KB
/
dna_demo.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
"""
This example demonstrates creating DNA from scratch.
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 dna_demo.py
mayapy dna_demo.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
def create_dna(path):
stream = FileStream(path, FileStream.AccessMode_Write, FileStream.OpenMode_Binary)
writer = BinaryStreamWriter(stream)
# Sets a couple of parameters about in the dna that is about to be created and written to
writer.setName("rig name")
writer.setLODCount(4)
writer.setJointName(0, "spine")
writer.setJointName(1, "neck")
writer.setMeshName(0, "head")
writer.setVertexPositions(0, [[0.0, 0.5, 0.3], [1.0, 3.0, -8.0]])
writer.setVertexTextureCoordinates(0, [[0.25, 0.55], [1.5, 3.6]])
# Creates the DNA
writer.write()
if not Status.isOk():
status = Status.get()
raise RuntimeError(f"Error saving DNA: {status.message}")
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 print_dna_summary(dna_reader):
print(f"Name: {dna_reader.getName()}")
print(f"Joint count: {dna_reader.getJointCount()}")
joint_names = ", ".join(
dna_reader.getJointName(i) for i in range(dna_reader.getJointCount())
)
print(f"Joint names: {joint_names}")
for mesh_idx in range(dna_reader.getMeshCount()):
# Get vertices one by one
for vtx_id in range(dna_reader.getVertexPositionCount(mesh_idx)):
vtx = dna_reader.getVertexPosition(mesh_idx, vtx_id)
print(f"Mesh {mesh_idx} - Vertex {vtx_id} : {vtx}")
# Get all X / Y / Z coordinates
print(dna_reader.getVertexPositionXs(mesh_idx))
print(dna_reader.getVertexPositionYs(mesh_idx))
print(dna_reader.getVertexPositionZs(mesh_idx))
for tc_idx in range(dna_reader.getVertexTextureCoordinateCount(mesh_idx)):
tex_coord = dna_reader.getVertexTextureCoordinate(mesh_idx, tc_idx)
print(f"Mesh {mesh_idx} - Texture coordinate {tc_idx} : {tex_coord}")
def create_new_dna(dna_path):
create_dna(dna_path)
dna_reader = load_dna(dna_path)
print_dna_summary(dna_reader)
print('Done.')
if __name__ == "__main__":
makedirs(OUTPUT_DIR, exist_ok=True)
create_new_dna(OUTPUT_DNA)