-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdazrenderqueue.dsa
More file actions
515 lines (431 loc) · 21.9 KB
/
dazrenderqueue.dsa
File metadata and controls
515 lines (431 loc) · 21.9 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
(function(){
// Persistent auto-save file so it survives PC reboots and DAZ restarts
var sStateFile = App.getAppDataPath() + "/RenderQueueState.json";
var aQueue =[];
var oState = {
sceneIdx: 0,
frameIdx: 0,
smoothWait: 3,
shutdown: false
};
var bCancelQueue = false;
function getInt( wWidget, nDef ){
try {
var n = parseInt( wWidget.text );
if( isNaN(n) ) return nDef;
return n;
} catch(e) { return nDef; }
}
function getItemIndex( targetItem ) {
if(!targetItem) return -1;
var idx = parseInt(targetItem.text(1));
return isNaN(idx) ? -1 : idx;
}
function loadLastDir(){
var oSettings = new DzAppSettings("RenderQueue");
return oSettings.getStringValue("LastOpenDir", App.getDocumentsPath());
}
function saveLastDir( sPath ){
var oSettings = new DzAppSettings("RenderQueue");
oSettings.setStringValue("LastOpenDir", sPath);
}
function getJobStatusStr(job) {
if(job.done) return " [DONE]";
return job.override ? " [Animation]" : " [Current Frame]";
}
var sLastOpenDir = loadLastDir();
var wDlg = new DzDialog();
wDlg.caption = "Render Queue";
wDlg.width = 750; wDlg.height = 650;
var wLayout = new DzVBoxLayout(wDlg);
var wGrpOut = new DzGroupBox(wDlg);
wGrpOut.title = "Render Output Directory";
var wOutLayout = new DzGridLayout(wGrpOut);
var wLineOut = new DzLineEdit(wGrpOut);
var oInitSettings = new DzAppSettings("RenderQueue");
wLineOut.text = oInitSettings.getStringValue("OutputDir", App.getDocumentsPath() + "/RenderQueue/");
var wBtnOutBrowse = new DzPushButton(wGrpOut);
wBtnOutBrowse.text = "Browse...";
wBtnOutBrowse.maxWidth = 80;
wOutLayout.addWidget(wLineOut, 0, 0);
wOutLayout.addWidget(wBtnOutBrowse, 0, 1);
wLayout.addWidget(wGrpOut);
wBtnOutBrowse.clicked.connect(function(){
var sDir = FileDialog.doDirectoryDialog("Select Output Directory", "", wLineOut.text);
if(sDir){
var sFormatted = sDir.replace(/\\/g, "/");
if(!sFormatted.endsWith("/")) sFormatted += "/";
wLineOut.text = sFormatted;
var oSettings = new DzAppSettings("RenderQueue");
oSettings.setStringValue("OutputDir", sFormatted);
}
});
wLineOut.textChanged.connect(function(text){
var oSettings = new DzAppSettings("RenderQueue");
oSettings.setStringValue("OutputDir", text);
});
var lblInfo = new DzLabel(wDlg);
lblInfo.text = "Queue List (Double-Click an item to change Camera/Frames):";
wLayout.addWidget(lblInfo);
var wList = new DzListView(wDlg);
wList.addColumn("Queue Items");
wList.addColumn("ID");
wList.setColumnWidth(0, 700);
wList.setColumnWidth(1, 0);
wList.allColumnsShowFocus = true;
wLayout.addWidget(wList);
function refreshListUI() {
wList.clear();
for(var i=0; i<aQueue.length; i++) {
var wItem = new DzListViewItem(wList);
wItem.setText(0, aQueue[i].path + getJobStatusStr(aQueue[i]));
wItem.setText(1, i.toString());
}
}
function updateListRow(idx, textStr) {
var curr = wList.firstChild();
while(curr) {
if(parseInt(curr.text(1)) === idx) {
curr.setText(0, textStr);
return;
}
curr = curr.nextSibling();
}
}
var wTopBtnContainer = new DzWidget(wDlg);
var wTopBtnLayout = new DzGridLayout(wTopBtnContainer);
var wBtnAdd = new DzPushButton(wTopBtnContainer); wBtnAdd.text = "+ ADD SCENES / JSON"; wBtnAdd.minHeight = 40;
var wBtnRemove = new DzPushButton(wTopBtnContainer); wBtnRemove.text = "- REMOVE SELECTED"; wBtnRemove.minHeight = 40;
var wBtnExport = new DzPushButton(wTopBtnContainer); wBtnExport.text = "EXPORT SESSION"; wBtnExport.minHeight = 40;
wTopBtnLayout.addWidget(wBtnAdd, 0, 0);
wTopBtnLayout.addWidget(wBtnRemove, 0, 1);
wTopBtnLayout.addWidget(wBtnExport, 0, 2);
wLayout.addWidget(wTopBtnContainer);
var wGrpGlob = new DzGroupBox(wDlg);
wGrpGlob.title = "Global Execution Settings";
var wGlobLayout = new DzGridLayout(wGrpGlob);
var lblWait = new DzLabel(wGrpGlob); lblWait.text = "Smoothing Wait (Seconds):";
var wLineWait = new DzLineEdit(wGrpGlob); wLineWait.text = "3";
wGlobLayout.addWidget(lblWait, 0, 0); wGlobLayout.addWidget(wLineWait, 0, 1);
var wChkShutdown = new DzCheckBox(wGrpGlob);
wChkShutdown.text = "Shutdown PC when queue finishes";
wGlobLayout.addWidget(wChkShutdown, 1, 0, 1, 2);
wLayout.addWidget(wGrpGlob);
var wActionContainer = new DzWidget(wDlg);
var wActionLayout = new DzGridLayout(wActionContainer);
var wBtnRun = new DzPushButton(wActionContainer); wBtnRun.text = "START RENDER QUEUE"; wBtnRun.minHeight = 50;
wBtnRun.styleSheet = "background-color: #2E7D32; color: white; font-weight: bold; font-size: 14px;";
var wBtnCancel = new DzPushButton(wActionContainer); wBtnCancel.text = "STOP QUEUE"; wBtnCancel.minHeight = 50;
wBtnCancel.styleSheet = "background-color: #C62828; color: white; font-weight: bold; font-size: 14px;";
var wBtnReset = new DzPushButton(wActionContainer); wBtnReset.text = "RESET QUEUE"; wBtnReset.minHeight = 50;
wActionLayout.addWidget(wBtnRun, 0, 0);
wActionLayout.addWidget(wBtnCancel, 0, 1);
wActionLayout.addWidget(wBtnReset, 0, 2);
wLayout.addWidget(wActionContainer);
function syncState() {
if(aQueue.length === 0) {
var oFile = new DzFile(sStateFile); if(oFile.exists()) oFile.remove();
oState.sceneIdx = 0; oState.frameIdx = 0;
} else { saveState(oState.sceneIdx, oState.frameIdx); }
}
// Auto-save triggers when global settings change
wLineWait.textChanged.connect(function(){ syncState(); });
wChkShutdown.toggled.connect(function(){ syncState(); });
wBtnReset.clicked.connect(function(){
if( MessageBox.question("Are you sure you want to clear the queue and reset all progress?", "Confirm Reset", "&Yes", "&No") == 0 ){
aQueue =[];
oState.sceneIdx = 0; oState.frameIdx = 0;
var oFile = new DzFile(sStateFile); if(oFile.exists()) oFile.remove();
refreshListUI();
wBtnRun.text = "START RENDER QUEUE";
print("--- RENDER QUEUE: Cleared and Reset ---");
}
});
wBtnCancel.clicked.connect(function(){
if( wBtnRun.enabled == false ) {
bCancelQueue = true; wBtnCancel.text = "STOPPING...";
var oRenderMgr = App.getRenderMgr(); if (oRenderMgr.isRendering()) oRenderMgr.cancelRender();
}
});
wBtnRemove.clicked.connect(function(){
if( wBtnRun.enabled == false ) return;
var wItem = wList.currentItem(); if(!wItem) return;
var idx = getItemIndex(wItem);
if(idx > -1) { aQueue.splice(idx, 1); refreshListUI(); syncState(); }
});
wBtnExport.clicked.connect(function(){
if(aQueue.length === 0) return;
sLastOpenDir = loadLastDir();
var sSavePath = FileDialog.doFileDialog(false, "Save Queue Session", sLastOpenDir, "JSON Queue (*.json)");
if(!sSavePath) return;
var oFile = new DzFile(sSavePath);
if(oFile.open(DzFile.WriteOnly | DzFile.Truncate)){
oState.smoothWait = getInt(wLineWait, 3);
oState.shutdown = wChkShutdown.checked;
var oData = { isNativeSession: true, queue: aQueue, state: oState };
oFile.write(JSON.stringify(oData, null, 4)); oFile.close();
saveLastDir(new DzFileInfo(sSavePath).path());
}
});
wBtnAdd.clicked.connect(function(){
sLastOpenDir = loadLastDir();
var aPaths = FileDialog.getOpenFileNames( sLastOpenDir, "Supported Files (*.duf *.json);;DAZ Scene (*.duf);;JSON Queue (*.json)", "Select Scenes or JSON" );
if( !aPaths || aPaths.length === 0 ) return;
var oFileInfo = new DzFileInfo(aPaths[0]); sLastOpenDir = oFileInfo.path(); saveLastDir(sLastOpenDir);
for (var fIdx = 0; fIdx < aPaths.length; fIdx++) {
var sFile = aPaths[fIdx].replace(/\\/g, "/");
if( sFile.toLowerCase().endsWith(".json") ){
var oFile = new DzFile(sFile);
if( oFile.open( DzFile.ReadOnly ) ){
try {
var sData = oFile.read(); var parsedData = JSON.parse(sData);
if(parsedData.isNativeSession) {
aQueue = parsedData.queue; oState = parsedData.state;
refreshListUI(); wLineWait.text = oState.smoothWait.toString();
wChkShutdown.checked = oState.shutdown || false;
} else {
var tempArr = parsedData.scenes || (Array.isArray(parsedData) ? parsedData : [parsedData]);
for(var i=0; i<tempArr.length; i++){
var scn = tempArr[i]; var p = scn.file || scn.path || "";
var c = (scn.sceneAttrs && scn.sceneAttrs.cameraCurrent) ? scn.sceneAttrs.cameraCurrent : (scn.cam || "");
var isOver = scn.override || false;
if( p != "" ) aQueue.push({ path: p, cam: c, override: isOver, done: false });
}
}
} catch(e) {
print("--- ERROR: Could not parse JSON file: " + sFile);
}
oFile.close();
}
} else { aQueue.push({ path: sFile, cam: "", override: false, done: false }); }
}
refreshListUI(); syncState();
});
function openEditDialog( item ) {
if( !item ) return;
var idx = getItemIndex( item );
if(idx === -1) return;
var job = aQueue[idx];
var wEditDlg = new DzBasicDialog();
wEditDlg.caption = "Scene Options";
wEditDlg.width = 400;
wEditDlg.height = 150;
var wContainer = new DzWidget(wEditDlg);
var wLyt = new DzVBoxLayout(wContainer);
wLyt.margin = 0;
var sCamHelp = "<b>Target Camera Options</b><br><br>" +
"• <b>Leave Blank:</b> Renders the saved active camera.<br>" +
"• <b>Enter Name:</b> Renders a specific camera by name.<br>" +
"• <b>Type 'ALL':</b> Renders every custom camera in the scene.";
var sFrameHelp = "<b>Render Saved Animation Range?</b><br><br>" +
"• <b>Unchecked:</b> Renders only the single frame currently active on the timeline.<br>" +
"• <b>Checked:</b> Renders the full animation sequence based on the scene's Render Settings.";
var lblCam = new DzLabel(wContainer);
lblCam.text = "Target Camera Name:";
lblCam.whatsThis = sCamHelp;
wLyt.addWidget(lblCam);
var wEditCam = new DzLineEdit(wContainer);
wEditCam.text = job.cam;
wEditCam.whatsThis = sCamHelp;
wLyt.addWidget(wEditCam);
var wEditChk = new DzCheckBox(wContainer);
wEditChk.text = "Render Saved Animation Range?";
wEditChk.whatsThis = sFrameHelp;
wEditChk.checked = job.override;
wLyt.addWidget(wEditChk);
wLyt.addStretch(1);
wEditDlg.addWidget(wContainer);
if( wEditDlg.exec() ){
job.cam = wEditCam.text;
job.override = wEditChk.checked;
item.setText(0, job.path + getJobStatusStr(job));
syncState();
}
}
wList.doubleClicked.connect( openEditDialog );
function saveState( currentScene, currentFrameRel ){
oState.sceneIdx = currentScene || 0;
oState.frameIdx = currentFrameRel || 0;
oState.smoothWait = getInt(wLineWait, 3);
oState.shutdown = wChkShutdown.checked;
var oFile = new DzFile(sStateFile); if( oFile.open( DzFile.WriteOnly | DzFile.Truncate ) ){
oFile.write( JSON.stringify({ queue: aQueue, state: oState }, null, 4) ); oFile.close();
}
}
function loadState(){
var oFile = new DzFile(sStateFile);
if( oFile.exists() && oFile.open( DzFile.ReadOnly ) ){
var bSuccess = false;
try {
var oData = JSON.parse( oFile.read() );
aQueue = oData.queue;
oState = oData.state;
wLineWait.text = oState.smoothWait.toString();
wChkShutdown.checked = oState.shutdown || false;
refreshListUI();
bSuccess = true;
} catch(e) {
print("Error parsing queue state: " + e);
}
oFile.close();
return bSuccess;
}
return false;
}
wBtnRun.clicked.connect(function(){
var sWorkingDir = wLineOut.text.replace(/\\/g, "/");
if( sWorkingDir.trim() === "" || sWorkingDir.trim() === "/" ) {
MessageBox.warning("Please specify a valid Render Output Directory.", "Missing Output Directory", "&OK");
return;
}
if( !sWorkingDir.endsWith("/") ) sWorkingDir += "/";
var oDir = new DzDir(sWorkingDir);
if( !oDir.exists() ){ oDir.mkpath(sWorkingDir); }
var oRenderMgr = App.getRenderMgr();
var oContentMgr = App.getContentMgr();
var nWaitMs = getInt(wLineWait, 3) * 1000;
// Listener to catch native DAZ render cancelation
var bNativeRenderSuccess = true;
var onRenderFinish = function( bSuccess ) {
bNativeRenderSuccess = bSuccess;
};
oRenderMgr.renderFinished.connect( onRenderFinish );
bCancelQueue = false;
wBtnRun.enabled = false;
wBtnRun.text = "RENDERING...";
for( var s = oState.sceneIdx; s < aQueue.length; s++ ){
if(bCancelQueue) { break; }
var job = aQueue[s];
if(job.done) continue;
var sCurrentFile = Scene.getFilename().replace(/\\/g, "/");
var sTargetFile = job.path.replace(/\\/g, "/");
var bSceneReady = false;
if (sCurrentFile == sTargetFile) {
bSceneReady = true;
} else {
updateListRow(s, job.path + " [LOADING...]");
Scene.clear();
if( oContentMgr.openNativeFile( job.path, false ) ) {
bSceneReady = true;
} else {
print("--- ERROR: Failed to load scene file: " + job.path);
}
}
if( bSceneReady ){
var oOpt = oRenderMgr.getRenderOptions();
var sOrigRenderImgFilename = oOpt.renderImgFilename;
var nOrigRenderImgToId = oOpt.renderImgToId;
var bOrigIsCurrentFrameRender = oOpt.isCurrentFrameRender;
var nOrigTime = Scene.getTime();
var oActiveCam = MainWindow.getViewportMgr().getActiveViewport().get3DViewport().getCamera();
var aCamsToRender = [];
if( job.cam.toUpperCase() === "ALL" ) {
var allCams = Scene.getCameraList();
var aDefaultCams = ["Perspective View", "Front View", "Left View", "Right View", "Top View", "Bottom View", "Back View"];
for (var cIdx=0; cIdx < allCams.length; cIdx++) {
if(aDefaultCams.indexOf(allCams[cIdx].name) === -1) {
aCamsToRender.push(allCams[cIdx]);
}
}
if(aCamsToRender.length === 0 && oActiveCam) aCamsToRender.push(oActiveCam);
} else if( job.cam != "" ){
var oCamNode = Scene.findNodeByLabel(job.cam);
if(oCamNode) {
aCamsToRender.push(oCamNode);
} else {
if(oActiveCam) aCamsToRender.push(oActiveCam);
}
} else {
if(oActiveCam) aCamsToRender.push(oActiveCam);
}
var nTick = Scene.getTimeStep();
var nStartFrame = 0;
var nEndFrame = 0;
var oPlayRange = Scene.getPlayRange();
if (job.override) {
nStartFrame = Math.round(oPlayRange.start / nTick);
nEndFrame = Math.round(oPlayRange.end / nTick);
} else {
nStartFrame = Math.round(Scene.getTime() / nTick);
nEndFrame = nStartFrame;
}
var nTotalFrames = (nEndFrame - nStartFrame) + 1;
for( var f = oState.frameIdx; f < nTotalFrames; f++ ){
if(bCancelQueue) { break; }
var nAbsFrame = nStartFrame + f;
Scene.setTime( nAbsFrame * nTick ); Scene.update();
var nStartWait = new Date().getTime();
while( (new Date().getTime() - nStartWait) < nWaitMs ){
processEvents();
if(bCancelQueue) break;
}
if(bCancelQueue) { saveState(s, f); break; }
for( var c = 0; c < aCamsToRender.length; c++ ) {
var currentCam = aCamsToRender[c];
MainWindow.getViewportMgr().getActiveViewport().get3DViewport().setCamera(currentCam);
var sCamSafeName = currentCam.getLabel().replace(/[^a-z0-9_-]/gi, '_');
var sProgressText = "Scene " + (s+1) + "/" + aQueue.length + " (F: " + (f+1) + "/" + nTotalFrames + ") [Cam: " + currentCam.getLabel() + "]";
updateListRow(s, job.path + " [" + sProgressText + "]");
print(sProgressText);
var sSceneName = job.path.substring( job.path.lastIndexOf("/")+1, job.path.lastIndexOf(".") );
var sOutFile = "";
if (job.cam.toUpperCase() === "ALL" && aCamsToRender.length > 1) {
sOutFile = (nTotalFrames > 1) ? (sWorkingDir + sSceneName + "/" + sCamSafeName + "/" + sSceneName + "_" + sCamSafeName + "_Frame" + ("0000" + nAbsFrame).slice(-4) + ".png") : (sWorkingDir + sSceneName + "_" + sCamSafeName + ".png");
if (nTotalFrames > 1) { var oD = new DzDir(sWorkingDir + sSceneName + "/" + sCamSafeName + "/"); if(!oD.exists()) oD.mkpath(oD.path()); }
} else {
sOutFile = (nTotalFrames > 1) ? (sWorkingDir + sSceneName + "/" + sSceneName + "_Frame" + ("0000" + nAbsFrame).slice(-4) + ".png") : (sWorkingDir + sSceneName + ".png");
if (nTotalFrames > 1) { var oD = new DzDir(sWorkingDir + sSceneName + "/"); if(!oD.exists()) oD.mkpath(oD.path()); }
}
var oCheckFile = new DzFileInfo(sOutFile);
if (oCheckFile.exists() && oCheckFile.size() > 0) {
continue;
}
oOpt.renderImgToId = DzRenderOptions.DirectToFile;
oOpt.renderImgFilename = sOutFile;
oOpt.isCurrentFrameRender = true;
bNativeRenderSuccess = true;
oRenderMgr.doRender(oOpt);
while( oRenderMgr.isRendering() ){
processEvents();
sleep(500);
}
// Check if the user aborted via the DAZ UI
if ( bNativeRenderSuccess == false ) {
bCancelQueue = true;
wBtnCancel.text = "STOPPING...";
print("--- DAZ Native Cancel Detected. Aborting Queue ---");
}
if(bCancelQueue) break;
}
if(bCancelQueue) { saveState(s, f); break; }
}
oOpt.renderImgFilename = sOrigRenderImgFilename;
oOpt.renderImgToId = nOrigRenderImgToId;
oOpt.isCurrentFrameRender = bOrigIsCurrentFrameRender;
Scene.setTime( nOrigTime );
if(oActiveCam) {
MainWindow.getViewportMgr().getActiveViewport().get3DViewport().setCamera(oActiveCam);
}
Scene.update();
if(bCancelQueue) break;
oState.frameIdx = 0; job.done = true; updateListRow(s, job.path + " [DONE]"); syncState();
} else {
updateListRow(s, job.path + "[ERROR - FAILED TO LOAD]");
}
}
oRenderMgr.renderFinished.disconnect( onRenderFinish );
wBtnRun.enabled = true; wBtnRun.text = "START RENDER QUEUE"; wBtnCancel.text = "STOP QUEUE";
if(!bCancelQueue) {
var oFile = new DzFile(sStateFile); if(oFile.exists()) oFile.remove();
if (wChkShutdown.checked) {
var aArgs = ["cmd", "/c", "shutdown", "/s", "/t", "5"];
var oProc = new DzProcess(aArgs);
oProc.start();
} else {
MessageBox.information("Render queue complete!", "Success", "&OK");
}
}
});
if( loadState() ) wBtnRun.text = (oState.sceneIdx >= aQueue.length) ? "START RENDER QUEUE" : "RESUME (Scene " + (oState.sceneIdx+1) + ")";
wDlg.exec();
})();