-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameAssetHelperScripts.py
More file actions
2579 lines (2021 loc) · 92.8 KB
/
Copy pathGameAssetHelperScripts.py
File metadata and controls
2579 lines (2021 loc) · 92.8 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
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
import maya.cmds as cmds
import maya.mel as mel
import re
import random as rand
import math
import maya.OpenMaya as om
################################################################################
## Functions
################################################################################
def takeSecond( elem ):
return elem[1]
def getShells( obj ):
# convert to vertices
verts = cmds.polyListComponentConversion( obj, toVertex=True )
# flatten the list
verts = cmds.ls( verts, flatten=True )
# define variables
remaining_verts = verts
shells = list()
while len( remaining_verts ) > 0:
# select the first shell
cmds.select( remaining_verts[0] )
# convert to shell
cmds.polySelectConstraint( shell=True, t=0x0001, m=2 )
# define the shell as the next shell
next_shell = cmds.ls( selection=True, flatten=True )
# clear the selection
cmds.select( clear=True )
# add it to the list of shells
shells.append( next_shell )
# remove the shell from the list of remaining verts
remaining_verts = set( remaining_verts )
next_shell = set( next_shell )
remaining_verts = remaining_verts.difference( next_shell )
remaining_verts = list( remaining_verts )
cmds.polySelectConstraint( shell=False )
return shells
def getPoleVerts( obj ) :
# convert to vertices
verts = cmds.polyListComponentConversion( obj, toVertex=True )
# flatten the list
verts = cmds.ls( verts, flatten=True )
# create an empty list to store the poles
poles = list()
# for all vertices
for v in verts:
# get the edges connected to the vertex and store it in a list
edges = cmds.polyListComponentConversion( v, toEdge=True )
# flatten the list
edges = cmds.ls( edges, flatten=True )
# if there are more than 4 edges
if len( edges ) > 4 :
# add it to the list of poles
poles.append( v )
# return the list of poles
return poles
def getSeamEdges( obj, poles ):
# create an empty set to store the edges
pole_edges = set()
# for each pole
for v in poles:
# convert the pole to edges
edges = cmds.polyListComponentConversion( v, toEdge=True )
# flatten the list
edges = cmds.ls( edges, flatten=True )
# convert to a set
edges = set( edges )
# merge the esges with the set of edges
pole_edges = pole_edges.union( edges )
# get the first edge
edge = list( pole_edges )[0]
# get the index of the edge
temp_string = edge.split( "[" )
temp_string = temp_string[1].split( "]" )[0]
edge_index = int( temp_string )
# convert the edge to an edge loop
cmds.polySelect( obj, edgeLoop=edge_index )
# list and flatten the selection
seam_edges = cmds.ls( selection=True, flatten=True )
# convert to a set
seam_edges = set( seam_edges )
# get the union of the seam and pole edges
result_edges = seam_edges.union( pole_edges )
# convert to a list
result_edges = list( result_edges )
# return
return result_edges
def normalizePoleTriangles( obj, poles ):
# get all faces connected to the pole
pole_faces = cmds.polyListComponentConversion( poles, toFace=True )
# flatten the list
pole_faces = cmds.ls( pole_faces, flatten=True )
# get all the uvs for the pole
pole_uvs = cmds.polyListComponentConversion( poles, toUV=True )
# flatten the list
pole_uvs = cmds.ls( pole_uvs, flatten=True )
# convert to a set
pole_uvs = set( pole_uvs )
# define a threshold
threshold = 0.01
for face in pole_faces:
face_uvs = cmds.polyListComponentConversion( face, toUV=True )
face_uvs = cmds.ls( face_uvs, flatten=True )
face_uvs = set( face_uvs )
pole_uv = face_uvs.intersection( pole_uvs )
non_pole_uv = face_uvs.difference( pole_uv )
non_pole_uv = list( non_pole_uv )
pole_uv = list( pole_uv )
# get the pole coordinates
pole_coord = cmds.polyEditUV( pole_uv, query=True )
case = 0
# check case
if pole_coord[0] > ( 1 - threshold ) and pole_coord[0] < ( 1 + threshold ):
if pole_coord[1] > ( 1 - threshold ) and pole_coord[1] < ( 1 + threshold ):
case = 1
else:
case = 2
else:
case = 3
# move the pole uv
cmds.polyEditUV( pole_uv, u=0.5, v=1.0, r=False )
uvc_red = cmds.polyEditUV( non_pole_uv[0], query=True )
uvc_blue = cmds.polyEditUV( non_pole_uv[1], query=True )
if case == 2:
if uvc_red[0] > uvc_blue[0]:
a_uv = non_pole_uv[0]
b_uv = non_pole_uv[1]
else:
a_uv = non_pole_uv[1]
b_uv = non_pole_uv[0]
cmds.polyEditUV( a_uv, u=0.0, v=0.0, r=False )
cmds.polyEditUV( b_uv, u=1.0, v=0.0, r=False )
if case == 3:
if uvc_red[1] > uvc_blue[1]:
a_uv = non_pole_uv[1]
b_uv = non_pole_uv[0]
else:
a_uv = non_pole_uv[0]
b_uv = non_pole_uv[1]
cmds.polyEditUV( a_uv, u=0.0, v=0.0, r=False )
cmds.polyEditUV( b_uv, u=1.0, v=0.0, r=False )
def unitizeAndLayout( obj, seams, poles, uv_set ):
# convert the object to edges
edges = cmds.polyListComponentConversion( obj, toEdge=True )
# flatten the list
edges = cmds.ls( edges, flatten=True )
# convert to a set
edges = set( edges )
# convert to a set
seams = set( seams )
# get the difference between the two sets
sew_edges = edges.difference( seams )
# convert to a list
sew_edges = list( sew_edges )
# set the current uv set
cmds.polyUVSet( obj, currentUVSet=True, uvSet=uv_set )
# unitize the object
cmds.polyForceUV( obj, unitize=True )
# normalize the pole triangles
normalizePoleTriangles( obj, poles )
# move and sew edges
cmds.polyMapSewMove( sew_edges, nf=10, lps=0, ch=1 )
# layout
cmds.polyLayoutUV( obj, lm=1, sc=2, se=2, rbf=0, fr=1, ps=0, l=2, gu=1, gv=1, ch=1 )
# get the uv shell
uv_shell = cmds.polyListComponentConversion( obj, toUV=True )
# select the uv shell
cmds.select( uv_shell )
# rotate the uv shell
cmds.polyEditUVShell( rot=True, angle=180, pu=0.5, pv=0.5 )
# clear the selection
cmds.select( clear=True )
def getVertsWithEdgeCount( obj, edge_count ):
verts = cmds.polyListComponentConversion( obj, toVertex=True )
verts = cmds.ls( verts, flatten=True )
# make a list to hold the verts with the specified edge count
edge_verts = list()
# get the verts
for v in verts:
edges = cmds.polyListComponentConversion( v, toEdge=True )
edges = cmds.ls( edges, flatten=True )
if len( edges ) == edge_count:
edge_verts.append( v )
return edge_verts
def getEdgeLengthSum( edges ):
total_length = 0
for edge in edges:
verts = cmds.polyListComponentConversion( edge, toVertex=True )
p = cmds.xform( verts, q=True, t=True, ws=True )
length = math.sqrt( math.pow( p[0] - p[3], 2 ) + math.pow( p[1] - p[4], 2 ) + math.pow( p[2] - p[5], 2 ) )
total_length = total_length + length
return total_length
def multVectorByScalar( scalar, vector ):
b = []
for i in range( len( vector ) ):
b.append( scalar * vector[i] )
return b
def customRename( obj, name_base, sep, padding, index, first_index, mode ):
if mode==1:
suffix = str( index + first_index ).rjust( padding, '0' )
if mode==2:
suffix = chr( ord('@') + index + first_index )
new_name = name_base + sep + suffix
cmds.rename( obj, new_name )
def isGroup( obj ):
is_a_group = False
# the object is a group if it is a transform and it has no shape nodes
if ( ( cmds.objectType( obj, isType="transform" ) ) and ( not ( cmds.listRelatives( obj, shapes=True ) ) ) ):
is_a_group = True
return is_a_group
def worldSpaceToScreenSpace(cam_obj, worldPoint):
# get the dagPath to the camera shape node to get the world inverse matrix
selList = om.MSelectionList()
selList.add(cam_obj)
dagPath = om.MDagPath()
selList.getDagPath(0,dagPath)
dagPath.extendToShape()
camInvMtx = dagPath.inclusiveMatrix().inverse()
# use a camera function set to get projection matrix, convert the MFloatMatrix
# into a MMatrix for multiplication compatibility
fnCam = om.MFnCamera(dagPath)
mFloatMtx = fnCam.projectionMatrix()
projMtx = om.MMatrix(mFloatMtx.matrix)
# multiply all together and do the normalisation
mPoint = om.MPoint(worldPoint[0],worldPoint[1],worldPoint[2]) * camInvMtx * projMtx;
x = (mPoint[0] / mPoint[3] / 2 + .5)
y = (mPoint[1] / mPoint[3] / 2 + .5)
return [x,y]
def getConnectedEdges( edge_list ):
# The function takes a list of edges and returns a list of edge groups.
# Each group contains edges that are connected.
edge_groups = list()
initial_edges = set( edge_list )
max_iter = 100
j = 1
while j > 0 and j < max_iter:
# make a set to hold the chosen edges
chosen_edges = set()
chosen_edges.add( next( iter( initial_edges ) ) )
i = 1
while i > 0 and i < max_iter:
# expand the selection
expanded_verts = cmds.polyListComponentConversion( chosen_edges, toVertex=True )
expanded_edges = cmds.polyListComponentConversion( expanded_verts, toEdge=True )
# flatten the list and convert to a set
expanded_edges = set( cmds.ls( expanded_edges, long=True, flatten=True ) )
# remove the chosen edges from the set
expanded_edges = expanded_edges.difference( chosen_edges )
# get the edges in the expansion contained in the initial list
found_edges = expanded_edges.intersection( initial_edges )
# if: the expanded selection contains edges from the initial list
if ( found_edges ):
chosen_edges = chosen_edges.union( found_edges )
i += 1
else:
i = 0
# add the chosen edges to the group list
edge_groups.append( list( chosen_edges ) )
# remove the chosen edges from the initial edges
initial_edges = initial_edges.difference( chosen_edges )
if ( initial_edges ):
j += 1
else:
j = 0
return edge_groups
# Sort Outliner
def getParentChildList( objs ):
parent_child_list = list()
for obj in objs:
parent = cmds.listRelatives( obj, parent=True, fullPath=True )
child = cmds.ls( obj, long=True )[0]
if parent==None:
parent = "root"
else:
parent = parent[0]
parent_child_pair = list()
parent_child_pair.append( parent )
parent_child_pair.append( child )
parent_child_list.append( parent_child_pair )
return parent_child_list
def getUniqueParents( objs ):
parents_set = set()
for obj in objs:
parent = cmds.listRelatives( obj, parent=True, fullPath=True )
if parent==None:
parent = "root"
else:
parent = parent[0]
parents_set.add( parent )
unique_parents = list( parents_set )
return unique_parents
def groupParentChildren( parent_child_list, unique_parents):
grouped_children = list()
for p in unique_parents:
grouped_children.append( list() )
for pair in parent_child_list:
for i in range( len( unique_parents ) ):
if pair[0] == unique_parents[i]:
grouped_children[i].append( pair[1] )
return grouped_children
def getAllChildrenFromParents( parents ):
all_children = list()
for p in parents:
all_children.append( list() )
for i in range( len( parents ) ):
if parents[i]=="root":
children = cmds.ls( assemblies=True, long=True )
else:
children = cmds.listRelatives( parents[i], fullPath=True, children=True )
for c in children:
all_children[i].append( c )
return all_children
def getObjsAboveSelected( unique_parents, sel_children, all_children ):
above_selected = list()
for p in unique_parents:
above_selected.append( list() )
for i in range( len( unique_parents ) ):
#print( "parent: " + unique_parents[i] )
for c in all_children[i]:
#print( "all child: " + c )
match_found = False
for s in sel_children[i]:
#print( "comparing: " + s + " and " + c )
if c==s:
match_found = True
break
if not match_found:
#print( "NOT FOUND" )
above_selected[i].append( c )
else:
break
return above_selected
def getChildTransforms( objs ):
child_transforms = list()
for obj in objs:
children = cmds.listRelatives( obj, fullPath=True, children=True )
transforms = cmds.ls( children, long=True, type="transform" )
for t in transforms:
child_transforms.append( t )
return child_transforms
def sortOutliner( objs, recursive, reverse ):
parent_child_list = getParentChildList( objs )
unique_parents = getUniqueParents( objs )
grouped_children = groupParentChildren( parent_child_list, unique_parents )
all_children = getAllChildrenFromParents( unique_parents )
above_selected = getObjsAboveSelected( unique_parents, grouped_children, all_children )
for i in range( len( grouped_children ) ):
grouped_children[i].sort()
if not reverse:
grouped_children[i].reverse()
for i in range( len( above_selected ) ):
above_selected[i].reverse()
for ls in grouped_children:
for child in ls:
cmds.reorder( child, front=True )
for ls in above_selected:
for child in ls:
cmds.reorder( child, front=True )
if recursive:
child_transforms = getChildTransforms( objs )
if child_transforms:
sortOutliner( child_transforms, True, reverse )
################################################################################
## Buttons
################################################################################
# Combine, Separate & History
def OnBtnSeparate( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
shells = getShells( obj )
if len( shells ) > 1:
# separate objects and store in a list
separated_objects = cmds.polySeparate( obj )
# delete history
cmds.delete( separated_objects, constructionHistory=True )
# remove non transforms from list
separated_objects = cmds.ls( separated_objects, type="transform", long=True )
# center pivots and rename
for mesh in separated_objects:
short_name = obj.split('|')[-1]
cmds.xform( mesh, cp=True )
cmds.rename( mesh, short_name )
cmds.select( sel )
def OnBtnCombine( isChecked, mode ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
if mode == 1:
parents = set()
# get the parents
for obj in sel:
obj_parent = cmds.listRelatives( obj, parent=True, fullPath=True )
if obj_parent == None:
obj_parent = "root"
else:
obj_parent = obj_parent[0]
parents.add( obj_parent )
# combine the objects
combined = cmds.polyUnite( sel, ch=True, mergeUVSets=True, centerPivot=True )[0]
# if the objects share a common parent, parent the combined object to the parent
if len( parents ) <= 1:
parents = list( parents )
if parents[0] != "root":
cmds.parent( combined, parents[0] )
# delete history
cmds.delete( combined, constructionHistory=True )
# rename the combined object to the first selection
short_name = sel[0].split('|')[-1]
cmds.rename( combined, short_name )
if mode == 2:
for grp in sel:
# get the number of objects in the group
num_children = len( cmds.listRelatives( grp ) )
# combine only if there is more than one object in the group
if num_children > 1:
# get the parent group
parent_grp = cmds.listRelatives( grp, parent=True, fullPath=True )
# combine the objects in the group
combined = cmds.polyUnite( grp, ch=True, mergeUVSets=True, centerPivot=True )[0]
# delete construction history
cmds.delete( combined, constructionHistory=True )
# get the group short names
short_name = grp.split('|')[-1]
# rename the new object
cmds.rename( short_name )
# store the new object
combined_obj = cmds.ls( selection=True )
# clear the selection
cmds.select( clear=True )
if parent_grp:
# parent the new object to the parent group
cmds.parent( combined_obj, parent_grp )
def OnBtnDeleteHistory( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
cmds.delete( obj, constructionHistory=True )
# Normals
def OnBtnFixNormals( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
cmds.polySetToFaceNormal( obj )
#cmds.polySoftEdge( obj, a=60, ch=0 )
cmds.polySoftEdge( obj, a=180 )
cmds.select( clear=True )
cmds.select( sel )
# Topology
def OnBtnDeleteExtruded( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
verts = cmds.polyListComponentConversion( obj, toVertex=True )
verts = cmds.ls( verts, flatten=True )
# make a list to hold the corner verts
corner_verts = list()
# make a list to hold two edge verts
two_edge_verts = list()
# get the corner verts
for v in verts:
edges = cmds.polyListComponentConversion( v, toEdge=True )
edges = cmds.ls( edges, flatten=True )
if len( edges ) <= 3:
corner_verts.append( v )
if len( edges ) == 2:
two_edge_verts.append( v )
if len( corner_verts ) >= 8 and len( two_edge_verts ) < 1:
# get the corner internal edges
corner_internal_edges = cmds.polyListComponentConversion( corner_verts, toEdge=True, internal=True )
corner_internal_edges = cmds.ls( corner_internal_edges, flatten=True )
corner_internal_edges = set( corner_internal_edges )
# get the corner edges
corner_edges = cmds.polyListComponentConversion( corner_verts, toEdge=True )
corner_edges = cmds.ls( corner_edges, flatten=True )
corner_edges = set( corner_edges )
# get the corner seam edges
corner_seam_edges = corner_edges.difference( corner_internal_edges )
corner_seam_edges = list( corner_seam_edges )
# get the seam edges
cmds.select( corner_seam_edges )
cmds.polySelectConstraint( pp=4, t=0x8000, m=2 )
seam_edges = cmds.ls( selection=True, flatten=True )
# get the seam faces
seam_verts = cmds.polyListComponentConversion( seam_edges, toVertex=True )
seam_faces = cmds.polyListComponentConversion( seam_verts, toFace=True, internal=True )
cmds.select( seam_faces )
cmds.delete( seam_faces )
cmds.select( verts[0] )
cmds.polySelectConstraint( shell=True, t=0x0001, m=2 )
back_faces = cmds.ls( selection=True, flatten=True )
back_faces = cmds.polyListComponentConversion( back_faces, toFace=True )
cmds.select( clear=True )
cmds.polySelectConstraint( shell=False )
cmds.delete( back_faces )
cmds.select( sel )
def OnBtnPolyRetopo( isChecked, face_count ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
cmds.polyRetopo( obj, targetFaceCount=face_count )
# UV
def OnBtnDeleteUV( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
# get the names of all uv sets
uv_sets = cmds.polyUVSet( obj, auv=True, query=True )
# if there are no uv sets, create one
if len( uv_sets ) == 0:
cmds.polyUVSet( obj, create=True, uvSet="map1" )
# otherwise, delete all uv sets except for the first and name it to map1
else:
# get the first uv set
first_uv_set = uv_sets[0]
# if there are additional uv sets, delete them
if len( uv_sets ) > 1:
uv_sets.pop( 0 )
for uv_set in uv_sets:
# delete the uv set
cmds.polyUVSet( obj, delete=True, uvSet=uv_set )
# check if map1 exists
if first_uv_set != "map1":
# rename the first uv set to map1
cmds.polyUVSet( obj, rename=True, uvSet=first_uv_set, newUVSet="map1" )
# set the current uv set
cmds.polyUVSet( obj, currentUVSet=True, uvSet="map1" )
# delete the contents of map1
cmds.polyMapDel( obj )
# delete construction history
cmds.delete( constructionHistory=True )
cmds.select( clear=True )
def onBtnScaleUvQuad( isChecked, uv_set ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
corner_verts = getVertsWithEdgeCount( obj, 2 )
corner_edges = cmds.polyListComponentConversion( corner_verts[0], toEdge=True )
corner_edges = cmds.ls( corner_edges, long=True, flatten=True )
# get a list for the edges in both dimensions from the corner
dimension_edges = list()
for e in corner_edges:
cmds.select( e )
cmds.polySelectConstraint( m2a=45, m3a=180, m=2, t=0x8000, pp=4 )
edge_list = cmds.ls( selection=True, flatten=True, long=True )
cmds.select( clear=True )
dimension_edges.append( edge_list )
'''
# determine the dimension with more edges
edges_for_length = list()
if len( dimension_edges[0] ) >= len( dimension_edges[1] ):
edges_for_length = dimension_edges[0]
else:
edges_for_length = dimension_edges[1]
'''
# get the length of the edges
edge_lengths = list()
for e_list in dimension_edges:
edge_lengths.append( getEdgeLengthSum( e_list ) )
#print( edge_lengths[0] )
#print( edge_lengths[1] )
# unitize the UVs
cmds.select( obj )
OnBtnUnitizeUVPlanar( True, uv_set )
a = edge_lengths[0] / edge_lengths[1]
b = edge_lengths[1] / edge_lengths[0]
length_ratio = a
cmds.polyUVSet( obj, uvs=uv_set, cuv=True )
uvs = cmds.polyListComponentConversion( obj, toUV=True )
cmds.polyEditUV( uvs, pu=0, pv=0, su=length_ratio )
cmds.select( sel )
def OnBtnDeleteUV( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
# get the names of all uv sets
uv_sets = cmds.polyUVSet( obj, auv=True, query=True )
# if there are no uv sets, create one
if len( uv_sets ) == 0:
cmds.polyUVSet( obj, create=True, uvSet="map1" )
# otherwise, delete all uv sets except for the first and name it to map1
else:
# get the first uv set
first_uv_set = uv_sets[0]
# if there are additional uv sets, delete them
if len( uv_sets ) > 1:
uv_sets.pop( 0 )
for uv_set in uv_sets:
# delete the uv set
cmds.polyUVSet( obj, delete=True, uvSet=uv_set )
# check if map1 exists
if first_uv_set != "map1":
# rename the first uv set to map1
cmds.polyUVSet( obj, rename=True, uvSet=first_uv_set, newUVSet="map1" )
# set the current uv set
cmds.polyUVSet( obj, currentUVSet=True, uvSet="map1" )
# delete the contents of map1
cmds.polyMapDel( obj )
# delete construction history
cmds.delete( constructionHistory=True )
cmds.select( clear=True )
def onBtnCameraProjectUV( isChecked, uv_set, projection_fill ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
cam = sel[-1]
objs = sel[:-1]
#projection_fill = 1 # 0=fill, 1=horizontal, 2=vertical
#uv_set = "map1"
res_width = float( cmds.getAttr('defaultResolution.width') )
res_height = float( cmds.getAttr('defaultResolution.height') )
# set the current uv set to work on
cmds.polyUVSet( objs, cuv=True, uvs=uv_set )
# make some basic uvs
for obj in objs:
cmds.polyPlanarProjection( obj, ch=True, ibd=True, md="z" )
# convert the objs to vertex
uvs = cmds.polyListComponentConversion( objs, toUV=True )
uvs = cmds.ls( uvs, flatten=True, l=True )
# move each uv
for uv in uvs:
# convert to vertex
vertex = cmds.polyListComponentConversion( uv, toVertex=True )
vertex = cmds.ls( vertex, flatten=True, l=True )[0]
# get world position
world_pos = cmds.xform( vertex, t=True, ws=True, query=True )
# convert to screenspace
uv_coord = worldSpaceToScreenSpace( cam, world_pos )
# edit the uv
cmds.polyEditUV( uv, r=False, uValue=uv_coord[0], vValue=uv_coord[1], uvs=uv_set )
# print progress
complete = round( ( float( uvs.index( uv ) ) + 1 ) / len( uvs ) * 100 )
print( "calculating: " + str( complete ) + "%..." )
# set scale values depending on fill method
# horizontal
if projection_fill == 1:
scale_u = 1
scale_v = res_height / res_width
# vertical
elif projection_fill ==2:
scale_u = res_width / res_height
scale_v = 1
# fill
else:
scale_u = 1
scale_v = 1
# adjust the overall scale of the uvs
cmds.polyEditUV( uvs, scaleU=scale_u, scaleV=scale_v, pivotU=0.5, pivotV=0.5, uvs=uv_set )
# reset the selection
cmds.select( sel )
def onBtnFitUVsToDimension( isChecked, scale_dimension ):
# Scales and lays out the uvs on the selected objects to fit the zero to one uv space in the given dimension.
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
#scale_dimension = "u"
for obj in sel:
# convert the selection to uvs
sel_uv = cmds.polyListComponentConversion( obj, toUV=True )
sel_uv = cmds.ls( sel_uv, long=True, flatten=True )
# make a list to store the uv name and values
uv_name_val = list()
for uv in sel_uv:
new_entry = list()
uv_value = cmds.polyEditUV( uv, query=True )
new_entry.append( uv )
new_entry.append( uv_value[0] )
new_entry.append( uv_value[1] )
uv_name_val.append( new_entry )
for elem in uv_name_val:
print( elem )
# from the user input, get the array element
if scale_dimension == "u":
elem_for_scale = 1
else:
elem_for_scale = 2
# get the dimension
uv_name_val.sort( key=lambda x: float(x[elem_for_scale]) )
dimension = abs( uv_name_val[-1][elem_for_scale] - uv_name_val[0][elem_for_scale] )
# get the min uv
uv_name_val.sort( key=lambda x: float(x[1]) )
min_u_val = uv_name_val[0][1]
uv_name_min_u = list()
for elem in uv_name_val:
if elem[1] <= min_u_val:
uv_name_min_u.append( elem )
uv_name_min_u.sort( key=lambda x: float(x[2]) )
min_uv_name = uv_name_min_u[0][0]
# scale the uvs
v_scale = 1 / dimension
cmds.polyEditUV( sel_uv, su=v_scale, sv=v_scale )
# reposition the uvs at the origin
min_uv_val = cmds.polyEditUV( min_uv_name, query=True )
cmds.polyEditUV( sel_uv, u=0-min_uv_val[0], v=0-min_uv_val[1] )
# Multi UV Set Workflow
def OnBtnInitializeUV( isChecked ):
# get the selected objects
sel = cmds.ls( selection=True, long=True )
# throw an error if nothing is selected
if (not sel):
cmds.confirmDialog( title='ERROR', message=('ERROR: Nothing selected.'), button=['OK'], defaultButton='OK' )
return -1
for obj in sel:
# get the names of all uv sets
uv_sets = cmds.polyUVSet( obj, auv=True, query=True )
# if there are no uv sets, create one
if len( uv_sets ) == 0:
cmds.polyUVSet( obj, create=True, uvSet="map1" )
# otherwise, delete all uv sets except for the first and name it to map1
else:
# get the first uv set
first_uv_set = uv_sets[0]
# if there are additional uv sets, delete them
if len( uv_sets ) > 1:
uv_sets.pop( 0 )
for uv_set in uv_sets:
# delete the uv set
cmds.polyUVSet( obj, delete=True, uvSet=uv_set )
# check if map1 exists
if first_uv_set != "map1":
# rename the first uv set to map1
cmds.polyUVSet( obj, rename=True, uvSet=first_uv_set, newUVSet="map1" )
# get the faces of the selected objects
faces = cmds.polyListComponentConversion( sel, toFace=True )
# planar map the uvs
cmds.polyProjection( faces, type="Planar", ibd=True, kir=True, md="z", ch=True, uvSetName="map1" )
# create two new uv sets
for obj in sel:
# create new uv sets
cmds.polyCopyUV( obj, uvSetNameInput="map1", uvSetName="UnitizeUV", createNewMap=True, ch=True )