-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_selectNURBSCurvesInbetween.py
142 lines (122 loc) · 5.29 KB
/
mr_selectNURBSCurvesInbetween.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
# ------------------------------------------------------------------------------ #
# SCRIPT: mr_selectNURBSCurvesInbetween.py
# VERSION: 0003
#
# CREATORS: Maria Robertson
# ---------------------------------------
# Last tested for Autodesk Maya 2023.3
# ---------------------------------------
# DESCRIPTION:
# ---------------------------------------
# A script to help make selecting controls in hierarchies faster.
#
# EXAMPLE USES:
# ---------------------------------------
# - Selecting tail controls
# - Selecting spine controls
#
# INSTRUCTIONS:
# ---------------------------------------
# Select two NURB controls, and run the script to also select any inbetween NURB controls.
# e.g. select tail_01_ctrl and tail_12_ctrl to also select every tail_ctrl inbetween them.
#
# ---------------------------------------
# RUN COMMAND:
# ---------------------------------------
import importlib
import mr_selectNURBSCurvesInbetween
importlib.reload(mr_selectNURBSCurvesInbetween)
mr_selectNURBSCurvesInbetween.main(always_select_top_to_bottom=False)
# ---------------------------------------
# CHANGELOG:
# ---------------------------------------
# 2024-01-15 - 0003:
# - Renaming from mr_select_inbetween_nurb_controls.py to mr_selectNURBSCurvesInbetween.py.
# - Added option to always select from top to bottom, regardless of selection order.
#
# 2023-07-09 - 0002:
# - Cleaning comments.
# - Removing unneeded check for constraints.
#
# 2023-07-09 - 0002:
# - Finished second pass of script, to work in both top-to-bottom and bottom-to-top hierarchies.
#
# 2023-07-03 - 0001:
# - First pass.
# ------------------------------------------------------------------------------ #
"""
import maya.cmds as cmds
import maya.mel as mel
def main(always_select_top_to_bottom=False):
# -------------------------------------------------------------------
# 01. CHECK IF ONLY TWO OBJECTS ARE SELECTED.
# -------------------------------------------------------------------
selection = cmds.ls(selection=True)
if len(selection) != 2:
cmds.warning("Please select exactly two objects.")
return
# -------------------------------------------------------------------
# 01. CHECK IF SELECTED CONTROLS ARE NURBS CURVE TRANSFORMS.
# -------------------------------------------------------------------
for obj in selection:
shape = cmds.listRelatives(obj, shapes=True)
if shape and cmds.nodeType(shape[0]) == "nurbsCurve":
continue
else:
cmds.warning("Both selected objects must be NURB Curve transforms.")
return
# -------------------------------------------------------------------
# 01. CHECK IF OBJECTS SHARE SAME HIERARCHY.
# -------------------------------------------------------------------
first_item = selection[0]
second_item = selection[1]
first_fullpath = cmds.ls(first_item, long=True)[0]
second_fullpath = cmds.ls(second_item, long=True)[0]
first_object_hierarchy = first_fullpath.split('|')
second_object_hierarchy = second_fullpath.split('|')
hierarchy_objects = []
order = []
if first_item in second_object_hierarchy:
order = "top-to-bottom"
for obj in second_object_hierarchy[1:]:
hierarchy_objects.append(obj)
elif second_item in first_object_hierarchy:
order = "bottom-to-top"
for obj in first_object_hierarchy[1:]:
hierarchy_objects.append(obj)
else:
cmds.warning("Must select two objects in the same hierarchy branch.")
return
# -------------------------------------------------------------------
# 01. SELECT ONLY NURBS CURVES INBETWEEN THE SELECTION.
# -------------------------------------------------------------------
# Find the index of the selected objects in hierarchy_objects.
index1 = hierarchy_objects.index(first_item)
index2 = hierarchy_objects.index(second_item)
inbetween_objects = hierarchy_objects[min(index1, index2):max(index1, index2) + 1]
# Filter out unwanted transform types in inbetween_objects.
selected_transforms = [obj for obj in inbetween_objects if cmds.nodeType(obj) == "transform"]
# Create list of only NURBS curve transforms from selected_transforms.
nurbs_curve_transforms = [
transform
for transform in selected_transforms
if cmds.listRelatives(transform, shapes=True) and
cmds.nodeType(cmds.listRelatives(transform, shapes=True)[0]) == "nurbsCurve"]
if not nurbs_curve_transforms:
cmds.warning("No NURBS curve transforms found between the selected objects.")
return
# -------------------------------------------------------------------
# 01. FINSH WITH FOUND NURBS CURVE TRANSFORMS SELECTED.
# -------------------------------------------------------------------
if order == "top-to-bottom":
cmds.select(nurbs_curve_transforms, replace=True)
else:
if always_select_top_to_bottom:
cmds.select(nurbs_curve_transforms, replace=True)
else:
reversed_nurbs_curve_transforms = nurbs_curve_transforms[::-1]
cmds.select(reversed_nurbs_curve_transforms, replace=True)
# End with the Translate manipulator on.
mel.eval("buildTranslateMM;")
mel.eval("destroySTRSMarkingMenu MoveTool;")