test_connection.pyā¢4.09 kB
import socket
import json
import time
import logging
import os
from pathlib import Path
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
# Configuration
AE_HOST = "127.0.0.1"
AE_PORT = 8250
TEMP_DIR = "C:/ae_temp"
COMMAND_FILE = os.path.join(TEMP_DIR, "command.json")
RESULT_FILE = os.path.join(TEMP_DIR, "result.json")
def test_tcp_connection():
"""Test TCP connection to After Effects"""
try:
logging.info("Attempting TCP connection to After Effects...")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(2)
sock.connect((AE_HOST, AE_PORT))
# Send test command
test_command = {
"type": "create_text_layer",
"params": {
"text": "Test Connection",
"position": [960, 540],
"fontSize": 48,
"color": [1, 1, 1]
}
}
sock.send(json.dumps(test_command).encode())
response = sock.recv(4096).decode()
result = json.loads(response)
if result.get("status") == "success":
logging.info("TCP connection successful!")
return True
else:
logging.error(f"TCP command failed: {result.get('message')}")
return False
except ConnectionRefusedError:
logging.error("TCP connection refused. After Effects might not be running or the MCP Bridge script is not active.")
return False
except socket.timeout:
logging.error("TCP connection timed out.")
return False
except Exception as e:
logging.error(f"TCP connection error: {str(e)}")
return False
finally:
sock.close()
def test_file_connection():
"""Test file-based connection to After Effects"""
try:
logging.info("Attempting file-based connection to After Effects...")
# Create temp directory if it doesn't exist
Path(TEMP_DIR).mkdir(parents=True, exist_ok=True)
# Write test command
test_command = {
"type": "create_text_layer",
"params": {
"text": "Test File Connection",
"position": [960, 540],
"fontSize": 48,
"color": [1, 1, 1]
}
}
with open(COMMAND_FILE, 'w') as f:
json.dump(test_command, f)
# Wait for result
max_wait = 10 # seconds
start_time = time.time()
while time.time() - start_time < max_wait:
if os.path.exists(RESULT_FILE):
with open(RESULT_FILE, 'r') as f:
result = json.load(f)
# Clean up files
os.remove(COMMAND_FILE)
os.remove(RESULT_FILE)
if result.get("status") == "success":
logging.info("File-based connection successful!")
return True
else:
logging.error(f"File-based command failed: {result.get('message')}")
return False
time.sleep(0.5)
logging.error("File-based connection timed out waiting for response.")
return False
except Exception as e:
logging.error(f"File-based connection error: {str(e)}")
return False
def test_ae_connection():
"""Test connection to After Effects using both TCP and file-based methods"""
logging.info("Testing After Effects connection...")
# Try TCP first
if test_tcp_connection():
return True
logging.info("TCP connection failed, trying file-based connection...")
# If TCP fails, try file-based
if test_file_connection():
return True
logging.error("Both connection methods failed. Please check After Effects and the MCP Bridge script.")
return False
if __name__ == "__main__":
test_ae_connection()