File size: 36,439 Bytes
34097e9 |
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 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 |
const api = require('../api')
const html_manip = require('../html_manip')
const selection = require('../../selection')
const note = require('../notification')
const controlnet_preset = require('../presets/controlnet_preset')
const preset = require('../presets/preset')
const Enum = require('../../enum')
const event = require('../event')
// const g_controlnet_max_supported_models = 3
let g_controlnet_presets
let g_module_detail
class ControlNetUnit {
static {}
static resetUnit(index) {
const controlnet_unit_default = {
module: null,
model: null,
weight: 1.0,
resize_mode: null,
lowvram: null,
processor_res: null,
threshold_a: null,
threshold_b: null,
guidance_start: 0,
guidance_end: 1,
guessmode: null,
}
this.setUnit(index, controlnet_unit_default)
}
static resetUnits() {
for (let i = 0; i < g_controlnet_max_models; ++i) {
this.resetUnit(i)
}
}
static getUnit(index) {
const controlnet_unit = {
module: this.getModule(index),
model: this.getModel(index),
weight: this.getWeight(index),
resize_mode: null,
lowvram: null,
processor_res: null,
threshold_a: null,
threshold_b: null,
guidance_start: this.getGuidanceStrengthStart(index),
guidance_end: this.getGuidanceStrengthEnd(index),
guessmode: null,
}
return controlnet_unit
}
static setUnit(index, unit_settings) {
const controlnet_unit_setters = {
module: this.setModule,
model: this.setModel,
weight: this.setWeight,
resize_mode: null,
lowvram: null,
processor_res: null,
threshold_a: null,
threshold_b: null,
guidance_start: this.setGuidanceStrengthStart,
guidance_end: this.setGuidanceStrengthEnd,
guessmode: null,
}
for (const [name, value] of Object.entries(unit_settings)) {
try {
if (
controlnet_unit_setters.hasOwnProperty(name)
//&& value.toString() // check if it has a value, null return error; undefine return error; 0 pass
) {
// if (value) {
const setter = controlnet_unit_setters[name]
setter(index, value)
// }
}
} catch (e) {
console.warn(e)
}
}
}
static getUnits() {
const controlnet_units = {}
for (let i = 0; i < g_controlnet_max_models; ++i) {
controlnet_units[i] = this.getUnit(i)
}
return controlnet_units
}
static setUnits(controlnet_units) {
for (const [index, unit] of Object.entries(controlnet_units)) {
try {
this.setUnit(index, unit)
} catch (e) {
console.warn(e)
}
}
}
static doesUnitExist(index) {
//TODO: check if controlnet unit exist
if (index >= 0) {
}
}
static getModule(index) {
const module = getSelectedModule(index)
return module
}
static setModule(index, module_item) {
try {
const module_menu_element = controlnetElement(
index,
'.mModulesMenuControlNet_'
)
html_manip.selectMenuItemByElement(module_menu_element, module_item)
// module_menu_element.dispatchEvent(new Event('click'))
// module_menu_element.click()
changeModule(module_item, index)
} catch (e) {
html_manip.unselectMenuItemByElement(module_menu_element)
console.warn(e)
}
}
static getModel(index) {
const model = getSelectedModel(index)
return model
}
static setModel(index, model_item) {
try {
const model_menu_element = controlnetElement(
index,
'.mModelsMenuControlNet_'
)
html_manip.selectMenuItemByElement(model_menu_element, model_item)
} catch (e) {
html_manip.unselectMenuItemByElement(model_menu_element)
console.warn(e)
}
}
static getWeight(index = 0) {
const weight = getWeight(index)
return weight
}
static setWeight(index, weight) {
setWeight(index, weight)
}
static getGuidanceStrengthStart(index) {
const guidance_strength = getControlNetGuidanceStrengthStart(index)
return guidance_strength
}
static setGuidanceStrengthStart(index, sd_value) {
setControlNetGuidanceStrengthStart(index, sd_value)
}
static getGuidanceStrengthEnd(index) {
const guidance_strength = getControlNetGuidanceStrengthEnd(index)
return guidance_strength
}
static setGuidanceStrengthEnd(index, sd_value) {
setControlNetGuidanceStrengthEnd(index, sd_value)
}
static getProcessorRes(index) {
const slider = controlnetElement(index, '.slControlNetProcessorRes_')
return slider.value
}
static setProcessorRes(index, sd_value) {
const slider = controlnetElement(index, '.slControlNetProcessorRes_')
slider.value = sd_value
}
static getThreshold(index, a_or_b) {
let slider
if (a_or_b === 'a') {
slider = controlnetElement(index, '.slControlNetThreshold_A_')
} else if (a_or_b === 'b') {
slider = controlnetElement(index, '.slControlNetThreshold_B_')
}
const sd_value = general.mapRange(
slider.value,
slider.min,
slider.max,
slider.dataset['sd_min'],
slider.dataset['sd_max']
)
return sd_value
}
static setThreshold(index, a_or_b, sd_value) {
let slider
let label
if (a_or_b === 'a') {
slider = controlnetElement(index, '.slControlNetThreshold_A_')
label = controlnetElement(index, '.lControlNetThreshold_A_')
} else if (a_or_b === 'b') {
slider = controlnetElement(index, '.slControlNetThreshold_B_')
label = controlnetElement(index, '.lControlNetThreshold_B_')
}
const slider_value = general.mapRange(
sd_value,
slider.dataset['sd_min'],
slider.dataset['sd_max'],
slider.min,
slider.max
)
slider.value = String(slider_value)
label.innerText = String(sd_value)
}
static getControlNetUnitJson(index = 0) {}
}
async function checkIfControlNetInstalled() {}
async function requestControlNetDetectMap(
controlnet_init_image,
_module,
processor_res,
threshold_a,
threshold_b
) {
try {
const payload = {
controlnet_module: _module,
controlnet_input_images: [controlnet_init_image],
controlnet_processor_res: processor_res,
controlnet_threshold_a: threshold_a,
controlnet_threshold_b: threshold_b,
}
const full_url = `${g_sd_url}/controlnet/detect`
// debugger
const response_data = await api.requestPost(full_url, payload)
// update the mask preview with the new detectMap
if (response_data['images'].length === 0) {
app.showAlert(response_data['info'])
}
return response_data['images'][0]
} catch (e) {
console.warn('requestControlNetDetectMap(): ', _module, e)
}
}
async function requestControlNetModelList() {
const control_net_json = await api.requestGet(
`${g_sd_url}/controlnet/model_list`
)
const model_list = control_net_json?.model_list
// const model_list = [
// 'none',
// 'control_sd15_depth [fef5e48e]',
// 'control_sd15_openpose [fef5e48e]',
// 'control_sd15_scribble [fef5e48e]',
// ]
return model_list
}
async function requestControlNetModuleList() {
// const control_net_json = await api.requestGet(
// `${g_sd_url}/controlnet/model_list`
// )
// const module_list = [
// // 'none',
// 'canny',
// 'depth',
// 'depth_leres',
// 'hed',
// 'mlsd',
// 'normal_map',
// 'openpose',
// // "openpose_hand",
// 'pidinet',
// 'scribble',
// 'fake_scribble',
// 'segmentation',
// ]
// const module_list = g_sd_config_obj.getControlNetPreprocessors()
const result = await api.requestGet(
`${g_sd_url}/controlnet/module_list?alias_names=1`
)
return result?.module_list
}
async function populateModelMenu() {
try {
const models = await requestControlNetModelList()
for (let index = 0; index < g_controlnet_max_models; index++) {
const menu_element = controlnetElement(
index,
'.mModelsMenuControlNet_'
)
html_manip.populateMenuByElement(
menu_element,
'mModelsMenuItemControlNet_',
models,
(item, item_html_element) => {
item_html_element.innerHTML = item
item_html_element.dataset['index'] = index
},
false,
'Select Model'
)
}
} catch (e) {
console.warn(e)
}
}
function changeModule(_module, index) {
// const index = index
const preprocessor_res_element = controlnetElement(
index,
'.slControlNetProcessorRes_'
)
const threshold_a_element = controlnetElement(
index,
'.slControlNetThreshold_A_'
)
const threshold_b_element = controlnetElement(
index,
'.slControlNetThreshold_B_'
)
const detail = g_module_detail[_module]
function remapArray(arr) {
let obj = {
preprocessor_res: arr[0] || null,
threshold_a: arr[1] || null,
threshold_b: arr[2] || null,
}
return obj
}
const params = remapArray(detail['sliders'])
const model_free = detail.model_free
// threshold_a_element.min = prams.
// threshold_a_element.max =
// debugger
if (model_free)
controlnetElement(
index,
'.mModelsMenuControlNet_'
).parentElement.style.display = 'none'
else
controlnetElement(
index,
'.mModelsMenuControlNet_'
).parentElement.style.display = 'block'
if (params?.preprocessor_res) {
const preprocessor_res_label_element = controlnetElement(
index,
'.labelControlNetProcessorRes_'
)
preprocessor_res_element.style.display = 'block'
preprocessor_res_label_element.innerText = params.preprocessor_res.name
} else {
preprocessor_res_element.style.display = 'none'
}
if (params?.threshold_a) {
const threshold_a_label_element = controlnetElement(
index,
'.labelControlNetThreshold_A_'
)
threshold_a_element.dataset['sd_min'] = params.threshold_a.min
threshold_a_element.dataset['sd_max'] = params.threshold_a.max
ControlNetUnit.setThreshold(index, 'a', params.threshold_a.value)
threshold_a_element.style.display = 'block'
threshold_a_label_element.innerText = params.threshold_a.name + ":"
} else {
ControlNetUnit.setThreshold(index, 'a', 32)
threshold_a_element.style.display = 'none'
}
if (params?.threshold_b) {
const threshold_b_label_element = controlnetElement(
index,
'.labelControlNetThreshold_B_'
)
threshold_b_element.dataset['sd_min'] = params.threshold_b.min
threshold_b_element.dataset['sd_max'] = params.threshold_b.max
ControlNetUnit.setThreshold(index, 'b', params.threshold_b.value)
threshold_b_element.style.display = 'block'
threshold_b_label_element.innerText = params.threshold_b.name + ":"
} else {
ControlNetUnit.setThreshold(index, 'b', 32)
threshold_b_element.style.display = 'none'
}
}
async function populatePreprocessorMenu() {
try {
// debugger
const modules = await requestControlNetModuleList()
for (let index = 0; index < g_controlnet_max_models; index++) {
const menu_element = controlnetElement(
index,
'.mModulesMenuControlNet_'
)
menu_element.dataset['index'] = String(index)
html_manip.populateMenuByElement(
menu_element,
'mModuleMenuItemControlNet_',
modules,
(item, item_html_element) => {
item_html_element.innerHTML = item
item_html_element.dataset['index'] = index
},
false,
'Select Module'
)
menu_element.addEventListener('click', (event) => {
changeModule(
event.target.innerText,
event.target.dataset['index']
)
})
}
} catch (e) {
console.warn(e)
}
}
function controlnetElement(index, class_) {
const element = document.querySelector(
`#controlnet_settings_${index} ${class_}`
)
return element
}
function getControlNetGuidanceStrengthStart(index) {
const slider_element = document.querySelector(
`#controlnet_settings_${index} .slControlNetGuidanceStrengthStart_`
)
const sd_value = html_manip.getSliderSdValueByElement(
slider_element,
0,
100,
0,
1
)
return sd_value
}
function setControlNetGuidanceStrengthStart(index, sd_value) {
const slider_element = controlnetElement(
index,
'.slControlNetGuidanceStrengthStart_'
)
const label_element = controlnetElement(
index,
'.lControlNetGuidanceStrengthStart_'
)
html_manip.setSliderSdValueByElements(
slider_element,
label_element,
sd_value,
0,
100,
0,
1
)
}
function getControlNetGuidanceStrengthEnd(index) {
const slider_element = controlnetElement(
index,
'.slControlNetGuidanceStrengthEnd_'
)
const sd_value = html_manip.getSliderSdValueByElement(
slider_element,
0,
100,
0,
1
)
return sd_value
}
function setControlNetGuidanceStrengthEnd(index, sd_value) {
const slider_element = controlnetElement(
index,
'.slControlNetGuidanceStrengthEnd_'
)
const label_element = controlnetElement(
index,
'.lControlNetGuidanceStrengthEnd_'
)
html_manip.setSliderSdValueByElements(
slider_element,
label_element,
sd_value,
0,
100,
0,
1
)
}
// controlnet settings getters
function getControlNetWeightGuidanceStrengthStart(index = 0) {
const slider_element = controlnetElement(
index,
'.slControlNetGuidanceStrengthStart_'
)
const slider_value = slider_element.value
const sd_value = general.mapRange(slider_value, 0, 100, 0, 1) // convert slider value to SD ready value
return sd_value
}
function getControlNetWeightGuidanceStrengthEnd(index = 0) {
const slider_value = controlnetElement(
index,
'.slControlNetGuidanceStrengthEnd_'
).value
const sd_value = general.mapRange(slider_value, 0, 100, 0, 1) // convert slider value to SD ready value
return sd_value
}
function getWeight(index = 0) {
const slider_value = document.querySelector(
`#controlnet_settings_${index} .slControlNetWeight_`
).value
const sd_value = general.mapRange(slider_value, 0, 100, 0, 2) // convert slider value to SD ready value
return sd_value
}
function setWeight(index = 0, sd_weight) {
const slider_element = document.querySelector(
`#controlnet_settings_${index} .slControlNetWeight_`
)
const label_element = document.querySelector(
`#controlnet_settings_${index} .lControlNetWeight_`
)
html_manip.setSliderSdValueByElements(
slider_element,
label_element,
sd_weight,
0,
100,
0,
2
)
}
function getUseLowVram(index = 0) {
const b_result = document.querySelector(
`#controlnet_settings_${index} .chlowVram_`
).checked
return b_result
}
function getEnableControlNet(index = 0) {
const is_enable = document.querySelector(
`#controlnet_settings_${index} .chEnableControlNet_`
).checked
return is_enable
}
function setEnable(index) {
document.querySelector(
`#controlnet_settings_${index} .chEnableControlNet_`
).checked = b_live_update
}
function getSelectedModule(index = 0) {
const menu_element = controlnetElement(index, '.mModulesMenuControlNet_')
// debugger
const module_name =
html_manip.getSelectedMenuItemTextContentByElement(menu_element)
return module_name
}
function getSelectedModel(index = 0) {
const menu_element = controlnetElement(index, '.mModelsMenuControlNet_')
const model_name =
html_manip.getSelectedMenuItemTextContentByElement(menu_element)
return model_name
}
function getUseGuessMode(index = 0) {
const is_guess_mode = document.querySelector(
`#controlnet_settings_${index} .chGuessMode_`
).checked
return is_guess_mode
}
function isControlNetModeEnable() {
let is_tab_enabled = !document.getElementById('chDisableControlNetTab')
.checked
let numOfEnabled = 0
if (g_controlnet_max_models <= 0) {
return false
}
if (is_tab_enabled) {
for (let index = 0; index < g_controlnet_max_models; index++) {
if (getEnableControlNet(index)) {
numOfEnabled += 1
}
}
}
let is_mode_enabled = is_tab_enabled // could be true
if (is_tab_enabled === false || numOfEnabled === 0) {
is_mode_enabled = false
}
return is_mode_enabled
}
// function getControlNetMaxModelsNumber() {
// return g_controlnet_max_supported_models
// }
function preprocessorData(
processor_res = 512,
threshold_a = 64,
threshold_b = 64,
a_min = 64,
a_max = 1024,
b_min = 64,
b_max = 1024,
processor_res_label = 'Annotator resolution',
threshold_a_label = 'threshold a',
threshold_b_label = 'threshold b'
) {
return {
processor_res,
threshold_a,
threshold_b,
a_min,
a_max,
b_min,
b_max,
processor_res_label,
threshold_a_label,
threshold_b_label,
}
}
function mapPluginSettingsToControlNet(plugin_settings) {
const ps = plugin_settings // for shortness
let controlnet_units = []
// debugger
let active_index = 0
for (let index = 0; index < g_controlnet_max_models; index++) {
const preprocessor_name = getSelectedModule(index)
controlnet_units[active_index] = {
enabled: getEnableControlNet(index),
input_image: g_generation_session.controlNetImage[index],
mask: '',
module: getSelectedModule(index),
model: getSelectedModel(index),
weight: getWeight(index),
resize_mode: 'Scale to Fit (Inner Fit)',
lowvram: getUseLowVram(index),
processor_res: ControlNetUnit.getProcessorRes(index),
threshold_a: ControlNetUnit.getThreshold(index, 'a'),
threshold_b: ControlNetUnit.getThreshold(index, 'b'),
// guidance: ,
guidance_start: getControlNetWeightGuidanceStrengthStart(index),
guidance_end: getControlNetWeightGuidanceStrengthEnd(index),
guessmode: false,
}
active_index++
}
if (
plugin_settings['mode'] === Enum.generationModeEnum['Img2Img'] ||
plugin_settings['mode'] === Enum.generationModeEnum['Inpaint'] ||
plugin_settings['mode'] === Enum.generationModeEnum['Outpaint']
) {
const b_use_guess_mode = getUseGuessMode()
controlnet_units[0]['guessmode'] = b_use_guess_mode
}
const controlnet_payload = {
...ps,
controlnet_units, //keep for backward compatibility for now
subseed: -1,
override_settings: {},
override_settings_restore_afterwards: true,
alwayson_scripts: {
controlnet: {
args: controlnet_units,
},
},
}
return controlnet_payload
}
function refreshControlNetTab() {}
async function populateControlNetPresetMenu() {
// const default_preset_name = 'Select CtrlNet Preset'
// const default_preset_settings = {} // empty preset
const custom_presets = await preset.getAllCustomPresetsSettings(
Enum.PresetTypeEnum['ControlNetPreset']
)
g_controlnet_presets = {
'Select CtrlNet Preset': {},
...controlnet_preset.ControlNetNativePresets,
...custom_presets,
}
const presets_names = Object.keys(g_controlnet_presets)
html_manip.populateMenu(
'mControlNetPresetMenu',
'mControlNetPresetMenuItem',
presets_names,
(preset_name, item_html_element) => {
item_html_element.innerHTML = preset_name
}
)
html_manip.selectMenuItem('mControlNetPresetMenu', 'Select CtrlNet Preset')
}
document
.getElementById('mControlNetPresetMenu')
.addEventListener('change', async (evt) => {
try {
ControlNetUnit.resetUnits()
const preset_index = evt.target.selectedIndex
const preset_name = evt.target.options[preset_index].textContent
units_settings = g_controlnet_presets[preset_name]
ControlNetUnit.setUnits(units_settings)
} catch (e) {
console.warn(e)
}
})
document
.getElementById('bSetAllControlImage')
.addEventListener('click', async () => {
const selectionInfo = await selection.Selection.getSelectionInfoExe()
if (selectionInfo) {
const base64_image =
await g_generation_session.setControlNetImageHelper()
for (index = 0; index < g_controlnet_max_models; index++) {
await g_generation_session.setControlNetImage(
index,
base64_image
)
}
} else {
await note.Notification.inactiveSelectionArea()
}
})
function initControlNetUnitsEventListeners(controlnet_max_models) {
for (let index = 0; index < controlnet_max_models; index++) {
//event listeners
controlnetElement(
index,
'.slControlNetGuidanceStrengthStart_'
).addEventListener('input', (evt) => {
// debugger
const sd_value = general.mapRange(evt.target.value, 0, 100, 0, 1) // convert slider value to SD ready value
controlnetElement(
index,
'.lControlNetGuidanceStrengthStart_'
).textContent = Number(sd_value).toFixed(2)
})
controlnetElement(
index,
'.slControlNetGuidanceStrengthEnd_'
).addEventListener('input', (evt) => {
// debugger
const sd_value = general.mapRange(evt.target.value, 0, 100, 0, 1) // convert slider value to SD ready value
controlnetElement(
index,
'.lControlNetGuidanceStrengthEnd_'
).textContent = Number(sd_value).toFixed(2)
})
document
.querySelector(`#controlnet_settings_${index} .slControlNetWeight_`)
.addEventListener('input', (evt) => {
// debugger
const sd_value = general.mapRange(
evt.target.value,
0,
100,
0,
2
) // convert slider value to SD ready value
document.querySelector(
`#controlnet_settings_${index} .lControlNetWeight_`
).textContent = Number(sd_value).toFixed(2)
})
// controlnetElement(index, '.slControlNetProcessorRes_').addEventListener(
// 'input',
// (evt) => {
// // debugger
// const sd_value = general.mapRange(
// evt.target.value,
// 64,
// 2048,
// 64,
// 2048
// ) // convert slider value to SD ready value
// controlnetElement(index, '.lControlNetProcessorRes_').textContent =
// Number(sd_value).toFixed(2)
// }
// )
controlnetElement(index, '.slControlNetThreshold_A_').addEventListener(
'input',
(event) => {
const slider = event.target
const sd_value = Number(
general
.mapRange(
slider.value,
slider.min,
slider.max,
slider.dataset['sd_min'],
slider.dataset['sd_max']
)
.toFixed(2)
)
controlnetElement(index, '.lControlNetThreshold_A_').innerText =
String(sd_value)
// ControlNetUnit.setThreshold(index, 'a', sd_value)
// controlnetElement(index, '.lControlNetThreshold_A_').value =
// sd_value
}
)
controlnetElement(index, '.slControlNetThreshold_B_').addEventListener(
'input',
(event) => {
const slider = event.target
const sd_value = Number(
general
.mapRange(
slider.value,
slider.min,
slider.max,
slider.dataset['sd_min'],
slider.dataset['sd_max']
)
.toFixed(2)
)
controlnetElement(index, '.lControlNetThreshold_B_').innerText =
String(sd_value)
// ControlNetUnit.setThreshold(index, 'a', sd_value)
// controlnetElement(index, '.lControlNetThreshold_A_').value =
// sd_value
}
)
document
.querySelector(`#controlnet_settings_${index} .bSetControlImage_`)
.addEventListener('click', async () => {
const selectionInfo =
await selection.Selection.getSelectionInfoExe()
if (selectionInfo) {
// debugger
const base64_image =
await g_generation_session.setControlNetImageHelper()
await g_generation_session.setControlNetImage(
index,
base64_image
)
} else {
await note.Notification.inactiveSelectionArea()
}
})
document
.querySelector(`#controlnet_settings_${index} .bControlMask_`)
.addEventListener('click', async () => {
await previewAnnotator(index)
})
}
}
document
.getElementById('mControlNetPresetMenu')
.addEventListener('updatePresetMenuEvent', async (event) => {
// console.log("I'm listening on a custom event")
await populateControlNetPresetMenu()
})
async function previewAnnotator(index) {
try {
const controlnet_init_image =
g_generation_session.controlNetImage[index]
const _module = ControlNetUnit.getModule(index)
const processor_res = ControlNetUnit.getProcessorRes(index)
const threshold_a = ControlNetUnit.getThreshold(index, 'a')
const threshold_b = ControlNetUnit.getThreshold(index, 'b')
if (!controlnet_init_image) {
const error = 'ControlNet initial image is empty'
app.showAlert(error)
throw error
}
if (!_module || _module === 'none') {
const error = 'select a valid controlnet module (preprocessor)'
app.showAlert(error)
throw error
}
const detect_map = await requestControlNetDetectMap(
controlnet_init_image,
_module,
processor_res,
threshold_a,
threshold_b
)
const rgb_detect_map_url =
await io.convertBlackAndWhiteImageToRGBChannels3(detect_map)
const rgb_detect_map = general.base64UrlToBase64(rgb_detect_map_url)
g_generation_session.controlNetMask[index] = rgb_detect_map
html_manip.setControlMaskSrc(rgb_detect_map_url, index)
} catch (e) {
console.warn('PreviewAnnotator click(): index: ', index, e)
}
}
async function previewAnnotatorFromCanvas(index) {
try {
const _module = ControlNetUnit.getModule(index)
const width = html_manip.getWidth()
const height = html_manip.getHeight()
const selectionInfo = await psapi.getSelectionInfoExe()
g_generation_session.control_net_preview_selection_info = selectionInfo
const base64 = await io.IO.getSelectionFromCanvasAsBase64Interface_New(
width,
height,
selectionInfo,
true
)
if (!_module || _module === 'none') {
const error = 'select a valid controlnet module (preprocessor)'
app.showAlert(error)
throw error
}
const processor_res = ControlNetUnit.getProcessorRes(index)
const threshold_a = ControlNetUnit.getThreshold(index, 'a')
const threshold_b = ControlNetUnit.getThreshold(index, 'b')
const detect_map = await requestControlNetDetectMap(
base64,
_module,
processor_res,
threshold_a,
threshold_b
)
const rgb_detect_map_url =
await io.convertBlackAndWhiteImageToRGBChannels3(detect_map)
g_generation_session.controlNetMask[index] = detect_map
html_manip.setControlMaskSrc(rgb_detect_map_url, index)
} catch (e) {
console.warn('PreviewAnnotator click(): index: ', index, e)
}
}
// document
// .getElementById('bPreviewAnnotator_0')
// .addEventListener('click', async (event) => {
// const index = parseInt(event.target.dataset['controlnet-index'])
// await previewAnnotator(index)
// })
function initPreviewElement(index) {
//make init mask image use the thumbnail class with buttons
// const mask_image_html = document.getElementById(
// 'control_net_preview_image_0'
// )
const mask_image_html = controlnetElement(index, '.control_net_mask_')
const mask_parent_element = mask_image_html.parentElement
this.thumbnail_container = thumbnail.Thumbnail.wrapImgInContainer(
mask_image_html,
'viewer-image-container'
)
mask_parent_element.appendChild(thumbnail_container)
async function toCanvas(index) {
// debugger
if (
g_generation_session.control_net_preview_selection_info &&
g_generation_session.controlNetMask[index]
) {
const selection_info =
g_generation_session.control_net_preview_selection_info
const layer = await io.IO.base64ToLayer(
g_generation_session.controlNetMask[index],
'ControlNet Mask.png',
selection_info.left,
selection_info.top,
selection_info.width,
selection_info.height
)
} else {
// await note.Notification.inactiveSelectionArea()
app.showAlert('Mask Image is not available')
}
}
async function toControlNetInitImage(index) {
const preview_result_base64 = g_generation_session.controlNetMask[index]
g_generation_session.controlNetImage[index] = preview_result_base64
g_generation_session.control_net_selection_info =
g_generation_session.control_net_preview_selection_info
const rgb_detect_map_url =
await io.convertBlackAndWhiteImageToRGBChannels3(
preview_result_base64
)
// g_generation_session.controlNetMask[index] = rgb_detect_map
html_manip.setControlImageSrc(rgb_detect_map_url, index)
}
thumbnail.Thumbnail.addSPButtonToContainer(
this.thumbnail_container,
'svg_sp_btn',
'use as ControlNet init image',
toControlNetInitImage,
index
)
thumbnail.Thumbnail.addSPButtonToContainer(
this.thumbnail_container,
'svg_sp_btn_canvas',
'move to the canvas',
toCanvas,
index
)
thumbnail.Thumbnail.addSPButtonToContainer(
this.thumbnail_container,
'svg_sp_btn_preview',
'preview selection from canvas',
previewAnnotatorFromCanvas,
index
)
}
function initControlNetUnit(index) {
document.querySelector(
`#controlnet_settings_${index} .controlnet_unit_label_`
).textContent = `Control Net Settings Slot ${index}`
}
async function initializeControlNetTab(controlnet_max_models) {
try {
if (controlnet_max_models <= 0) {
document.getElementById('controlnetMissingError').style.display =
'block'
return
}
document.getElementById('controlnetMissingError').style.display = 'none'
// if (controlnet_max_models > g_controlnet_max_models)
// controlnet_max_models = g_controlnet_max_models
await populateControlNetPresetMenu()
const parent_container = document.getElementById(
'controlnet_parent_container'
)
parent_container.innerHTML = ''
const controlnet_unit_template = document.getElementById(
'controlnet_settings_template'
)
for (let index = 0; index < controlnet_max_models; index++) {
const controlnet_unit = controlnet_unit_template.cloneNode(true)
controlnet_unit.id = 'controlnet_settings_' + index
controlnet_unit.dataset['index'] = String(index)
parent_container.appendChild(controlnet_unit)
}
const full_url = `${g_sd_url}/controlnet/module_list?alias_names=1`
let result_json = await api.requestGet(full_url)
g_module_detail = result_json['module_detail']
initControlNetUnitsEventListeners(controlnet_max_models) // add event listener to all units after cloning
for (let index = 0; index < controlnet_max_models; index++) {
await populateModelMenu(index)
await populatePreprocessorMenu(index)
document.getElementById(
'controlnet_settings_' + index
).style.display = 'block'
initPreviewElement(index)
initControlNetUnit(index)
}
} catch (e) {
console.warn(e)
}
}
module.exports = {
requestControlNetModelList,
populateModelMenu,
initializeControlNetTab,
getWeight,
mapPluginSettingsToControlNet,
getEnableControlNet,
getSelectedModule,
getSelectedModel,
// getControlNetMaxModelsNumber,
getControlNetGuidanceStrengthStart,
setControlNetGuidanceStrengthStart,
getControlNetGuidanceStrengthEnd,
setControlNetGuidanceStrengthEnd,
ControlNetUnit,
populateControlNetPresetMenu,
isControlNetModeEnable,
getModuleDetail() {
return g_module_detail
}
}
|