Skip to content

Commit 894fdd1

Browse files
committed
Initial commit
0 parents  commit 894fdd1

File tree

4 files changed

+288
-0
lines changed

4 files changed

+288
-0
lines changed

blender_server.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import socket
2+
import sys
3+
import bpy
4+
5+
HOST = '127.0.0.1'
6+
PORT = 65432
7+
8+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
9+
s.bind((HOST, PORT))
10+
11+
# listen() marks the socket referred to by sockfd as a passive socket (awaits for an in­coming connection,
12+
# which will spawn a new active socket once a connection is established), that is, as a socket that
13+
# will be used to accept incoming connection requests using accept(2).
14+
15+
s.listen()
16+
17+
# Extracts the first connection request on the queue of pending connections for the listening socket,
18+
# sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket.
19+
# The newly created socket is not in the listening state. The original socket sockfd is unaffected by
20+
# this call.
21+
conn, addr = s.accept()
22+
conn.settimeout(0.0)
23+
print("New client: ", addr)
24+
25+
def handle_data():
26+
interval = 0.5
27+
28+
data = None
29+
30+
# In non-blocking mode blocking operations error out with OS specific errors.
31+
# https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
32+
try:
33+
data = conn.recv(1024)
34+
except:
35+
pass
36+
37+
if not data:
38+
pass
39+
else:
40+
print("Received data: " + data.decode("utf-8"))
41+
42+
# Fetch the 'Sockets' collection or create one. Anything created via sockets will be linked
43+
# to that collection.
44+
collection = None
45+
try:
46+
collection = bpy.data.collections["Sockets"]
47+
except:
48+
collection = bpy.data.collections.new("Sockets")
49+
bpy.context.scene.collection.children.link(collection)
50+
51+
try:
52+
imported_object = bpy.ops.import_scene.obj(filepath=data.decode("utf-8"), filter_glob="*.obj;*.OBJ;*.mtl")
53+
conn.sendall(b"ack_")
54+
except:
55+
conn.sendall(b"nack")
56+
57+
if "quit" in data.decode("utf-8"):
58+
conn.close()
59+
s.close()
60+
61+
return interval
62+
63+
bpy.app.timers.register(handle_data)

midi.py

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import rtmidi
2+
from pynput.keyboard import Key
3+
from pynput.mouse import Button
4+
import pynput
5+
import time
6+
from pathlib import Path
7+
8+
# 36 - 51
9+
10+
keyboard = pynput.keyboard.Controller()
11+
mouse = pynput.mouse.Controller()
12+
13+
knob_position = 0
14+
knob_previous = knob_position
15+
16+
BUTTON_TO_BUTTON_ID = {
17+
"A1": 36,
18+
"A2": 37,
19+
"A3": 38,
20+
"A4": 39,
21+
"B1": 40,
22+
"B2": 41,
23+
"B3": 42,
24+
"B4": 43,
25+
"C1": 44,
26+
"C2": 45,
27+
"C3": 46,
28+
"C4": 47,
29+
"D1": 48,
30+
"D2": 49,
31+
"D3": 50,
32+
"D4": 51,
33+
}
34+
35+
cmd = "echo test"
36+
37+
ATOM_NAME = "ATOM"
38+
EXPORT_PATH = Path(r"C:\Users\mc\Desktop\crouching_girl")
39+
40+
midiin = rtmidi.RtMidiIn()
41+
42+
ports = range(midiin.getPortCount())
43+
44+
if ports:
45+
for i in ports:
46+
port_name = midiin.getPortName(i)
47+
print(f"Port name {i}: {port_name}")
48+
if ATOM_NAME in midiin.getPortName(i):
49+
print(f"Opening port {i}: " + midiin.getPortName(i))
50+
m = midiin.openPort(i)
51+
break
52+
53+
def exportMeshZbrush(filename: str):
54+
with keyboard.pressed(Key.ctrl):
55+
with keyboard.pressed(Key.alt):
56+
keyboard.press("p")
57+
keyboard.release("p")
58+
time.sleep(0.02)
59+
with keyboard.pressed(Key.ctrl):
60+
keyboard.press("l")
61+
keyboard.release("l")
62+
time.sleep(0.02)
63+
keyboard.type(str(EXPORT_PATH))
64+
keyboard.press(Key.enter)
65+
keyboard.release(Key.enter)
66+
keyboard.press(Key.tab)
67+
keyboard.release(Key.tab)
68+
keyboard.press(Key.tab)
69+
keyboard.release(Key.tab)
70+
keyboard.type(filename)
71+
keyboard.press(Key.enter)
72+
keyboard.release(Key.enter)
73+
with keyboard.pressed(Key.alt):
74+
keyboard.press("y")
75+
keyboard.release("y")
76+
77+
78+
while True:
79+
m = midiin.getMessage(250)
80+
if m:
81+
if m.isNoteOn():
82+
# print('On', m.getMidiNoteName(m.getNoteNumber()), m.getVelocity())
83+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["A1"]:
84+
exportMeshZbrush("export_1")
85+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["A2"]:
86+
exportMeshZbrush("export_2")
87+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["A3"]:
88+
exportMeshZbrush("export_3")
89+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["A4"]:
90+
exportMeshZbrush("export_4")
91+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["B1"]:
92+
keyboard.press("s")
93+
keyboard.release("s")
94+
time.sleep(0.02)
95+
mouse.press(Button.left)
96+
time.sleep(0.05)
97+
mouse.move(2, 0)
98+
time.sleep(0.05)
99+
mouse.release(Button.left)
100+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["B3"]:
101+
keyboard.press("b")
102+
keyboard.release("b")
103+
time.sleep(0.02)
104+
keyboard.press("c")
105+
keyboard.release("c")
106+
time.sleep(0.02)
107+
keyboard.press("b")
108+
keyboard.release("b")
109+
time.sleep(0.02)
110+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["B2"]:
111+
pass
112+
if m.getNoteNumber() == BUTTON_TO_BUTTON_ID["B4"]:
113+
pass
114+
elif m.isNoteOff():
115+
print("OFF", m.getMidiNoteName(m.getNoteNumber()))
116+
elif m.isController():
117+
print("CONTROLLER", m.getControllerNumber(), m.getControllerValue())
118+
if m.getNoteNumber() == 14:
119+
knob_position = m.getControllerValue()
120+
knob_rotation = knob_position - knob_previous
121+
122+
cursor_movement = knob_rotation
123+
124+
if cursor_movement == 0:
125+
cursor_movement = 2
126+
127+
keyboard.press("s")
128+
keyboard.release("s")
129+
time.sleep(0.02)
130+
mouse.press(Button.left)
131+
time.sleep(0.02)
132+
mouse.move(cursor_movement, 0)
133+
time.sleep(0.02)
134+
mouse.release(Button.left)
135+
136+
knob_previous = knob_position
137+
else:
138+
print("?", m)

