Metahuman_DNA_Calibration/examples/dna_demo.py
2022-11-29 14:25:24 +01:00

141 lines
5.0 KiB
Python

"""
This example demonstrates creating DNA from scratch.
- usage in command line:
- call without arguments:
python dna_demo.py
mayapy dna_demo.py
Expected: Script will generate CustomDNA.dna in OUTPUT_DIR.
NOTE: If OUTPUT_DIR does not exist, it will be created.
- call with arguments:
python dna_demo.py --dna_path=<PATH TO NEW DNA FILE>
mayapy dna_demo.py --dna_path=<PATH TO NEW DNA FILE>
Expected: script will generate <PATH TO NEW DNA FILE>.
NOTE: The directory referenced by the given path must exist. If the directory does not exist, the script is going to fail.
- usage in Maya:
1. copy whole content of this file to Maya Script Editor
2. delete "if __name__ == "__main__":
main()"
3. delete whole "def main" method
4. 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.
5. call method create_new_dna(<PATH TO NEW DNA FILE>)
Expected: script will generate <PATH TO NEW DNA FILE>.
NOTE: The directory referenced by the given path must exist. If the directory does not exist, the script is going to fail.
NOTE: If running on Linux, please make sure to append the LD_LIBRARY_PATH with absolute path to the lib/linux directory before running the example:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path-to-lib-linux-dir>
"""
import argparse
from os import environ, makedirs
from os import path as ospath
from sys import path as syspath
from sys import platform
# if you use Maya, use absolute path
ROOT_DIR = f"{ospath.dirname(ospath.abspath(__file__))}/..".replace("\\", "/")
OUTPUT_DIR = f"{ROOT_DIR}/output"
ROOT_LIB_DIR = f"{ROOT_DIR}/lib"
if platform == "win32":
LIB_DIR = f"{ROOT_LIB_DIR}/windows"
elif platform == "linux":
LIB_DIR = f"{ROOT_LIB_DIR}/linux"
else:
raise OSError(
"OS not supported, please compile dependencies and add value to LIB_DIR"
)
# Add bin directory to maya plugin path
if "MAYA_PLUG_IN_PATH" in environ:
separator = ":" if platform == "linux" else ";"
environ["MAYA_PLUG_IN_PATH"] = separator.join([environ["MAYA_PLUG_IN_PATH"], LIB_DIR])
else:
environ["MAYA_PLUG_IN_PATH"] = LIB_DIR
# Adds directories to path
syspath.insert(0, ROOT_DIR)
syspath.insert(0, LIB_DIR)
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)
def main():
parser = argparse.ArgumentParser(description="DNA demo")
parser.add_argument(
"--dna_path", metavar="--dna_path", help="Path where to save the DNA file", default=f"{OUTPUT_DIR}/CustomDNA.dna"
)
makedirs(OUTPUT_DIR, exist_ok=True)
args = parser.parse_args()
create_new_dna(args.dna_path)
if __name__ == "__main__":
main()