[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
ただいまコメントを受けつけておりません。
# -*- coding: utf-8 -*-
from maya import cmds
from PIL import Image
import pymel.core as pm
import os
import stat
def get_shading_engines(root_node=None):
'''
指定ノード以下のshading_engineの重複しないリストを取得
'''
en_list = []
if root_node is None:
shapes = pm.ls(type="mesh")
else:
if isinstance(root_node, (str, unicode)): root_node = pm.PyNode(root_node)
shapes = root_node.listRelatives(ad=True, type="mesh")
file_nodes = []
for i in shapes:
shading_engines = i.shadingGroups()
en_list+=shading_engines
#重複をなくしてから戻す
return list(set(en_list))
def get_all_texture_node(root_node=None):
'''
指定ノード以下に接続されている全テクスチャノードを取得
'''
file_nodes = []
en_list = get_shading_engines(root_node)
for en in en_list:
file_nodes.extend(cmds.ls(cmds.listHistory(en.name()),type='file'))
return list(set(file_nodes))
def get_truth_file_path(filepath, dir):
'''
パスの存在をしらべる。なければセットプロジェクトされているフォルダ以下にないか探しにいく。
'''
if os.path.exists(filepath):
return filepath
workpath = pm.workspace( q=True, rootDirectory=True )
if dir is not None:
workpath = workpath + dir
filename = os.path.basename(filepath)
for root, dirs, files in os.walk(workpath):
for file in files:
if filename == file:
return root + r"/" +file
return filepath
def get_use_texture_path_list(root_node=None):
'''
指定ノード以下に接続されているテクスチャパスのリスト
'''
file_nodes = get_all_texture_node(root_node)
files = []
for tex in file_nodes:
path = cmds.getAttr(tex + '.fileTextureName')
truth_path= get_truth_file_path(path, "sourceimages")
if truth_path is None:continue;
files.append(truth_path)
files = list(set(files))
return files
def set_texture_to_specified_dir(root_node=None, dir=""):
'''
指定ノード以下に接続されているテクスチャを指定のディレクトリに接続し直す
'''
file_nodes = get_all_texture_node(root_node)
for tex in file_nodes:
def_path = cmds.getAttr(tex + '.fileTextureName')
base_name = os.path.basename(def_path)
new_path = os.path.join(dir, base_name)
if os.path.isfile(new_path):
cmds.setAttr(tex + '.fileTextureName', new_path, type='string')
def resize_texture(root_node=None, dir="", magnification=1, width=None, height=None):
'''
指定ノード以下のテクスチャをリサイズ(別フォルダに出力)
画像幅と高さが両方していされてればそっち優先
root_node : テクスチャ書き出すルートノード
export_dir : 書き出すフォルダ
magnification : テクスチャの倍率
width : 画像幅
height: 画像高さ
'''
files = get_use_texture_path_list(root_node)
if os.path.exists(dir) == False:
os.makedirs(dir)
# ペースト先のファイルが読み取り専用だった場合エラーになるので対処しとく
for f in files:
dir_path, file_name = os.path.split(f)
save_file_path = dir+'\\'+file_name
if os.path.exists(save_file_path):
os.chmod(save_file_path, stat.S_IWRITE)
im = Image.open(f)
w = int(im.size[0]) * magnification
h = int(im.size[1]) * magnification
if width is not None and height is not None:
w = width
h = height
im.resize((int(w),int(h))).save(save_file_path)
set_texture_to_specified_dir(root_node=root_node, dir=dir)
resize_texture(root_node='root', dir=r'C:\temp\test', magnification=0.1)