2022-10-31 18:15:50 +08:00
|
|
|
"""
|
|
|
|
This example demonstrates creating DNA from scratch.
|
2023-04-20 21:44:56 +08:00
|
|
|
IMPORTANT: You have to setup the environment before running this example. Please refer to the 'Environment setup' section in README.md.
|
2022-10-31 18:15:50 +08:00
|
|
|
|
2023-04-20 21:44:56 +08:00
|
|
|
- usage in command line:
|
|
|
|
python dna_demo.py
|
|
|
|
mayapy dna_demo.py
|
2022-10-31 18:15:50 +08:00
|
|
|
- usage in Maya:
|
|
|
|
1. copy whole content of this file to Maya Script Editor
|
2023-04-20 21:44:56 +08:00
|
|
|
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:
|
2022-10-31 18:15:50 +08:00
|
|
|
Use `/` (forward slash), because Maya uses forward slashes in path.
|
|
|
|
|
2023-04-20 21:44:56 +08:00
|
|
|
- customization:
|
|
|
|
- change CHARACTER_NAME to Taro, or the name of a custom DNA file placed in /data/dna_files
|
2022-10-31 18:15:50 +08:00
|
|
|
|
2023-04-20 21:44:56 +08:00
|
|
|
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.
|
2022-10-31 18:15:50 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2023-04-20 21:44:56 +08:00
|
|
|
from os import makedirs
|
2022-10-31 18:15:50 +08:00
|
|
|
from os import path as ospath
|
|
|
|
|
2023-06-30 14:11:40 +08:00
|
|
|
# If you use Maya, use absolute path
|
2022-10-31 18:15:50 +08:00
|
|
|
ROOT_DIR = f"{ospath.dirname(ospath.abspath(__file__))}/..".replace("\\", "/")
|
|
|
|
OUTPUT_DIR = f"{ROOT_DIR}/output"
|
|
|
|
|
2023-04-20 21:44:56 +08:00
|
|
|
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"
|
2022-10-31 18:15:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2024-08-15 16:04:44 +08:00
|
|
|
print('Done.')
|
2022-10-31 18:15:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-04-20 21:44:56 +08:00
|
|
|
makedirs(OUTPUT_DIR, exist_ok=True)
|
|
|
|
create_new_dna(OUTPUT_DNA)
|