-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_set_currentTime_halfway.py
181 lines (154 loc) · 6.9 KB
/
mr_set_currentTime_halfway.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"""
# ------------------------------------------------------------------------------ #
# SCRIPT: mr_set_currentTime_halfway.py
# VERSION: 0006
#
# CREATORS: Maria Robertson
# ---------------------------------------
# Last tested for Autodesk Maya 2023.3
# ---------------------------------------
# DESCRIPTION:
# ---------------------------------------
# Set the current time to the midpoint of queried parameters, depending where the mouse cursor is.
#
# - If the mouse pointer is NOT above Graph Editor:
# - Set the current time to the midpoint of the playback range.
#
# - If the mouse pointer IS above Graph Editor:
# - If keys on animation curves are selected:
# - Set the current time to the midpoint of the selected keys' range.
# - If NO keys on animation curves are selected:
# - Set the current time to the midpoint of the next and previous key of the current frame.
# - If there are keys at the current frame, do nothing.
#
# ---------------------------------------
# RUN COMMANDS:
# ---------------------------------------
import importlib
import mr_set_currentTime_halfway
importlib.reload(mr_set_currentTime_halfway)
# USE ONE OF THE FOLLOWING:
mr_set_currentTime_halfway.main(float=False)
mr_set_currentTime_halfway.main(float=True)
# ------------------------------------------------------------------------------ #
"""
import maya.cmds as cmds
# ------------------------------------------------------------------------------ #
def main(float=False):
"""
:param float: If False, always set the current time as a whole number, with no sub-frames.
:type float: float
"""
selection = cmds.ls(selection=True)
# ---------------------------------------
# 01. IF NOTHING IS SELECTED.
# ---------------------------------------
if not selection:
startTime = cmds.playbackOptions(query=True, min=True)
endTime = cmds.playbackOptions(query=True, max=True)
# Go to midpoint of the playback range.
if float:
midTime = (startTime + endTime) / 2.0
else:
midTime = (startTime + endTime) // 2
cmds.currentTime(midTime)
return
current_panel = cmds.getPanel(underPointer=True)
current_time = cmds.currentTime(query=True)
# ---------------------------------------
# 01. IF MOUSE CURSOR IS OVER GRAPH EDITOR.
# ---------------------------------------
if current_panel == "graphEditor1":
# ---------------------------------------
# 02. IF KEYS ARE SELECTED.
# ---------------------------------------
selected_keys = cmds.keyframe(query=True, selected=True)
if selected_keys:
keyframe_range = (min(selected_keys), max(selected_keys))
# Go to midpoint of selected keys.
if float:
midTime = (keyframe_range[0] + keyframe_range[1]) / 2.0
else:
midTime = (keyframe_range[0] + keyframe_range[1]) // 2
cmds.currentTime(midTime)
return
# ---------------------------------------
# 01. IF THERE ARE VISIBLE ANIMATIONS CURVES IN THE GRAPH EDITOR.
# ---------------------------------------
# Check if any visible animation curves in the Graph Editor have keys at current time.
visible_animation_curves = cmds.animCurveEditor('graphEditor1GraphEd', query=True, curvesShown=True)
if visible_animation_curves:
are_keys_at_currentTime = cmds.keyframe(query=True, time=(current_time, current_time), keyframeCount=True)
# If there are no keys,
if are_keys_at_currentTime == 0:
# go to midpoint of next and previous keys.
midPoint = get_midpoint_of_next_and_previous_key_at_currentTime(float=float)
cmds.currentTime(midPoint)
# Otherwise, do nothing.
else:
return
# ---------------------------------------
# 01. OTHERWISE
# ---------------------------------------
else:
midPoint = get_midpoint_of_next_and_previous_key_at_currentTime(float=float)
cmds.currentTime(midPoint)
return
##################################################################################################################################################
########################################################################
# #
# SUPPORT FUNCTIONS #
# #
########################################################################
# ------------------------------------------------------------------------------ #
def get_midpoint_of_next_and_previous_key_at_currentTime(float=False):
"""
Get the midpoint between the previous and next keyframes at the current time.
:param float: If False, always set the current time as a whole number, with no sub-frames.
:type float: float
"""
current_time = cmds.currentTime(query=True)
startTime = cmds.playbackOptions(query=True, min=True)
endTime = cmds.playbackOptions(query=True, max=True)
prevKeyframe = cmds.findKeyframe(timeSlider=True, which="previous")
nextKeyframe = cmds.findKeyframe(timeSlider=True, which="next")
if prevKeyframe is None or prevKeyframe >= current_time:
prevKeyframe = startTime
if nextKeyframe is None or nextKeyframe <= current_time:
nextKeyframe = endTime
# Go to the midpoint of the previous and next keyframe of selected objects.
if float:
midPoint = (nextKeyframe + prevKeyframe) / 2.0
else:
midPoint = (nextKeyframe + prevKeyframe) // 2
return midPoint
"""
##################################################################################################################################################
# ---------------------------------------
# CHANGELOG:
# ---------------------------------------
# 2024-02-25 - 0006:
# - Adding outcome for if mouse cursor is not over graph editor and no visible animation curves are there.
#
# 2024-01-28 - 0005:
# - Renamed and divided original function.
# - Fixed logic for when mouse cursor is over graph editor, and no keys are selected.
# - Rearranged logic.
# - Added to description.
# - Moved changelog to the bottom.
#
# 2023-06-25 - 0004:
# - Renamed script.
# - Adding option to set currentTime as float or integer.
#
# 2023-06-24 - 0003:
# - Converted script from MEL to Python.
# - Added ability to jump to midpoint of selected keys in Graph Editor.
# - Added option to use startTime or endTime as the prev/nextKeyframe, if one doesn't exist.
#
# 2023-01-30 - 0002:
# - If no objects are selected, script will jump to midpoint of playback range.
#
# ---------------------------------------
##################################################################################################################################################
"""