1+ #!/usr/bin/env python3
2+ """
3+ Quick script to connect to MF THREE scanner, open a project, and run alignment.
4+ """
5+
6+ from three .scanner import Scanner
7+ from three .MF .V3 import Task , TaskState
8+ from three .MF .V3 .Settings .Align import Align
9+ from three .MF .V3 .Descriptors .Project import Project
10+ import time
11+
12+ def main ():
13+ # Callback functions for handling scanner events
14+ def on_task (task : Task ):
15+ """Handle task updates (progress, completion, errors)"""
16+ if task .Progress :
17+ print (f"Task { task .Type } - Progress: { task .Progress .current } /{ task .Progress .total } " )
18+ elif task .State == TaskState .Completed .value :
19+ print (f"Task { task .Type } completed successfully" )
20+ elif task .State == TaskState .Failed .value :
21+ print (f"Task { task .Type } failed: { task .Error } " )
22+
23+ def on_message (message ):
24+ """Handle general system messages"""
25+ print (f"Message received: { message } " )
26+
27+ def on_buffer (descriptor , buffer_data ):
28+ """Handle buffer data (images, scans, etc.)"""
29+ print (f"Buffer received: { descriptor .Size } bytes" )
30+
31+ # Create scanner instance
32+ scanner = Scanner (
33+ OnTask = on_task ,
34+ OnMessage = on_message ,
35+ OnBuffer = on_buffer
36+ )
37+
38+ try :
39+ # Connect to scanner (replace with your scanner's IP if not using Zeroconf)
40+ print ("Connecting to scanner..." )
41+ scanner .Connect ("ws://matterandform.local:8081" , timeoutSec = 10 )
42+ print ("Connected successfully!" )
43+
44+ # List available projects
45+ print ("\n Listing projects..." )
46+ projects_task = scanner .list_projects ()
47+
48+ if projects_task .Error :
49+ print (f"Error listing projects: { projects_task .Error } " )
50+ return
51+
52+ if not projects_task .Output or len (projects_task .Output ) == 0 :
53+ print ("No projects found. Please create a project first." )
54+ return
55+
56+ # Display available projects
57+ print ("Available projects:" )
58+ for i , project_data in enumerate (projects_task .Output ):
59+ project = Project .Brief (** project_data )
60+ print (f" { i } : { project .name } (Index: { project .index } )" )
61+
62+ # Select the first project (or modify to select a specific one)
63+ selected_project_index = 16
64+ selected_project = Project .Brief (** projects_task .Output [selected_project_index ])
65+ print (f"\n Selected project: { selected_project .name } " )
66+
67+ # Open the selected project
68+ print ("Opening project..." )
69+ open_task = scanner .open_project (selected_project .index )
70+
71+ if open_task .Error :
72+ print (f"Error opening project: { open_task .Error } " )
73+ return
74+
75+ print ("Project opened successfully!" )
76+
77+ list_groups = scanner .list_groups ()
78+ for i , group_data in enumerate (list_groups .Output ['groups' ]):
79+ group = Project .Group (** group_data )
80+ print (f"Group { i } : { group .name } (Index: { group .index } )" )
81+
82+
83+ # Run alignment on the project
84+ print ("Starting alignment..." )
85+ align_task = scanner .align (19 , 14 , rough = Align .Rough (method = Align .Rough .Method .Ransac ))
86+
87+ if align_task .Error :
88+ print (f"Error running alignment: { align_task .Error } " )
89+ return
90+
91+ print ("Alignment completed successfully!" )
92+
93+ except Exception as e :
94+ print (f"Error: { e } " )
95+
96+ finally :
97+ # Clean up connection
98+ if scanner .IsConnected ():
99+ print ("Disconnecting..." )
100+ scanner .Disconnect ()
101+ print ("Disconnected." )
102+
103+ if __name__ == "__main__" :
104+ main ()
0 commit comments