-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
401 lines (394 loc) · 13.7 KB
/
code.ts
File metadata and controls
401 lines (394 loc) · 13.7 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
figma.showUI(__html__, { width: 320, height: 720 });
const FRAME_TYPE_NAME = "FRAME";
const DEBUG_MODE = false;
const DEFAULT_UPSCALE_SCALE=2
class Cache{
settings_version: string = Settings.latest_settings_version;
url: string ="";
api_key: string = "";
prompt: string = "";
negative_prompt: string = "";
step: number = 20;
sampler: string = "Euler";
seed: string = "-1";
cfg_scale: number = 7;
target_length: number = 1024;
batch_size: number = 1;
denoising_strength: number = 0.5;
large_image: boolean = false;
upscaler: string = "R-ESRGAN 4x+";
alpha_matting_foreground_threshold:number=120;
alpha_matting_background_threshold:number=10;
}
class Settings extends Cache{
static latest_settings_version: string = "1.0.0";
static getInstance(cache: Cache) {
let settings = new Settings();
const merge = Object.assign({}, settings, cache);
Object.assign(settings, merge);
return settings;
}
async save() {
if(debugMode())console.log("Saving",this);
await figma.clientStorage.setAsync("settings", JSON.stringify(this));
}
async load() {
try{
let settings_str = await figma.clientStorage.getAsync("settings");
if (settings_str) {
let cached_settings:Cache = JSON.parse(settings_str);
if(cached_settings.settings_version&&cached_settings.settings_version==Settings.latest_settings_version)
Object.assign(this, cached_settings);
}
}catch(e: any){
if(debugMode())console.log(e.message)
}
}
toJson() {
return JSON.stringify(this);
}
getHeaders() {
if (!this.api_key) return { 'Content-Type': 'application/json' };
return { 'Content-Type': 'application/json', "Authorization": this.api_key };
}
}
let settings = new Settings();
let cache = new Cache();
async function initialize() {
await settings.load();
const merge = Object.assign({}, cache, settings);
Object.assign(cache, merge)
settings = Settings.getInstance(cache);
if(debugMode())console.log("Initialized with",settings);
figma.ui.postMessage({ type: 'sync_settings', settings: settings.toJson(),debug_mode:debugMode() });
}
initialize()
const figmaUiMessageHandler=async (msg:any)=>{
let ui_cache:Cache = JSON.parse(msg.cache);
const merge = Object.assign({}, cache, ui_cache);
Object.assign(cache, merge);
const type = msg.type;
try{
switch (type) {
case "save_settings":
settings = Settings.getInstance(cache);
await settings.save();
figma.ui.postMessage({ type: 'saved_settings', settings: JSON.stringify(settings) });
break;
case "focus_selected_items":
if (figma.currentPage.selection.length > 0)
figma.viewport.scrollAndZoomIntoView(figma.currentPage.selection);
else
figma.notify("Please select items to focus");
break;
case "focus_frame":
if (figma.currentPage.selection.length > 0){
let frame=selectFrameFromSelection(figma.currentPage.selection);
if(frame){
figma.viewport.scrollAndZoomIntoView([frame]);
figma.currentPage.selection = [frame];
}
}else{
figma.notify("Please select items that have frame to focus");
}
break;
case "txt2img":
await txt2img();
break;
case "img2img":
await img2img();
break;
case "remove_background":
await removeBackground();
break;
case "get_background_mask":
await get_background_mask();
break
case "upscale":
await upscale();
break;
}
}catch(e: any){
if(debugMode())console.log(e)
figma.ui.postMessage({ type: 'error', message: e.message });
}
}
figma.ui.onmessage = figmaUiMessageHandler;
function debugMode(){
return DEBUG_MODE;
}
function selectFrameFromSelection(selection: readonly SceneNode[]) {
let frame = null;
for (const selected_item of selection) {
if (selected_item.type === FRAME_TYPE_NAME) {
frame = selected_item;
break;
}
}
if (frame === null) {
for (const selected_item of selection){
frame = getFrameFromNode(selected_item);
}
}
return frame;
}
function getFrameFromNode(node: BaseNode|null) {
let frame = null;
if (node === null) return frame;
while (node.parent != null) {
if (node.type === FRAME_TYPE_NAME) {
frame = node;
break;
}
node = node.parent;
}
return frame;
}
async function txt2img(){
const frame=figma.createFrame();
frame.name=settings.prompt;
frame.resize(settings.target_length,settings.target_length);
frame.x=figma.viewport.center.x-frame.width/2;
frame.y=figma.viewport.center.y-frame.height/2;
frame.clipsContent=true;
const optimistSize=getOptimistSize(frame);
let query: any = {
"prompt": settings.prompt,
"negative_prompt": settings.negative_prompt,
"steps": settings.step,
"sampler_index": settings.sampler,
"seed": settings.seed,
"cfg_scale": settings.cfg_scale,
"width": optimistSize.width,
"height": optimistSize.height,
"batch_size": settings.batch_size
}
if(debugMode())console.log("Query",query);
figma.ui.postMessage({ type: 'generating' });
let data = await getJsonResponse(`${settings.url}/sdapi/v1/txt2img`, query);
if(debugMode())console.log("Response",data);
const images=data['images'];
for (const base64 of images) {
const byte_array = await base64_to_Uint8Array(base64);
create_image_node("txt2image", byte_array,frame, settings.prompt, frame.width, frame.height);
}
figma.ui.postMessage({ type: 'generated' });
}
async function img2img(){
const frame=selectFrameFromSelection(figma.currentPage.selection);
if (!frame) {
figma.notify("Please select a frame or something that is within a frame to generate image");
return;
}
const optimistSize=getOptimistSize(frame);
const imageUrl = await extract_base64(frame);
let query: any = {
"init_images": [imageUrl],
"denoising_strength": settings.denoising_strength,
"prompt": settings.prompt,
"negative_prompt": settings.negative_prompt,
"steps": settings.step,
"sampler_index": settings.sampler,
"cfg_scale": settings.cfg_scale,
"width": optimistSize.width,
"height": optimistSize.height,
"seed": settings.seed,
"batch_size": settings.batch_size
}
if(debugMode())console.log("Query",query);
figma.ui.postMessage({ type: 'generating' });
let data = await getJsonResponse(`${settings.url}/sdapi/v1/img2img`, query);
if(debugMode())console.log("Response",data);
const images=data['images'];
for (const base64 of images) {
const byte_array = await base64_to_Uint8Array(base64);
create_image_node("img2img", byte_array,frame, settings.prompt, frame.width, frame.height);
}
}
async function removeBackground(){
const frame=selectFrameFromSelection(figma.currentPage.selection);
if (!frame) {
figma.notify("Please select a frame or something that is within a frame to generate image");
return;
}
const newFrame=figma.createFrame();
newFrame.name=frame.name;
newFrame.resize(frame.width,frame.height);
newFrame.x=figma.viewport.center.x-newFrame.width/2;
newFrame.y=figma.viewport.center.y-newFrame.height/2;
newFrame.clipsContent=true;
const base64_frame = await extract_base64(frame);
const base64_rmbg = await get_auto_mask(base64_frame, false);
const byte_array = await base64_to_Uint8Array(base64_rmbg);
create_image_node("removeBackground", byte_array,newFrame ,"removed-background-of--"+frame.name, newFrame.width, newFrame.height);
}
async function get_background_mask(){
const frame=selectFrameFromSelection(figma.currentPage.selection);
if (!frame) {
figma.notify("Please select a frame or something that is within a frame to generate image");
return;
}
const base64_frame = await extract_base64(frame);
const base64_rmbg = await get_auto_mask(base64_frame, true);
const svgstr = await img2svg(base64_rmbg);
create_node_from_svg("get_background_mask", svgstr,frame ,"mask-of--"+frame.name, frame.width, frame.height);
}
function img2svg(base64:string){
return new Promise<string>((resolve) => {
function handleMessage(msg: any) {
if (msg.type === "img2svg_result") {
figma.ui.onmessage = figmaUiMessageHandler;
resolve(msg.svgstr);
}
}
figma.ui.onmessage = handleMessage;
figma.ui.postMessage({ type: "img2svg", base64 });
});
}
async function upscale(){
const frame=selectFrameFromSelection(figma.currentPage.selection);
if (!frame) {
figma.notify("Please select a frame or something that is within a frame to generate image");
return;
}
const newFrame=figma.createFrame();
newFrame.name=frame.name;
newFrame.resize(frame.width*DEFAULT_UPSCALE_SCALE,frame.height*DEFAULT_UPSCALE_SCALE);
newFrame.x=figma.viewport.center.x-newFrame.width/2;
newFrame.y=figma.viewport.center.y-newFrame.height/2;
newFrame.clipsContent=true;
const base64_frame = await extract_base64(frame);
const base64_upscale = await upscale_image(base64_frame);
const byte_array = await base64_to_Uint8Array(base64_upscale);
create_image_node("upscale", byte_array,newFrame ,"upscaled--"+frame.name, newFrame.width, newFrame.height);
}
async function getJsonResponse(url: string, query: any) {
try{
let res = await fetch(url.replace("//sdapi","/sdapi").replace("//figma","/figma"), {
method: 'POST',
headers: settings.getHeaders(),
body: JSON.stringify(query)
}) as FetchResponse;
return await res.json();
}catch(e: any){
if(e.message.includes("timeout"))
figma.notify("Figma plugin server is not responding within limited time. Please try to lower the batch size, steps count or don't check the large image option. You can also use a more powerful GPU or wait for this plugin to updates.");
if(debugMode())console.log(e.message);
return {images:[]};
}
}
async function get_auto_mask(imageUrl: string, mask_only: boolean) {
let query: any = {
"image_str": imageUrl,
"alpha_matting": false,
"alpha_matting_foreground_threshold": 120,
"alpha_matting_background_threshold": 10,
"alpha_matting_erode_size": 10,
"session_name": "u2net",
"only_mask": mask_only,
"post_process_mask": true,
}
if(debugMode())console.log("Query",query);
let data = await getJsonResponse(`${settings.url}/figma/auto_mask/remove-background`, query);
const base64 = data['mask'];
return base64;
}
async function upscale_image(imageUrl: string) {
let query: any = {
"resize_mode": 0,
"show_extras_results": true,
"gfpgan_visibility": 0,
"codeformer_visibility": 0,
"codeformer_weight": 0,
"upscaling_resize": DEFAULT_UPSCALE_SCALE,
"upscaling_crop": true,
"upscaler_1": settings.upscaler,
"upscale_first": false,
"image": imageUrl
}
if(debugMode())console.log("Query",query);
let data = await getJsonResponse(`${settings.url}/sdapi/v1/extra-single-image`, query);
const base64 = data['image'];
return base64;
}
async function create_image_node(original_task: string, byte_array: Uint8Array,frame:FrameNode, image_name=settings.prompt, width = -1, height = -1) {
const imageNode = figma.createRectangle();
const image = await figma.createImage(byte_array);
if (width === -1 || height === -1) { let size = await image.getSizeAsync(); width = size.width; height = size.height; }
imageNode.resize(width, height);
imageNode.fills = [
{
type: 'IMAGE',
imageHash: image.hash,
scaleMode: 'FILL'
}
]
imageNode.name = image_name;
frame.appendChild(imageNode);
figma.ui.postMessage({ original_task, type: 'generated' });
figma.commitUndo();
}
async function create_node_from_svg(original_task: string, svgstr: string,frame:FrameNode, image_name=settings.prompt, width=-1, height=-1) {
const svgNode = figma.createNodeFromSvg(svgstr);
if (width === -1 || height === -1) { width = frame.width; height = frame.height; }
svgNode.resize(width, height);
svgNode.name = image_name;
frame.appendChild(svgNode);
figma.ui.postMessage({ original_task, type: 'generated' });
figma.commitUndo();
}
function getOptimistSize(frame:FrameNode){
const width = frame.width;
const height = frame.height;
const targetWidth = settings.target_length;
const aspectRatio = width / height;
const referenceLogic=settings.large_image?width<height:width>height;
if (referenceLogic) {
return {
width: targetWidth,
height: targetWidth / aspectRatio
};
}else{
return {
width: targetWidth * aspectRatio,
height: targetWidth
};
}
}
async function extract_base64(frame:FrameNode){
const frameUint8array = await frame.exportAsync({ format: "PNG", constraint: { type: "SCALE", value: 1 } });
const base64 = await Uint8Array_to_base64(frameUint8array);
return base64;
}
async function base64_to_Uint8Array(base64: string) {
return new Promise<Uint8Array>((resolve) => {
function handleMessage(msg: any) {
if (msg.type === "Uint8Array_to_base64_result") {
figma.ui.onmessage = figmaUiMessageHandler;
resolve(msg.base64);
}
if (msg.type === "base64_to_Uint8Array_result") {
figma.ui.onmessage = figmaUiMessageHandler;
resolve(msg.uint8array);
}
}
figma.ui.onmessage = handleMessage;
figma.ui.postMessage({ type: "base64_to_Uint8Array", base64 });
});
}
function Uint8Array_to_base64(uint8array: Uint8Array) {
return new Promise<string>((resolve) => {
function handleMessage(msg: any) {
if (msg.type === "Uint8Array_to_base64_result") {
figma.ui.onmessage = figmaUiMessageHandler;
resolve(msg.base64);
}
if (msg.type === "base64_to_Uint8Array_result") {
figma.ui.onmessage = figmaUiMessageHandler;
resolve(msg.uint8array);
}
}
figma.ui.onmessage = handleMessage;
figma.ui.postMessage({ type: "Uint8Array_to_base64", uint8array });
});
}