Reputation: 11
Can't find the right command in maya.cmds to convert selection into UV Shell border and Contained Edges.
Basically, I was trying to make a Python script for maya 2022. Which will split object according the UV shells. For example an object has 6 uv shells, The object is going to be separated into 6 different pieces.
The functions of script are going to be: It will select all uvs points of the selected object in uvs mode, convert that selection into UV Shell border, and then convert that selection to Contained Edges. Then with that selection on, it is going to run the detach component action. after that a separate action. which will separate the object.
Upvotes: 1
Views: 1005
Reputation: 333
Here is a basic example of one way to accomplish this; The following will get you the faces of each UV shell. From there you can handle it how you like, but for the sake of simplicity, I just duplicated the mesh for each shell, and deleted the unwanted faces.
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 (str/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)
Upvotes: 0