requirements.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pathtools==0.1.2
2+
pynput==1.6.8
3+
rtmidi==2.3.4
4+
six==1.14.0
5+
watchdog==0.10.2

wd.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import sys
2+
import time
3+
from watchdog.observers import Observer
4+
from watchdog.events import PatternMatchingEventHandler
5+
from pathlib import Path
6+
from enum import Enum
7+
import socket
8+
9+
EXPORT_PATH = Path(r"C:\Users\mc\Desktop\crouching_girl")
10+
HOST = 'localhost'
11+
PORT =65432
12+
13+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
14+
sock.settimeout(20)
15+
16+
17+
class ArtifactState(Enum):
18+
NEW = 1
19+
MODIFIED = 2
20+
DELETED = 3
21+
22+
class Artifact():
23+
def __init__(self, name: str, state: ArtifactState = ArtifactState.NEW):
24+
self.name = name
25+
self.state = state
26+
27+
artifacts = []
28+
29+
def f_created(event):
30+
print(f"Created... {event.src_path}")
31+
32+
def f_deleted(event):
33+
print(f"Deleted... {event.src_path}")
34+
f_name = Path(event.src_path).name
35+
if 'obj' in event.src_path.lower():
36+
# Check if object is already in the list.
37+
for a in artifacts:
38+
if a.name == f_name:
39+
artifacts.remove(a)
40+
break
41+
42+
def f_modified(event):
43+
print(f"Modified... {event.src_path}")
44+
f_name = Path(event.src_path).name
45+
if 'obj' in event.src_path.lower():
46+
# Check if object is already in the list.
47+
known_files = [f.name for f in artifacts]
48+
if f_name in known_files:
49+
pass
50+
else:
51+
artifacts.append(Artifact(f_name, ArtifactState.MODIFIED))
52+
sock.send(bytes(event.src_path, 'ascii'))
53+
data = sock.recv(4)
54+
print(data)
55+
56+
def f_moved(event):
57+
print(f"Moved... {event.src_path}")
58+
59+
60+
if __name__ == "__main__":
61+
observer = Observer()
62+
event_handler = PatternMatchingEventHandler(ignore_directories=True, case_sensitive=True)
63+
observer.schedule(event_handler, str(EXPORT_PATH), recursive=False)
64+
observer.start()
65+
66+
event_handler.on_created = f_created
67+
event_handler.on_deleted = f_deleted
68+
event_handler.on_modified = f_modified
69+
event_handler.on_moved = f_moved
70+
71+
try:
72+
sock.connect((HOST, PORT))
73+
except socket.timeout:
74+
print("Trying to connect to server timed out...")
75+
76+
try:
77+
while True:
78+
time.sleep(1)
79+
except KeyboardInterrupt:
80+
observer.stop()
81+
observer.join()
82+

0 commit comments

Comments
 (0)