Skip to content

Commit 46c6d6f

Browse files
committed
FIX light refactoring and input reorder
1 parent e09fb79 commit 46c6d6f

File tree

9 files changed

+146
-135
lines changed

9 files changed

+146
-135
lines changed

deps/eigen

Submodule eigen updated from ac6955e to 81044ec

src/gh/components/DF_http_listener/code.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ def RunScript(self,
1616
i_load: bool,
1717
i_ply_url: str):
1818

19-
sc.sticky.setdefault('ply_url', None) #last url processed
20-
sc.sticky.setdefault('imported_geom', None) #geo imported from ply
21-
sc.sticky.setdefault('status_message','Idle') #status message on component
22-
sc.sticky.setdefault('prev_load', False) #previous state of toggle
23-
sc.sticky.setdefault('thread_running', False) #is an import thread active?
19+
prefix = 'http'
20+
21+
# initialize sticky variables
22+
sc.sticky.setdefault(f'{prefix}_ply_url', None) # last url processed
23+
sc.sticky.setdefault(f'{prefix}_imported_geom', None) # last geo imported from ply
24+
sc.sticky.setdefault(f'{prefix}_status_message', "Waiting..") # status message on component
25+
sc.sticky.setdefault(f'{prefix}_prev_load', False) # previous state of toggle
26+
sc.sticky.setdefault(f'{prefix}_thread_running', False) # is a background thread running?
2427

2528
def _import_job(url):
2629
"""
@@ -30,7 +33,10 @@ def _import_job(url):
3033
- Extracts the new geometry (point cloud or mesh)
3134
- Cleans up the temporary file and document objects
3235
- Updates sticky state and status message
36+
- Signals to GH that it should re-solve
3337
"""
38+
39+
tmp = None
3440
try:
3541
if not url.lower().endswith('.ply'):
3642
raise ValueError("URL must end in .ply")
@@ -71,43 +77,43 @@ def _import_job(url):
7177
doc.Views.Redraw()
7278

7379
# store new geometry
74-
sc.sticky['imported_geom'] = geom
80+
sc.sticky[f'{prefix}_imported_geom'] = geom
7581
count = geom.Count if isinstance(geom, rg.PointCloud) else geom.Vertices.Count
7682
if isinstance(geom, rg.PointCloud):
77-
sc.sticky['status_message'] = f"Done: {count} points"
83+
sc.sticky[f'{prefix}_status_message'] = f"Loaded pcd with {count} pts"
7884
else:
79-
sc.sticky['status_message'] = f"Done: {count} vertices"
80-
ghenv.Component.Message = sc.sticky.get('status_message') # noqa: F821
85+
sc.sticky[f'{prefix}_status_message'] = f"Loaded mesh wih {count} vertices"
86+
ghenv.Component.Message = sc.sticky.get(f'{prefix}_status_message') # noqa: F821
8187

8288
except Exception as e:
83-
sc.sticky['imported_geom'] = None
84-
sc.sticky['status_message'] = f"Error: {e}"
89+
sc.sticky[f'{prefix}_imported_geom'] = None
90+
sc.sticky[f'{prefix}_status_message'] = f"Error: {e}"
8591
finally:
8692
try:
8793
os.remove(tmp)
8894
except Exception:
8995
pass
9096
# mark thread as finished
91-
sc.sticky['thread_running'] = False
97+
sc.sticky[f'{prefix}_thread_running'] = False
9298
ghenv.Component.ExpireSolution(True) # noqa: F821
9399

94100
# check if the URL input has changed
95-
if sc.sticky['ply_url'] != i_ply_url:
96-
sc.sticky['ply_url'] = i_ply_url
97-
sc.sticky['status_message'] = "URL changed. Press Load"
98-
sc.sticky['thread_running'] = False
99-
sc.sticky['prev_load'] = False
101+
if sc.sticky[f'{prefix}_ply_url'] != i_ply_url:
102+
sc.sticky[f'{prefix}_ply_url'] = i_ply_url
103+
sc.sticky[f'{prefix}_status_message'] = "URL changed. Press Load"
104+
sc.sticky[f'{prefix}_thread_running'] = False
105+
sc.sticky[f'{prefix}_prev_load'] = False
100106

101107
# start importing if Load toggle is pressed and import thread is not already running
102-
if i_load and not sc.sticky['prev_load'] and not sc.sticky['thread_running']:
103-
sc.sticky['status_message'] = "Loading..."
104-
sc.sticky['thread_running'] = True
108+
if i_load and not sc.sticky[f'{prefix}_prev_load'] and not sc.sticky[f'{prefix}_thread_running']:
109+
sc.sticky[f'{prefix}_status_message'] = "Loading..."
110+
sc.sticky[f'{prefix}_thread_running'] = True
105111
threading.Thread(target=_import_job, args=(i_ply_url,), daemon=True).start()
106112

