-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-client.js
More file actions
220 lines (198 loc) · 5.24 KB
/
test-client.js
File metadata and controls
220 lines (198 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env node
/**
* DazScriptServer Node.js Client Example
*
* This example demonstrates how to connect to DazScriptServer from Node.js.
*
* Installation:
* npm install node-fetch
*
* Usage:
* node test-client.js
*/
const fs = require('fs');
const path = require('path');
const os = require('os');
// Use node-fetch for HTTP requests (built-in fetch in Node 18+)
let fetch;
try {
// Try built-in fetch first (Node 18+)
fetch = globalThis.fetch;
if (!fetch) {
// Fall back to node-fetch
fetch = require('node-fetch');
}
} catch (e) {
console.error('Error: This script requires node-fetch.');
console.error('Install with: npm install node-fetch');
process.exit(1);
}
// Server configuration
const SERVER_URL = 'http://127.0.0.1:18811';
const TOKEN_PATH = path.join(os.homedir(), '.daz3d', 'dazscriptserver_token.txt');
/**
* Read API token from file
*/
function readToken() {
try {
const token = fs.readFileSync(TOKEN_PATH, 'utf8').trim();
return token;
} catch (error) {
console.error(`Error reading token from ${TOKEN_PATH}:`, error.message);
console.error('Make sure DazScriptServer has been started at least once to generate the token.');
process.exit(1);
}
}
/**
* Check server status
*/
async function checkStatus() {
try {
const response = await fetch(`${SERVER_URL}/status`);
const data = await response.json();
console.log('Server Status:', data);
return data.running;
} catch (error) {
console.error('Error connecting to server:', error.message);
console.error('Make sure DazScriptServer is running in DAZ Studio.');
return false;
}
}
/**
* Check server health
*/
async function checkHealth() {
try {
const response = await fetch(`${SERVER_URL}/health`);
const data = await response.json();
console.log('\nServer Health:', data);
} catch (error) {
console.error('Error getting health:', error.message);
}
}
/**
* Execute a DazScript
*/
async function executeScript(token, script, args = {}) {
try {
const response = await fetch(`${SERVER_URL}/execute`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Token': token
},
body: JSON.stringify({
script: script,
args: args
})
});
const data = await response.json();
if (!response.ok) {
console.error(`\nHTTP ${response.status} Error:`, data.error);
return null;
}
return data;
} catch (error) {
console.error('Error executing script:', error.message);
return null;
}
}
/**
* Main execution
*/
async function main() {
console.log('=== DazScriptServer Node.js Client Example ===\n');
// Check if server is running
const isRunning = await checkStatus();
if (!isRunning) {
return;
}
// Check server health
await checkHealth();
// Read API token
const token = readToken();
console.log(`\nAPI Token: ${token.substring(0, 8)}...${token.substring(token.length - 8)}`);
// Example 1: Simple script
console.log('\n--- Example 1: Simple Script ---');
let result = await executeScript(token, 'return "Hello from DazScript!";');
if (result) {
console.log('Success:', result.success);
console.log('Result:', result.result);
console.log('Request ID:', result.request_id);
}
// Example 2: Script with arguments
console.log('\n--- Example 2: Script with Arguments ---');
result = await executeScript(
token,
`
var args = getArguments()[0];
var message = args.message || "default";
var count = args.count || 1;
return message + " (x" + count + ")";
`,
{ message: "Node.js calling DAZ", count: 3 }
);
if (result) {
console.log('Success:', result.success);
console.log('Result:', result.result);
}
// Example 3: Get scene information
console.log('\n--- Example 3: Get Scene Info ---');
result = await executeScript(
token,
`
var scene = Scene.getPrimarySelection();
if (scene) {
return {
name: scene.name,
type: scene.className(),
visible: scene.isVisible()
};
} else {
return "No selection";
}
`
);
if (result) {
console.log('Success:', result.success);
console.log('Result:', JSON.stringify(result.result, null, 2));
}
// Example 4: Script with output (print statements)
console.log('\n--- Example 4: Script with Output ---');
result = await executeScript(
token,
`
print("Starting calculation...");
var sum = 0;
for (var i = 1; i <= 5; i++) {
sum += i;
print("Step " + i + ": sum = " + sum);
}
print("Calculation complete!");
return sum;
`
);
if (result) {
console.log('Success:', result.success);
console.log('Result:', result.result);
console.log('Output:', result.output);
}
// Example 5: Error handling
console.log('\n--- Example 5: Error Handling ---');
result = await executeScript(
token,
'return nonExistentVariable;' // This will cause an error
);
if (result) {
console.log('Success:', result.success);
if (!result.success) {
console.log('Error:', result.error);
}
}
console.log('\n=== All examples completed ===');
}
// Run main function
main().catch(error => {
console.error('Unexpected error:', error);
process.exit(1);
});