Last active
July 2, 2018 07:49
-
-
Save tokejepsen/d3f74f892186e55b7f5f9dbb49046b6b to your computer and use it in GitHub Desktop.
AfterEffects Python Communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import socket | |
import traceback | |
def send(cmd, port): | |
host = '127.0.0.1' | |
# we expect a result no matter if it errors, so we keep trying until we | |
# get a reply. This is slow, but relyable. | |
keep_trying = True | |
result = "" | |
while(keep_trying): | |
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
conn.connect((host, port)) | |
conn.send(cmd) | |
try: | |
result = conn.recv(4096) | |
conn.close() | |
if result: | |
keep_trying = False | |
except: | |
pass | |
# raise error | |
if result.startswith("Error: "): | |
msg = result.replace("Error: ", "") | |
msg += "CMD: " + cmd | |
raise ValueError(msg) | |
# all replies comes in with a newline | |
result = result.replace("\n", "") | |
# more Pythonic return of nothing | |
if result == "undefined": | |
return None | |
return result | |
def stop_server(port): | |
send("keep_serving = false", port) | |
if __name__ == '__main__': | |
port = int(os.environ["AFTEREFFECTS_PORT"]) | |
try: | |
# Experiment with sending Javascipt commands here safely. | |
print "After Effect version: {0}".format( | |
send("return app.version", port) | |
) | |
except: | |
print traceback.format_exc() | |
finally: | |
raw_input("Pause... Press any key to continue") | |
stop_server(port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// settting port environment variable | |
var port = Math.floor((Math.random() * 1000) + 8000); | |
$.setenv("AFTEREFFECTS_PORT", port); | |
// start pyblish lite | |
var filename = "" | |
try{ | |
filename = app.project.file.fsName | |
}catch(err){} | |
// Remember forward slashes for the python script path | |
var cmd = "start \"\" python \"/path/to/acp.py\"" | |
var batFile= new File("~/aftereffects.bat"); | |
batFile.open("w"); | |
batFile.write(cmd); | |
batFile.close(); | |
batFile.execute(); | |
// create a new socket | |
conn = new Socket(); | |
var keep_serving = true; | |
while (keep_serving) { | |
if (conn.listen(port)) // ... you'd probably want to make this configurable | |
{ | |
// wait forever for a connection | |
var incoming; | |
do incoming = conn.poll(); | |
while (incoming == null); | |
// grab the next non-null communication | |
new_cmd = incoming.read(); | |
try { | |
var F = new Function (new_cmd); | |
var results = F(); | |
incoming.writeln(results); | |
} | |
catch (err) { | |
incoming.writeln("Error: " + err.toString()); | |
} | |
} // end if | |
} // -- end while |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment