|
| 1 | +// Configuration field mapping |
| 2 | +const CONFIG_FIELDS = { |
| 3 | + // File paths |
| 4 | + 'properties-json': 'properties_json', |
| 5 | + 'base-scene-blendfile': 'base_scene_blendfile', |
| 6 | + 'output-scene-file': 'output_scene_file', |
| 7 | + |
| 8 | + // Directories |
| 9 | + 'shape-dir': 'shape_dir', |
| 10 | + 'material-dir': 'material_dir', |
| 11 | + 'output-image-dir': 'output_image_dir', |
| 12 | + 'output-scene-dir': 'output_scene_dir', |
| 13 | + 'masks-dir': 'masks_dir', |
| 14 | + 'enhanced-image-dir': 'enhanced_image_dir', |
| 15 | + |
| 16 | + // Rendering settings |
| 17 | + 'use-gpu': 'use_gpu', |
| 18 | + 'width': 'width', |
| 19 | + 'height': 'height', |
| 20 | + 'render-tile-size': 'render_tile_size' |
| 21 | +} |
| 22 | + |
1 | 23 | // Object Selector Functionality |
2 | 24 | let objectsData = {} |
3 | 25 | let selectedObjects = { |
@@ -25,6 +47,81 @@ async function initObjectSelectors() { |
25 | 47 | } |
26 | 48 | } |
27 | 49 |
|
| 50 | +// Configuration Management Functions |
| 51 | +async function loadConfiguration() { |
| 52 | + try { |
| 53 | + const response = await fetch('/api/config') |
| 54 | + const data = await response.json() |
| 55 | + |
| 56 | + if (data.success) { |
| 57 | + populateConfigurationForm(data.config) |
| 58 | + appendOutput("\n✅ Configuration loaded successfully\n") |
| 59 | + } else { |
| 60 | + console.error('Failed to load configuration:', data.error) |
| 61 | + appendOutput(`\n❌ Failed to load configuration: ${data.error}\n`) |
| 62 | + } |
| 63 | + } catch (error) { |
| 64 | + console.error('Error fetching configuration:', error) |
| 65 | + appendOutput(`\n❌ Error fetching configuration: ${error.message}\n`) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +function populateConfigurationForm(config) { |
| 70 | + // Iterate through all configured fields and populate them |
| 71 | + Object.entries(CONFIG_FIELDS).forEach(([fieldId, configKey]) => { |
| 72 | + const field = document.getElementById(fieldId) |
| 73 | + if (field && config[configKey] !== undefined) { |
| 74 | + if (field.type === 'checkbox') { |
| 75 | + field.checked = config[configKey] |
| 76 | + } else { |
| 77 | + field.value = config[configKey] |
| 78 | + } |
| 79 | + } |
| 80 | + }) |
| 81 | +} |
| 82 | + |
| 83 | +async function saveConfiguration(formData) { |
| 84 | + try { |
| 85 | + // Convert form data to config object using the mapping |
| 86 | + const configData = {} |
| 87 | + |
| 88 | + Object.entries(CONFIG_FIELDS).forEach(([fieldId, configKey]) => { |
| 89 | + const formKey = fieldId.replace(/-/g, '_') |
| 90 | + const value = formData[formKey] |
| 91 | + |
| 92 | + if (configKey === 'use_gpu') { |
| 93 | + configData[configKey] = value === 'on' |
| 94 | + } else if (value !== undefined && value !== '') { |
| 95 | + if (['width', 'height', 'render_tile_size'].includes(configKey)) { |
| 96 | + configData[configKey] = parseInt(value) |
| 97 | + } else { |
| 98 | + configData[configKey] = value |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + const response = await fetch('/api/config', { |
| 104 | + method: 'POST', |
| 105 | + headers: { |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + }, |
| 108 | + body: JSON.stringify(configData) |
| 109 | + }) |
| 110 | + |
| 111 | + const data = await response.json() |
| 112 | + |
| 113 | + if (data.success) { |
| 114 | + appendOutput("✅ Configuration saved successfully\n") |
| 115 | + } else { |
| 116 | + console.error('Failed to save configuration:', data.error) |
| 117 | + appendOutput(`❌ Failed to save configuration: ${data.error}\n`) |
| 118 | + } |
| 119 | + } catch (error) { |
| 120 | + console.error('Error saving configuration:', error) |
| 121 | + appendOutput(`❌ Error saving configuration: ${error.message}\n`) |
| 122 | + } |
| 123 | +} |
| 124 | + |
28 | 125 | function showObjectLoadError() { |
29 | 126 | const singleToggle = document.getElementById('objects-single-toggle') |
30 | 127 | const multipleToggle = document.getElementById('objects-multiple-toggle') |
@@ -224,11 +321,14 @@ const renderForms = document.querySelectorAll(".render-form") |
224 | 321 | const singleForm = document.getElementById("single-render-form") |
225 | 322 | const multipleForm = document.getElementById("multiple-render-form") |
226 | 323 | const backgroundForm = document.getElementById("background-generation-form") |
| 324 | +const configurationForm = document.getElementById("configuration-form") |
227 | 325 | const outputDiv = document.getElementById("output") |
228 | 326 | const outputStatus = document.getElementById("output-status") |
229 | 327 | const singleSubmitBtn = document.getElementById("single-submit-btn") |
230 | 328 | const multipleSubmitBtn = document.getElementById("multiple-submit-btn") |
231 | 329 | const backgroundSubmitBtn = document.getElementById("background-submit-btn") |
| 330 | + |
| 331 | +const saveConfigBtn = document.getElementById("save-config-btn") |
232 | 332 | const stopBtn = document.getElementById("stop-btn") |
233 | 333 |
|
234 | 334 | // WebSocket connection |
@@ -505,6 +605,27 @@ backgroundForm.addEventListener("submit", (e) => { |
505 | 605 | } |
506 | 606 | }) |
507 | 607 |
|
| 608 | +// Configuration form event listeners |
| 609 | +configurationForm.addEventListener("submit", async (e) => { |
| 610 | + e.preventDefault() |
| 611 | + |
| 612 | + const submitBtn = e.target.querySelector('button[type="submit"]') |
| 613 | + const originalHTML = submitBtn.innerHTML |
| 614 | + |
| 615 | + // Show loading state |
| 616 | + submitBtn.disabled = true |
| 617 | + submitBtn.innerHTML = '<span class="loading"></span>Saving configuration...' |
| 618 | + |
| 619 | + const formData = new FormData(e.target) |
| 620 | + const data = Object.fromEntries(formData.entries()) |
| 621 | + |
| 622 | + await saveConfiguration(data) |
| 623 | + |
| 624 | + // Re-enable submit button |
| 625 | + submitBtn.disabled = false |
| 626 | + submitBtn.innerHTML = originalHTML |
| 627 | +}) |
| 628 | + |
508 | 629 | // Add input event listeners for real-time validation feedback |
509 | 630 | document.querySelectorAll("input, select").forEach((field) => { |
510 | 631 | field.addEventListener("input", () => { |
@@ -558,6 +679,5 @@ stopBtn.addEventListener("click", () => { |
558 | 679 | document.addEventListener('DOMContentLoaded', () => { |
559 | 680 | initWebSocket() |
560 | 681 | initObjectSelectors() |
| 682 | + loadConfiguration() |
561 | 683 | }) |
562 | | - |
563 | | -console.log("🎬 Blender Render Control initialized successfully!") |
|
0 commit comments