Skip to content

Instantly share code, notes, and snippets.

@zclongpop123
Last active January 11, 2023 20:49
Show Gist options
  • Save zclongpop123/2440119b109ddb91400ab4668c4f2f2f to your computer and use it in GitHub Desktop.
Save zclongpop123/2440119b109ddb91400ab4668c4f2f2f to your computer and use it in GitHub Desktop.
自动初始化UV
#========================================
# author: Changlong.Zang
# mail: zclongpop123@163.cOpenMaya.
# time: Wed Oct 20 21:20:37 2021
#========================================
import os
import glob
import datetime
import maya.cmds as mc
import pymel.core as pm
import maya.OpenMaya as OpenMaya
import maya.api.OpenMaya as om
#--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
def export_uv(uv_file):
'''
'''
l_meshes = []
top_group = 'master'
if pm.objExists('master_grp'):
top_group = 'master_grp'
mesh_group = '|%s|poly' % top_group
l_meshes = pm.listRelatives(mesh_group, ad=True, type='mesh')
f = open(uv_file, 'w')
f.write('// ' + pm.sceneName() + '\n')
for node in l_meshes:
if node.isIntermediateObject():
continue
f.write(
' '.join([node.fullPath(), str(node.numVertices()), str(node.numEdges()), str(node.numFaces())]) + '\n')
uvset_list = [n for n in pm.polyUVSet(node, allUVSets=True, q=True)]
for uvset in uvset_list:
f.write(uvset + '\n')
pm.polyUVSet(node, currentUVSet=True, uvSet=uvset)
sl = om.MSelectionList()
sl.add(node.name())
mesh_dag = sl.getDagPath(0)
mesh_mfn = om.MFnMesh(mesh_dag)
(l_u, l_v) = mesh_mfn.getUVs()
(uv_cnts, uv_ids) = mesh_mfn.getAssignedUVs()
if not (l_u and l_v and uv_cnts and uv_ids):
continue
f.write(' '.join([str(i) for i in l_u]) + '\n')
f.write(' '.join([str(i) for i in l_v]) + '\n')
f.write(' '.join([str(i) for i in uv_cnts]) + '\n')
f.write(' '.join([str(i) for i in uv_ids]) + '\n')
pm.polyUVSet(node, currentUVSet=True, uvSet='map1')
f.close()
def main():
'''
'''
for asset_root in glob.iglob('Z:/projects/huoshen/publish/asset/*/*'):
if not os.path.isdir(asset_root):
continue
asset_name = os.path.basename(asset_root)
uv_file = os.path.join(asset_root, 'srf', '{0}.srf.surfacing'.format(asset_name), '{0}.uv'.format(asset_name))
if not os.path.isfile(uv_file):
continue
print uv_file
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(uv_file))
if mtime.year == 2021 and mtime.month == 10 and mtime.day >= 20:
continue
srf_file = '{0}.ma'.format(os.path.splitext(uv_file)[0])
print srf_file
try:
mc.file(srf_file, o=True, f=True)
except:
continue
for geo_node in mc.listRelatives(mc.ls(typ='mesh'), p=True, path=True):
try:
geo_mfn = OpenMaya.MFnMesh(pm.PyNode(geo_node).__apimdagpath__())
except:
continue
geo_uv_names = list()
geo_mfn.getUVSetNames(geo_uv_names)
if not geo_uv_names:
uv_set = mc.polyUVSet(create=True, uvSet='map1')
mc.polyUVSet(currentUVSet=True, uvSet=uv_set[0])
mc.polyAutoProjection(geo_node)
_u = OpenMaya.MFloatArray()
_v = OpenMaya.MFloatArray()
for uv_set in geo_uv_names:
geo_mfn.getUVs(_u, _v, uv_set)
if not _u or not _v:
mc.polyUVSet(currentUVSet=True, uvSet=uv_set)
mc.polyAutoProjection(geo_node)
export_uv(uv_file)
print 'OK!\n'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment