如何用Maya Python API获取几何体各UV Shell的对应面?
获取几何体UV Shell对应面:API与cmds结果差异解决方案
问题背景
需要查询几何体每个UV Shell对应的面,使用maya.cmds.polyEvaluate(uvShellIds=True)能得到按面返回的Shell ID,但回调场景下速度不足。改用Maya Python API的MFnMesh.getUvShellsIds()时,返回结果长度与cmds不一致,无法直接对应到面。
差异说明
cmds.polyEvaluate(uvShellIds=True):返回每个面的UV Shell ID,列表长度等于几何体的面数(比如默认立方体返回6个ID)。MFnMesh.getUvShellsIds():返回每个UV顶点的Shell ID,列表长度等于模型的UV顶点总数(默认立方体的UV顶点数少于24是因为共享UV导致)。
解决方案
通过将UV顶点级的Shell ID映射到面级即可得到与cmds一致的结果。由于单个面的所有UV顶点必然属于同一个UV Shell,只需取每个面的任意一个UV顶点对应的Shell ID作为该面的Shell ID即可。
实现代码
import maya.api.OpenMaya as om def get_face_uv_shell_ids(mesh_dag_path): mesh_fn = om.MFnMesh(mesh_dag_path) # 获取每个面的UV数量与全局UV索引 uvs_per_face, uv_indices = mesh_fn.getAssignedUVs() # 获取所有UV顶点对应的Shell ID _, shell_ids = mesh_fn.getUvShellsIds() face_shell_ids = [] current_idx = 0 for face_uv_count in uvs_per_face: # 取当前面第一个UV顶点的Shell ID作为该面的Shell ID face_shell_ids.append(shell_ids[uv_indices[current_idx]]) current_idx += face_uv_count return face_shell_ids # 调用示例 sel_list = om.MSelectionList() sel_list.add("pCube1") dag_path = sel_list.getDagPath(0) print(get_face_uv_shell_ids(dag_path))
效果验证
上述代码运行后,输出结果会与maya.cmds.polyEvaluate('pCube1.f[*]', uvShellIds=True)完全一致,但执行速度远快于cmds,适合在回调等对性能敏感的场景中使用。
内容的提问来源于stack exchange,提问作者MyHornCanPierceTheSky
相关产品推荐
相关产品推荐

