Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

03001_谢丽妍_期中作业第一版 #13

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions core/Asset_IO_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# coding=utf-8
import os
import pprint
import shutil
import time
import json


def get_input_data():
"""
get input directory and get all the maya files info
:return: maya files data and input directory
"""
input_dir = input("请选择客户文件路径>>>>>>>>>>")
# r'C:\Users\geyij\Desktop\new\AssetIO\example\project\Inbox\20191001'
input_data = {}
for a, b, c in os.walk(input_dir):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要使用 a,b,c 作为变量名称

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for path, dirs, files in os.walk

if c:
if c[0].endswith('.ma'):
i, j, k = c[0].split('.')[0].split('_')
file_info = {
'input_file_name': c[0],
'input_file_path': a,
'asset_name': j.lower(),
'asset_type': k.lower(),
'seq': i.upper()
}
input_data[file_info['asset_name']] = file_info
return input_data, input_dir


def create_directory(directory):
"""
check if the dir exists and create it
:param directory:
:return:
"""
if not os.path.exists(directory):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.isdir(directory)会优于 os.path.exists

os.makedirs(directory)


def get_file_time(directory):
"""
get the create time of file or folder
:param directory:
:return: create_time
"""
file_time_sec = os.path.getctime(directory)
file_time_struct = time.localtime(file_time_sec)
file_time_str = time.strftime('%Y-%m-%d-%H-%M-%S', file_time_struct)
return file_time_str


def bak_file(directory):
"""
get a full path file and create a bak folder with file create time
then bak file
:param directory:
:return:
"""
file_directory = directory
file_path = os.path.split(file_directory)[0]
file_name = os.path.split(file_directory)[1]
file_time = get_file_time(file_directory)
bak_path = os.path.join(file_path, 'bak', file_time)
create_directory(bak_path)
bak_name = os.path.splitext(file_name)[0] + '_' + file_time + os.path.splitext(file_name)[1]
bak_file_directory = os.path.join(bak_path, bak_name)
os.rename(file_directory, bak_file_directory)


def save_data_file(data, name, directory):
"""
save given date with given name in given dir
:param data: dic data
:param name: date file name
:param directory: target dir
:return: saved file full path
"""
save_data = data
save_name = name + '.json'
save_path = directory
save_file = os.path.join(save_path, save_name)
with open(save_file, 'w+') as f:
json.dump(save_data, f)
return save_file


def copy_input_files():
"""
get a show path
get input data from input dir
save client files to right dir
update assets data
and save data to a json file
:return: asset data dic and data file path
"""
assets_data, input_dir = get_input_data()
show_dir = input("请选择项目路径>>>>>>>>>>>")
# r'C:\Users\geyij\Desktop\new\AssetIO\example\project\TDC'
show_name = show_dir.rpartition('\\')[-1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show_name = os.path.basename(show_dir)


input_dir_time = get_file_time(input_dir)
data_file_name = 'Input_Data_' + input_dir_time

for asset, info in assets_data.items():
asset_name = info['asset_name'].capitalize()
asset_type = info['asset_type'].capitalize()
save_name = f'{show_name}_{asset_name}_{asset_type}.ma'
save_path = os.path.join(show_dir, 'Asset', asset_type, asset_name)
save_file = os.path.join(save_path, save_name)
create_directory(save_path)
if os.path.exists(save_file):
bak_file(save_file)
input_file = os.path.join(assets_data[asset]['input_file_path'], assets_data[asset]['input_file_name'])
shutil.copy(input_file, save_file)
assets_data[asset]['saved_file_name'] = save_name
assets_data[asset]['saved_file_path'] = save_path

saved_data_file = save_data_file(assets_data, data_file_name, input_dir)

return assets_data, saved_data_file


copy_input_files()