-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS_ConformByUV.ls
675 lines (601 loc) · 18.4 KB
/
JS_ConformByUV.ls
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
/*------------------------------------------------------------------------------
Modeler LScript: Conform By UV
Version: 1.2
Author: Johan Steen
Author URI: http://www.artstorm.net/
Date: 27 Oct 2010
Description: Conforms the foreground mesh to the background by using
the UV coordinates as reference.
Copyright (c) 2010, Johan Steen
All Rights Reserved.
Use is subject to license terms.
------------------------------------------------------------------------------*/
@version 2.4
@warnings
@script modeler
@name "JS_ConformByUV"
// Main Variables
cbuv_version = "1.2";
cbuv_date = "27 October 2010";
cfg_file = getdir(SETTINGSDIR) + getsep() + "JS_ConformByUV.cfg";
// GUI Settings
var tolerance = 0.02;
var subdivideUV = false;
var operationMode = 1; // 1 = normal, 2 = Cleanup UV, 3 = Morph Batch
var unweldBG = false;
var unweldFG = false;
var createMorph = true;
var morphPrefix = "Morph";
// GUI Gadgets
var ctlUnweldBG, ctlSubD, ctlUnweldFG, ctlMorphCreate, ctlMorphPfx;
// Misc
var fg;
var bg;
// Point Variables
var uvMap; // Holds the selected UV map
var iTotBGPnts; // Integer with total number of points in BG layer
var iTotFGPnts; // Integer with total number of selected points in FG layer
var globalPntCnt; // Integer with total number of poins in FG layer
var aBGPntData; // Array that keeps track of all point coords and UV coords
var selPnts; // Array that keeps track of current user point selection
var morphMap;
var morphCtr = 1;
// Stats Variables
var statsNoUV = 0;
var statsUnMatched = nil;
var statsOverlapped = nil;
main
{
//
// Make all preparations so the plugin finds enough data to be used.
// --------------------------------------------------------------------------------
// Get selected UV Map
uvMap = VMap(VMTEXTURE, 0) || error("Please select a UV map."); // By using 0, the modeler selected UV map is acquired.
// Get total number of points, not caring about selections
selmode(GLOBAL);
globalPntCnt = pointcount();
//
selmode(USER);
// Get the layers
fg = lyrfg();
bg = lyrbg();
// If no BG selected, exit plugin
if(bg == nil) error("Please select a BG layer.");
// if FG is empty, exit plugin
var iTotFGPnts = pointcount();
if(iTotFGPnts <= 0) error("Please use a FG layer with geometry.");
// If selected points differs from total points, store the selection
if (globalPntCnt != iTotFGPnts) {
storeSelPnts();
}
// Switch to BG
lyrsetfg(bg);
// Get number of points in BG layer
iTotBGPnts = pointcount();
// If BG is empty, exit plugin
if(iTotBGPnts == 0){
lyrsetfg(fg);
lyrsetbg(bg);
error("Please use a BG layer with geometry.");
}
//
// Open the main window and collect / process user input.
// --------------------------------------------------------------------------------
// Restore layer selections
lyrsetfg(fg);
lyrsetbg(bg);
var mainWin = open_main_window();
if (mainWin == false)
return;
/* Window Closed, start the process */
// Check so no morph is selected if morphs shall be created
if ((operationMode == 1 && createMorph == true) || operationMode == 3) {
var isMorphSelected = VMap(VMMORPH,0);
if (isMorphSelected) {
infoWindow("Error", "Please deselect any morph maps before using the plugin to Create Morphs (LScript limitation).", 460);
return;
}
}
// undogroup crashes in batch mode for some reason (probably because of reset morph)
if (operationMode != 3) {
undogroupbegin();
}
// Unweld the FG
if (unweldFG == true && operationMode == 2)
unweld();
// Switch to BG again
lyrsetfg(bg);
// If in morph mode, loop through all selected bg layers and perform the operation
if (operationMode == 3) {
var aBG = bg;
for (i = 1; i <= aBG.size(); i++) {
bg = aBG[i];
lyrsetfg(bg);
abort = findMatches();
if (abort)
return;
}
} else {
// Unweld the background
if (unweldBG == true) {
unweld();
iTotBGPnts = pointcount();
}
// Unweld and subdivide the background
if (subdivideUV == true) {
unweld();
subdivide(FLAT);
iTotBGPnts = pointcount();
}
abort = findMatches();
if (abort)
return;
}
// Merge the FG
if (unweldFG == true && operationMode == 2)
mergepoints(0);
// Open the result window
if (operationMode != 3) {
openResultWin();
}
// undogroupend crashes in batch mode for some reason probably because of reset morph)
if (operationMode != 3) {
undogroupend();
}
}
// Finds the matching UV coordinates and performed the desired operation
findMatches {
// If MorphBatch, return to base morph for each new iteration
if (operationMode == 3) {
resetMorph();
}
// Initialize the progress bar (iTotBGPnts for looping the BG array + iTotFGPnts when looping though the FG points )
moninit((iTotBGPnts + 1) + iTotFGPnts);
// Get all info from bg layer
var abort = getBGData(uvMap);
// Update number of BG Points (if some lacked UV coords)
// using iTotBGPnts on each loop later, is faster than using .size() on each iteration
iTotBGPnts = aBGPntData.size();
// Restore layer selections
lyrsetfg(fg);
lyrsetbg(bg);
// If aborted during getBGInfo, exit
if(abort) return true;
//
// Start moving the points in the foreground
// --------------------------------------------------------------------------------
// If selected points differs from total points, get the selection
if (globalPntCnt != iTotFGPnts) {
getSelPnts();
}
editbegin();
// If Create Morph is enabled in normal mode
if (operationMode == 1 && createMorph == true) {
morphMap = VMap(VMMORPH,morphPrefix, 3);
}
// Setup vertex maps for the morph mode
if (operationMode == 3) {
if (morphCtr > 1)
oldie = morphMap.name;
morphMap = VMap(VMMORPH,morphPrefix + morphCtr.asStr(), 3);
current = morphMap.name;
morphCtr++;
}
// loop through all points in foreground
var p;
foreach(p,points){
// Get the UV for current point
var uv = uvMap.getValue(p);
if(uv == nil) {
// If the point lacks UV coords
statsNoUV++;
} else {
// Convert the UV coords to a vector
var uvVec = <uv[1],uv[2],0>;
var bestMatch = nil; // Keep track of current best match
var matchPnt = nil;
// Loop through all BG UV coords
for (i=1; i <= iTotBGPnts; i++) {
// Skip if BG pnt already has been used
if (aBGPntData[i] != nil) {
// Get the distance between the FG and BG UV coord vectors
var getDist = vmag(uvVec - aBGPntData[i,4]);
// If perfect match is found
if (getDist == 0) {
// match immediately and break the for loop
matchPnt = i;
break;
}
// Check if distance is smaller than current best match, and that distance is within the tolerance
if (getDist < bestMatch && getDist < tolerance) {
bestMatch = getDist;
matchPnt = i;
}
}
}
// If a match was found, move the point into position
if (matchPnt != nil) {
if (operationMode == 1 && createMorph == false)
positionPnt(p, matchPnt);
if (operationMode == 1 && createMorph == true)
positionMorph(p, matchPnt);
if (operationMode == 2)
positionUV(p, matchPnt);
if (operationMode == 3)
positionMorph(p, matchPnt);
} else {
// if no match was found, add the unmatched point to the stats array
statsUnMatched += p;
}
}
// Increase the progressbar
if(monstep()){
monend();
editend(ABORT);
return true;
}
}
monend();
editend();
return false;
}
// Moves points, for normal mode
positionPnt: p, matchPnt {
if (aBGPntData[matchPnt,5] == true) {
statsOverlapped += p;
}
p.x = aBGPntData[matchPnt,1];
p.y = aBGPntData[matchPnt,2];
p.z = aBGPntData[matchPnt,3];
aBGPntData[matchPnt,5] = true;
}
// Moves UV coordinates for Cleanup mode
positionUV: p, matchPnt {
if (aBGPntData[matchPnt,5] == true) {
statsOverlapped += p;
}
var thisUV = aBGPntData[matchPnt,4];
uv[1] = thisUV.x; uv[2] = thisUV.y;
uvMap.setValue(p,uv);
aBGPntData[matchPnt,5] = true;
}
// Moves positions into relative morphs
positionMorph: p, matchPnt {
// Dirty, dirty solution, fix this.
/* if (morphCtr > 2) {
var oldMap = VMap(VMMORPH, morphPrefix + (morphCtr - 2).asStr());
if(oldMap.isMapped(p)) {
valold = oldMap.getValue(p);
morphMap = VMap(VMMORPH,morphPrefix + (morphCtr - 1).asStr(), 3);
val[1] = aBGPntData[matchPnt,1] + valold[1] - p.x;
val[2] = aBGPntData[matchPnt,2] + valold[2] - p.y;
val[3] = aBGPntData[matchPnt,3] + valold[3] - p.z;
} else {
if (morphCtr == 3) {
val[1] = aBGPntData[matchPnt,1] - p.x;
val[2] = aBGPntData[matchPnt,2] - p.y;
val[3] = aBGPntData[matchPnt,3] - p.z;
}
}
} else { */
val[1] = aBGPntData[matchPnt,1] - p.x;
val[2] = aBGPntData[matchPnt,2] - p.y;
val[3] = aBGPntData[matchPnt,3] - p.z;
// }
morphMap.setValue(p,val);
}
// Resets the morph back to the base layer
resetMorph {
new();
close();
}
/*
* Function to loop through the BG points and build an array of all x,y,z,uv coordinates
*
* @returns Nothing
*/
getBGData: uvMap
{
aBGPntData = nil;
editbegin();
var i = 1;
foreach(p, points)
{
// Increase the progress bar
if(monstep()){
editend(ABORT);
return true;
}
// Get the UV values
var uv = uvMap.getValue(p);
// Check so the point contains UV data
if(uv == nil){
// skip rest of the loop and continue with the next point
continue;
}
// If UV data was present
aBGPntData[i,1] = p.x;
aBGPntData[i,2] = p.y;
aBGPntData[i,3] = p.z;
aBGPntData[i,4] = <uv[1],uv[2],0>;
aBGPntData[i,5] = false;
i++;
}
editend();
return false;
}
/*
* Functions to handle point selections
*
* @returns Nothing
*/
// Stores all selected Point ID's in an array
storeSelPnts
{
editbegin();
foreach(p, points) {
selPnts += p;
}
editend();
}
// Selects all points stored in the selection array
getSelPnts
{
selmode(USER);
selpolygon(CLEAR); // Switch to polygon mode, to speed up drawing of point selections
selpoint(SET, POINTID, selPnts);
selpoint(SET,NPEQ,1000); // Switch back to point selection mode (Dummy selection value to keep current selection)
}
/*
* Create and handle the main window
* @since 1.0
* @modified 1.2
* @returns true/false (boolean) true for OK and false for Cancel.
*/
// Main Window, Returns false for cancel
open_main_window
{
load_config();
reqbegin("Conform By UV v" + cbuv_version);
reqsize(248,324 + 22); // Width, Height
// Get the path to the folder of the script
script_path = parse(getsep(),SCRIPTID);
script_folder = "";
for(i=1; i < script_path.size(); i++)
script_folder += script_path[i] + getsep();
// Check so the logo exists on disk
logo_path = script_folder + "JS_ConformByUV.tga";
logo_exists = File(logo_path, "rb");
if (!logo_exists)
error ("Error: Could not find the file 'JS_ConformByUV.tga'.");
logo_exists.close();
ctlLogo = ctlimage(logo_path);
ctlTol = ctlnumber("Tolerance", tolerance);
ctlMode = ctlpopup("Mode", operationMode, @ "Normal","Cleanup UV","Morph Batch" @);
ctlUnweldBG = ctlcheckbox("Unweld BG Data", unweldBG);
ctlSubD = ctlcheckbox("Subdivide BG Data", subdivideUV);
ctlUnweldFG = ctlcheckbox("Unweld & Merge FG", unweldFG);
ctlMorphCreate = ctlcheckbox("Create Morph", createMorph);
ctlMorphPfx = ctlstring("Morph Name: ", morphPrefix, 156);
ctlAbout = ctlbutton("About the Plugin", 73, "openAboutWin");
ctlSep1 = ctlsep();
ctlSep2 = ctlsep();
ctlSep3 = ctlsep();
// Positions
ctlposition(ctlLogo, 0,0);
var yComp = 78;
ctlposition(ctlMode, 53,10 + yComp);
ctlposition(ctlTol, 32, 32 + yComp, 204);
ctlposition(ctlSep1, 0, 60 + yComp);
ctlposition(ctlUnweldBG, 86, 68 + yComp, 150);
ctlposition(ctlSubD, 86, 90 + yComp, 150);
ctlposition(ctlUnweldFG, 86, 112 + yComp, 150);
ctlposition(ctlSep2, 0, 140 + yComp);
ctlposition(ctlMorphCreate, 86, 148 + yComp, 150);
ctlposition(ctlMorphPfx, 13, 148 + 22 + yComp);
ctlposition(ctlSep3, 0, 176 + 22 + yComp);
ctlposition(ctlAbout, 86, 184 + 22 + yComp, 150);
// Refresh controller on Mode Change
ctlrefresh(ctlMode, "refreshMainWin");
ctlactive(ctlMorphCreate, "toggleMorph", ctlMorphPfx);
// Default States
ctlUnweldFG.active(false);
if (!reqpost())
return false;
tolerance = getvalue(ctlTol);
operationMode = getvalue(ctlMode);
unweldBG = getvalue(ctlUnweldBG);
subdivideUV = getvalue(ctlSubD);
unweldFG = getvalue(ctlUnweldFG);
createMorph = getvalue(ctlMorphCreate);
morphPrefix = getvalue(ctlMorphPfx);
save_config();
reqend();
return true;
}
// Functions to refresh the main window
toggleMorph: value
{
return(value);
}
refreshMainWin: value
{
if (value == 1) {
ctlMorphPfx.label = "Morph Name:";
setvalue(ctlMorphPfx, "Morph");
ctlUnweldBG.active(true);
ctlSubD.active(true);
ctlUnweldFG.active(false);
ctlMorphCreate.active(true);
if (getvalue(ctlMorphCreate) == true) {
ctlMorphPfx.active(true);
} else {
ctlMorphPfx.active(false);
}
}
if (value == 2) {
ctlUnweldBG.active(true);
ctlSubD.active(true);
ctlUnweldFG.active(true);
ctlMorphCreate.active(false);
ctlMorphPfx.active(false);
}
if (value == 3) {
ctlMorphPfx.label = "Morph Prefix:";
setvalue(ctlMorphPfx, "Morph_");
ctlUnweldBG.active(false);
ctlSubD.active(false);
ctlUnweldFG.active(false);
ctlMorphCreate.active(false);
ctlMorphPfx.active(true);
}
requpdate();
}
// Result Window
openResultWin
{
reqbegin("Conform By UV");
reqsize(240,170); // X,Y
// Add result info here
c2 = ctltext("","Points without UVs: " + statsNoUV);
ctlposition(c2,10,10,200,13);
c3 = ctltext("","Unmatched Points: " + statsUnMatched.size());
ctlposition(c3,10,30,200,13);
c5 = ctltext("","Overlapping Points: " + statsOverlapped.size());
ctlposition(c5,10,50,200,13);
c10 = ctlcheckbox("Create selection set of unmatched points", false);
ctlposition(c10,10,76);
c11 = ctlcheckbox("Create selection set of overlapping points", false);
ctlposition(c11,10,100);
return if !reqpost();
// Create selection set of unmatched points
if (getvalue(c10) == true && statsUnMatched.size() != 0) {
selmode(USER);
editbegin();
selMap = VMap(VMSELECT,"UnMatched",1);
foreach(p,statsUnMatched)
{
selMap.setValue(p,1);
}
editend();
}
// Create selection set of overlapping points
if (getvalue(c11) == true && statsOverlapped.size() != 0) {
selmode(USER);
editbegin();
selMap = VMap(VMSELECT,"Overlap",1);
foreach(p,statsOverlapped)
{
selMap.setValue(p,1);
}
editend();
}
reqend();
}
// About Window
openAboutWin
{
reqbegin("About Conform By UV");
reqsize(330,160);
ctlText1 = ctltext("","Conform By UV", "Version: " + cbuv_version, "Build Date: " + cbuv_date);
ctlText4 = ctltext("","Programming by Johan Steen.");
ctlText5 = ctltext("","Ideas, Logo & Testing by Lee Perry-Smith.");
ctlSep1 = ctlsep();
ctlposition(ctlText1, 10, 10);
ctlposition(ctlSep1, 0, 64);
ctlposition(ctlText4, 10, 78);
ctlposition(ctlText5, 10, 100);
url_johan = "http://www.artstorm.net/";
url_lee = "http://www.ir-ltd.net/";
url_docs = "http://www.artstorm.net/plugins/conform-by-uv/";
ctlurl1 = ctlbutton("Artstorm", 100, "goto_url", "url_johan");
ctlurl2 = ctlbutton("Infinite Realities", 100, "goto_url", "url_lee");
ctlurl3 = ctlbutton("Help", 100, "goto_url", "url_docs");
ctlposition(ctlurl1, 220, 75);
ctlposition(ctlurl2, 220, 97);
ctlposition(ctlurl3, 220, 10);
return if !reqpost();
reqend();
}
/*
* Function to popup an information window
* @title The title of the window
* @message The message displayed to the user
* @winWidth The width of the window
* @returns nothing
*/
infoWindow: title, message, winWidth {
reqbegin(title);
reqsize(winWidth,70);
c0 = ctltext("",message);
ctlposition(c0,10,12);
return if !reqpost();
reqend();
}
/*
* Load and apply the configuration file
* @since 1.2
* @returns Nothing
*/
load_config
{
// Open file
loader = File(cfg_file, "r");
// Check if file was opened
if(loader) {
// Loop util eof
while( !loader.eof() ) {
parsed_line = loader.parse("\t");
// Apply the settings
if (parsed_line[1] == "tolerance") tolerance = number(parsed_line[2]);
if (parsed_line[1] == "operation_mode") operationMode = number(parsed_line[2]);
if (parsed_line[1] == "unweld_bg") unweldBG = number(parsed_line[2]);
if (parsed_line[1] == "subdivide_uv") subdivideUV = number(parsed_line[2]);
if (parsed_line[1] == "unweld_fg") unweldFG = number(parsed_line[2]);
if (parsed_line[1] == "create_morph") createMorph = number(parsed_line[2]);
if (parsed_line[1] == "morph_prefix") morphPrefix = string(parsed_line[2]);
}
// Close file
loader.close();
}
}
/*
* Save the current settings to the configuration file
* @since 1.2
* @returns Nothing
*/
save_config
{
// Open file
saver = File(cfg_file, "w");
// Check if the file was openend
if (saver) {
// Save the settings with tab separation
saver.writeln("tolerance", "\t", tolerance);
saver.writeln("operation_mode", "\t", operationMode);
saver.writeln("unweld_bg", "\t", unweldBG);
saver.writeln("subdivide_uv", "\t", subdivideUV);
saver.writeln("unweld_fg", "\t", unweldFG);
saver.writeln("create_morph", "\t", createMorph);
saver.writeln("morph_prefix", "\t", morphPrefix);
// Close file
saver.close();
}
}
/*
* Opens a website in the system's browser
* @since 1.1
* @modified 1.2
* @param url (string) URL to open
* @returns Nothing
*/
goto_url: url
{
// Query the environment for the command line interpreter
cmd_ln = string(getenv("comspec"));
// Spawn the process
spawn_id = spawn(cmd_ln, " /C start ", url);
// If the spawn was unsuccessful, notify the user
if (spawn_id == nil)
info ("Failed to open website ", url);
}