107-
sc.sticky['prev_load'] = i_load
108-
ghenv.Component.Message = sc.sticky.get('status_message', "") # noqa: F821
113+
sc.sticky[f'{prefix}_prev_load'] = i_load
114+
ghenv.Component.Message = sc.sticky.get(f'{prefix}_status_message', "") # noqa: F821
109115

110116
# output
111-
o_geometry = sc.sticky.get('imported_geom')
117+
o_geometry = sc.sticky.get(f'{prefix}_imported_geom')
112118

113119
return [o_geometry]

src/gh/components/DF_tcp_listener/code.py

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,22 @@
1212
class DFTCPListener(component):
1313
def RunScript(self,
1414
i_start: bool,
15-
i_load: bool,
1615
i_stop: bool,
17-
i_port: int,
18-
i_host: str):
16+
i_load: bool,
17+
i_host: str,
18+
i_port: int):
19+
20+
prefix = 'tcp'
1921

20-
# Sticky defaults
21-
sc.sticky.setdefault('server_sock', None)
22-
sc.sticky.setdefault('server_started', False)
23-
sc.sticky.setdefault('cloud_buffer_raw', [])
24-
sc.sticky.setdefault('latest_cloud', None)
25-
sc.sticky.setdefault('status_message', 'Waiting...')
26-
sc.sticky.setdefault('prev_start', False)
27-
sc.sticky.setdefault('prev_stop', False)
28-
sc.sticky.setdefault('prev_load', False)
29-
sc.sticky.setdefault('client_socks', []) # Track client sockets
22+
# Sticky initialization
23+
sc.sticky.setdefault(f'{prefix}_server_sock', None)
24+
sc.sticky.setdefault(f'{prefix}_server_started', False)
25+
sc.sticky.setdefault(f'{prefix}_cloud_buffer_raw', [])
26+
sc.sticky.setdefault(f'{prefix}_latest_cloud', None)
27+
sc.sticky.setdefault(f'{prefix}_status_message', 'Waiting..')
28+
sc.sticky.setdefault(f'{prefix}_prev_start', False)
29+
sc.sticky.setdefault(f'{prefix}_prev_stop', False)
30+
sc.sticky.setdefault(f'{prefix}_prev_load', False)
3031

