python/maya.cmds 中有没有办法将选择从 UV 转换为 UV 外壳边框到包含边缘?

问题描述 投票:0回答:1

在 maya.cmds 中找不到正确的命令来将选区转换为 UV 壳边界和包含的边缘。

基本上,我试图为 Maya 2022 制作一个 Python 脚本。它将根据 UV 壳分割对象。例如一个物体有 6 个 uv 壳,该物体将被分成 6 个不同的部分。

脚本的功能将是: 它将在 uvs 模式下选择所选对象的所有 uvs 点,将该选区转换为 UV 壳边框,然后将该选区转换为包含边缘。然后,启用该选择后,它将运行分离组件操作。之后是一个单独的动作。这将分离物体。

python 3d maya mel
1个回答
0
投票

这是实现此目的的一种方法的基本示例;下面将为您提供每个 UV 壳的面。从那里你可以按照你喜欢的方式处理它,但为了简单起见,我只是复制了每个壳的网格,并删除了不需要的面。

def get_uv_shell_sets(objects):
    """Retrieve UV shells from the specified polygon objects, grouping faces by their associated UV shell.

    This function processes each polygon object provided, identifying and grouping faces based on their UV shell
    association. It's designed to handle multiple objects and consolidate UV shell data into a structured format.

    Parameters:
        objects (obj/list): The polygon object(s) to process. This can be a single polygon object or a list of polygon objects.

    Returns:
        list: A list containing sub-lists, each sub-list represents a UV shell and contains the faces associated with that UV shell.

    Raises:
        Warning: A warning is raised if a UV shell ID cannot be determined for a face, which might indicate an issue with the provided mesh.

    Example:
        >>> uv_shell_face_sets = get_uv_shell_sets('pSphere1')
        >>> print(uv_shell_face_sets)
        [[[MeshFace('pSphereShape1.f[0]'), MeshFace('pSphereShape1.f[1]'), ...]], [[...]], ...]
    """
    meshes = pm.ls(objects, objectsOnly=True)

    all_shells_faces = []
    for mesh in meshes:
        shells_faces = {}
        for face in mesh.faces:
            shell_id = pm.polyEvaluate(face, uvShellIds=True)
            try:
                shells_faces[shell_id[0]].append(face)
            except KeyError:
                try:
                    shells_faces[shell_id[0]] = [face]
                except IndexError:
                    pm.warning(f"Unable to get UV shell ID for face: {face}")

        # Extend the main list with the faces of each shell
        all_shells_faces.extend(list(shells_faces.values()))

    return all_shells_faces


selection = pm.selected()
shells = get_uv_shell_sets(selection)
for faces in shells:
    # Duplicate the original mesh
    original_mesh = faces[0].node()
    duplicated_mesh = pm.duplicate(original_mesh)[0]

    # Delete all faces in the duplicated mesh that don't belong to the current UV shell
    faces_to_delete = [f for f in duplicated_mesh.faces if f not in faces]
    pm.delete(faces_to_delete)
pm.delete(selection)
© www.soinside.com 2019 - 2024. All rights reserved.