Metahuman_DNA_Calibration/examples/dnacalib_rename_joint_demo.py
2023-04-21 11:52:01 +02:00

74 lines
2.5 KiB
Python

"""
This example demonstrates rename of a joint.
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 rename_joint_demo.py
mayapy rename_joint_demo.py
Expected: Script will generate Ada_output.dna in OUTPUT_DIR.
- 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, RenameJointCommand
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}")
if __name__ == "__main__":
makedirs(OUTPUT_DIR, exist_ok=True)
dna_reader = load_dna(CHARACTER_DNA)
calibrated = DNACalibDNAReader(dna_reader)
# Prints current joint name
print(calibrated.getJointName(10))
# Creates rename command
rename = RenameJointCommand(10, "NewJointA")
# Executes command
rename.run(calibrated)
# Prints the new joint name
print(calibrated.getJointName(10))
save_dna(calibrated, OUTPUT_DNA)