Event Simulate: and a --time-actions command line argument

When enabled, print the time taken between running actions.
This commit is contained in:
Campbell Barton 2021-07-13 14:32:41 +10:00
parent 5098678308
commit b90b1af25c
1 changed files with 33 additions and 2 deletions

View File

@ -165,6 +165,16 @@ def gen_events_type_text(text):
yield dict(type=type, value='RELEASE', **kw_extra)
def repr_action(name, args, kwargs):
return "%s(%s)" % (
name,
", ".join(
[repr(value) for value in args] +
[("%s=%r" % (key, value)) for key, value in kwargs.items()]
)
)
# -----------------------------------------------------------------------------
# Simulate Events
@ -505,6 +515,18 @@ def argparse_create():
required=False,
)
parser.add_argument(
"--time-actions",
dest="time_actions",
default=False,
action="store_true",
help=(
"Display the time each action takes\n"
"(useful for measuring delay between key-presses)."
),
required=False,
)
# Collect doc-strings from static methods in `actions`.
actions_docstring = []
for action_key in ACTION_DIR:
@ -554,7 +576,7 @@ def setup_default_preferences(prefs):
# Main Function
def main_event_iter(*, action_list):
def main_event_iter(*, action_list, time_actions):
"""
Yield all events from action handlers.
"""
@ -565,9 +587,18 @@ def main_event_iter(*, action_list):
yield dict(type='MOUSEMOVE', value='NOTHING', x=x_init, y=y_init)
if time_actions:
import time
t_prev = time.time()
for (op, args, kwargs) in action_list:
yield from handle_action(op, args, kwargs)
if time_actions:
t = time.time()
print("%.4f: %s" % ((t - t_prev), repr_action(op, args, kwargs)))
t_prev = t
def main():
from sys import argv
@ -588,7 +619,7 @@ def main():
bpy.app.use_event_simulate = False
run_event_simulate(
event_iter=main_event_iter(action_list=args.actions),
event_iter=main_event_iter(action_list=args.actions, time_actions=args.time_actions),
exit_fn=exit_fn,
)