---
# System prepended metadata

title: 上傳 Avalon 工作檔至 NCHC 算圖

---

上傳 Avalon 工作檔至 NCHC 算圖
===

```python
"""
Upload scene file and loaded representation to NCHC

File will be placed at Avalon Work dir

"""
import os
import re
import getpass
import subprocess
from maya import cmds
from avalon import api
from reveries.maya import capsule


def process(inputs):
    popen = subprocess.Popen(inputs,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             shell=True)

    output, _ = popen.communicate()
    print(output)


def is_versioned_path(path):
    pattern = (
        ".*[/\\\]publish"  # publish root
        "[/\\\]texture.*"  # subset dir
        "[/\\\]v[0-9]{3}"  # version dir
        "[/\\\]TexturePack"  # representation dir
    )
    return bool(re.match(pattern, path))


project_name = api.Session["AVALON_PROJECT"]


# Upload loaded representations
host = api.registered_host()
processed = set()

for container in host.ls():
    container_node = container["objectName"]
    id = container["representation"]

    if id not in processed:
        process(["upload_repr_to_nchc", id])
        processed.add(id)

print("\nAll dependnies uploaded.\n")

# Upload stray files...
stray = ["upload_files_to_nchc"]

for file_node in cmds.ls(type="file"):
    file_path = cmds.getAttr(file_node + ".fileTextureName")
    if not is_versioned_path(file_path):
        if not any(file_path.startswith(root) for root in ["O:", "P:", "Q:"]):
            continue
        if project_name not in file_path:
            continue
        stray.append(file_path)

process(stray)

print("\nAll stray files uploaded.\n")

# Unlock colorSpace...
with capsule.ref_edit_unlock():
    cmds.setAttr("*:*.colorSpace", lock=False)


# Upload scene file

scene = cmds.file(query=True, sceneName=True)
scene_dir, scene_fname = os.path.split(scene)
scene_fname, ext = scene_fname.rsplit(".")
safe_name = scene_dir + "/" + scene_fname + "_" + getpass.getuser() + "." + ext
cmds.file(rename=safe_name)
cmds.file(save=True, force=True)

process(["upload_scene_to_nchc", safe_name])

```
