Last active
February 18, 2022 08:52
-
-
Save zclongpop123/acb2828e207d55f0d9ccc84d0fc60dc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#======================================== | |
# author: Changlong.Zang | |
# mail: [email protected] | |
# time: Wed Feb 16 13:41:00 2022 | |
#======================================== | |
import re | |
import os | |
import shutil | |
import itertools | |
import maya.cmds as mc | |
import pymel.core as pm | |
import maya.OpenMaya as OpenMaya | |
#--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | |
attr_map = { | |
'baseColor': 'color', | |
'opacity':'transparency', | |
'metalness':'incandescence', | |
'specularRoughness':'sc', | |
'normalCamera':'normalCamera' | |
} | |
udim_list = [list(range(1001, 1101)[i:i+10]) for i in range(0, 100, 10)] | |
def main(geometry): | |
''' | |
''' | |
#- | |
mesh_pml_node = pm.PyNode(geometry) | |
mesh_api_obj = mesh_pml_node.__apimobject__() | |
mesh_api_mfn = OpenMaya.MFnMesh(mesh_api_obj) | |
u_array = OpenMaya.MFloatArray() | |
v_array = OpenMaya.MFloatArray() | |
mesh_api_mfn.getUVs(u_array, v_array) | |
u_array = [x // 1 for x in u_array] | |
v_array = [x // 1 for x in v_array] | |
uv_units = list() | |
for u, v in zip(u_array, v_array): | |
k = 'uv_{0}'.format(udim_list[int(u)][int(v)]) | |
if k not in uv_units: | |
uv_units.append(k) | |
#- | |
sg_nodes = list() | |
sg_iterator = OpenMaya.MItDependencyGraph(mesh_api_obj, OpenMaya.MFn.kShadingEngine, OpenMaya.MItDependencyGraph.kDownstream) | |
while not sg_iterator.isDone(): | |
sg_nodes.append(OpenMaya.MFnDependencyNode(sg_iterator.currentItem()).name()) | |
sg_iterator.next() | |
sg_nodes = [sg for sg in sg_nodes if sg not in ['initialShadingGroup']] | |
#- | |
shading_data = dict() | |
for sg_node, unit in itertools.product(sg_nodes, uv_units): | |
shading_node = pm.PyNode(sg_node).surfaceShader.connections() | |
if not shading_node: | |
continue | |
if not pm.nodeType(shading_node[0]).startswith('ai'): | |
continue | |
#- | |
members = list() | |
for meb in pm.sets(sg_node, q=True): | |
if not '.f' in meb.name(): | |
members.extend(mc.ls('{0}.f[:]'.format(meb.name()), fl=True)) | |
else: | |
members.extend(mc.ls(meb.name(), fl=True)) | |
shading_data[shading_node[0].name()] = members | |
#- | |
shader_name = '{0}_{1}'.format(unit, shading_node[0].name()) | |
if mc.objExists(shader_name): | |
continue | |
blinn_shader = pm.shadingNode('blinn', name=shader_name, asShader=True) | |
blinn_sg = pm.createNode('shadingEngine', name='{0}SG'.format(blinn_shader)) | |
blinn_shader.outColor >> blinn_sg.surfaceShader | |
for ai_attr in attr_map.keys(): | |
color_plug = shading_node[0].attr(ai_attr).__apiobject__() | |
iterator = OpenMaya.MItDependencyGraph(color_plug, OpenMaya.MFn.kFileTexture, OpenMaya.MItDependencyGraph.kUpstream) | |
texture_node = None | |
while not iterator.isDone(): | |
texture_node = pm.PyNode(OpenMaya.MFnDependencyNode(iterator.currentItem()).name()) | |
iterator.next() | |
if texture_node is not None: | |
new_texure_node = pm.duplicate(texture_node) | |
new_texure_node[0].setName('{0}_{1}'.format(unit, texture_node)) | |
orig_img = new_texure_node[0].ftn.get().replace('\\', '/').replace('.tx', '.tif') | |
new_img = orig_img.replace('/tex/', '/uetex/') | |
img_udim = re.search('(?<=_)\d{4}(?=\.)', new_img) | |
if img_udim: | |
new_img = re.sub(img_udim, 'ue{0}'.format(img_udim.group()), new_img) | |
if os.path.isfile(orig_img) and not os.path.isfile(new_img): | |
if not os.path.isdir(os.path.dirname(new_img)): | |
os.makedirs(os.path.dirname(new_img)) | |
shutil.copy2(orig_img, new_img) | |
new_texure_node[0].ftn.set(new_img, typ='string') | |
new_texure_node[0].uvTilingMode.set(0) | |
new_texure_node[0].outColor >> blinn_shader.attr(attr_map[ai_attr]) | |
pm.delete(sg_nodes) | |
for shader, faces in shading_data.items(): | |
for f in faces: | |
fn = pm.PyNode(f) | |
us, vs = fn.getUVs() | |
uc = sum(us) / len(us) | |
vc = sum(vs) / len(vs) | |
unit = 'uv_{0}'.format(udim_list[int(uc // 1.0)][int(vc // 1.0)]) | |
unit_shader = '{0}_{1}'.format(unit, shader) | |
if not unit_shader: | |
continue | |
mc.sets(f, e=True, forceElement='{0}SG'.format(unit_shader)) | |
if __name__ == '__main__': | |
for geo in mc.ls(geometry=True): | |
main(geo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment