Page MenuHome
Paste P657

x11_clipboard_watch_operator_id_py_to_c.py
ActivePublic

Authored by Campbell Barton (campbellbarton) on Apr 16 2018, 2:18 PM.
#!/usr/bin/env python3
"""
Continually check for operator strings in the clipboard.
"""
VERBOSE = False
def run(cmd, return_result=True):
if VERBOSE:
print(">>> ", cmd)
import subprocess
if return_result:
proc = subprocess.Popen(
cmd,
shell=False,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
result = proc.stdout.read()
return result
else:
proc = subprocess.Popen(cmd, shell=False)
proc.wait()
def translate(w):
pre = "bpy.ops."
if w.startswith(pre):
w = w[len(pre):].split('(')[0]
head, tail = w.partition('.')[0::2]
text = head.upper() + "_OT_" + tail
if text != translate.text:
print('- ' + text)
translate.text = text
translate.text = None
def main():
import time
clip_prev = None
while 1:
clip_curr = run(["xclip", "-selection", "clipboard", "-out"])
# first time only, don't do anything
if clip_prev is None:
clip_prev = clip_curr
if clip_curr != clip_prev:
clip_text = clip_curr.decode('utf-8')
if clip_text:
translate(clip_text)
clip_prev = clip_curr
time.sleep(0.05)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("finishing up!")