3132
# Client handler
3233
def handle_client(conn):
@@ -35,7 +36,7 @@ def handle_client(conn):
3536
"""
3637
buf = b''
3738
with conn:
38-
while sc.sticky.get('server_started', False):
39+
while sc.sticky.get(f'{prefix}_server_started', False):
3940
try:
4041
chunk = conn.recv(4096)
4142
if not chunk:
@@ -48,7 +49,7 @@ def handle_client(conn):
4849
except Exception:
4950
continue
5051
if isinstance(raw, list) and all(isinstance(pt, list) and len(pt) == 6 for pt in raw):
51-
sc.sticky['cloud_buffer_raw'] = raw
52+
sc.sticky[f'{prefix}_cloud_buffer_raw'] = raw
5253
except Exception:
5354
break
5455

@@ -73,49 +74,49 @@ def start_server():
7374
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
7475
sock.bind((i_host, i_port))
7576
sock.listen(1)
76-
sc.sticky['server_sock'] = sock
77-
sc.sticky['server_started'] = True
78-
sc.sticky['status_message'] = f'Listening on {i_host}:{i_port}'
77+
sc.sticky[f'{prefix}_server_sock'] = sock
78+
sc.sticky[f'{prefix}_server_started'] = True
79+
sc.sticky[f'{prefix}_status_message'] = f'Listening on {i_host}:{i_port}'
7980
# Only accept one connection to keep it long-lived
8081
threading.Thread(target=server_loop, args=(sock,), daemon=True).start()
8182

8283
def stop_server():
83-
sock = sc.sticky.get('server_sock')
84+
sock = sc.sticky.get(f'{prefix}_server_sock')
8485
if sock:
8586
try:
8687
sock.close()
8788
except Exception:
8889
pass
89-
sc.sticky['server_sock'] = None
90-
sc.sticky['server_started'] = False
91-
sc.sticky['cloud_buffer_raw'] = []
92-
sc.sticky['status_message'] = 'Stopped'
90+
sc.sticky[f'{prefix}_server_sock'] = None
91+
sc.sticky[f'{prefix}_server_started'] = False
92+
sc.sticky[f'{prefix}_cloud_buffer_raw'] = []
93+
sc.sticky[f'{prefix}_status_message'] = 'Stopped'
9394

9495
# Start or stop server based on inputs
95-
if i_start and not sc.sticky['prev_start']:
96+
if i_start and not sc.sticky[f'{prefix}_prev_start']:
9697
start_server()
97-
if i_stop and not sc.sticky['prev_stop']:
98+
if i_stop and not sc.sticky[f'{prefix}_prev_stop']:
9899
stop_server()
99100

100-
# Load buffered points into PointCloud
101-
if i_load and not sc.sticky['prev_load']:
102-
raw = sc.sticky.get('cloud_buffer_raw', [])
101+
# Load buffered points into Rhino PointCloud
102+
if i_load and not sc.sticky[f'{prefix}_prev_load']:
103+
raw = sc.sticky.get(f'{prefix}_cloud_buffer_raw', [])
103104
if raw:
104105
pc = rg.PointCloud()
105106
for x, y, z, r, g, b in raw:
106107
pc.Add(rg.Point3d(x, y, z), sd.Color.FromArgb(int(r), int(g), int(b)))
107-
sc.sticky['latest_cloud'] = pc
108-
sc.sticky['status_message'] = f'Retrieved {pc.Count} pts'
108+
sc.sticky[f'{prefix}_latest_cloud'] = pc
109+
sc.sticky[f'{prefix}_status_message'] = f'Loaded pcd with {pc.Count} pts'
109110
else:
110-
sc.sticky['status_message'] = 'No data buffered'
111+
sc.sticky[f'{prefix}_status_message'] = 'No data buffered'
111112

112113
# Update previous states
113-
sc.sticky['prev_start'] = i_start
114-
sc.sticky['prev_stop'] = i_stop
115-
sc.sticky['prev_load'] = i_load
114+
sc.sticky[f'{prefix}_prev_start'] = i_start
115+
sc.sticky[f'{prefix}_prev_stop'] = i_stop
116+
sc.sticky[f'{prefix}_prev_load'] = i_load
116117

117118
# Update UI and output
118-
ghenv.Component.Message = sc.sticky['status_message'] # noqa: F821
119+
ghenv.Component.Message = sc.sticky[f'{prefix}_status_message'] # noqa: F821
119120

120-
o_cloud = sc.sticky['latest_cloud']
121+
o_cloud = sc.sticky[f'{prefix}_latest_cloud']
121122
return [o_cloud]

src/gh/components/DF_tcp_listener/metadata.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"nickname": "TCPIn",
44
"category": "diffCheck",
55
"subcategory": "IO",
6-
"description": "This component get data from a tcp sender",
6+
"description": "This component get point cloud data from a tcp sender",
77
"exposure": 4,
88
"instanceGuid": "61a9cc27-864d-4892-bd39-5d97dbccbefb",
99
"ghpython": {
@@ -26,9 +26,9 @@
2626
"typeHintID": "bool"
2727
},
2828
{
29-
"name": "i_load",
30-
"nickname": "i_load",
31-
"description": "Button to get the latest PCD from the buffer",
29+
"name": "i_stop",
30+
"nickname": "i_stop",
31+
"description": "Button to stop the server and release the port",
3232
"optional": true,
3333
"allowTreeAccess": true,
3434
"showTypeHints": true,
@@ -38,9 +38,9 @@
3838
"typeHintID": "bool"
3939
},
4040
{
41-
"name": "i_stop",
42-
"nickname": "i_stop",
43-
"description": "Stop the server and release the port",
41+
"name": "i_load",
42+
"nickname": "i_load",
43+
"description": "Button to get the latest PCD from the buffer",
4444
"optional": true,
4545
"allowTreeAccess": true,
4646
"showTypeHints": true,
@@ -50,35 +50,35 @@
5050
"typeHintID": "bool"
5151
},
5252
{
53-
"name": "i_port",
54-
"nickname": "i_port",
55-
"description": "The port for the connection",
53+
"name": "i_host",
54+
"nickname": "i_host",
55+
"description": "The host to use for the connection",
5656
"optional": true,
5757
"allowTreeAccess": true,
5858
"showTypeHints": true,
5959
"scriptParamAccess": "item",
6060
"wireDisplay": "default",
6161
"sourceCount": 0,
62-
"typeHintID": "int"
62+
"typeHintID": "str"
6363
},
6464
{
65-
"name": "i_host",
66-
"nickname": "i_host",
67-
"description": "The host for the connection",
65+
"name": "i_port",
66+
"nickname": "i_port",
67+
"description": "The port to use for the connection",
6868
"optional": true,
6969
"allowTreeAccess": true,
7070
"showTypeHints": true,
7171
"scriptParamAccess": "item",
7272
"wireDisplay": "default",
7373
"sourceCount": 0,
74-
"typeHintID": "str"
74+
"typeHintID": "int"
7575
}
7676
],
7777
"outputParameters": [
7878
{
7979
"name": "o_cloud",
8080
"nickname": "o_cloud",
81-
"description": "The pcd that was received.",
81+
"description": "The Rhino pcd that was received.",
8282
"optional": false,
8383
"sourceCount": 0,
8484
"graft": false

0 commit comments

Comments
 (0)