/
display_lib.py
766 lines (676 loc) · 31.4 KB
/
display_lib.py
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
from subprocess import call
import maya.cmds as cmds
import maya.mel as mel
import webrImport as web
#
# web
cn = web.mod( 'constraint_lib' )
ac = web.mod( 'animCurve_lib' )
def message( what = '', maya = True ):
what = '-- ' + what + ' --'
if maya:
mel.eval( 'print \"' + what + '\";' )
else:
print( what )
def findControl( ann = '', panelTyp = '', split = 3 ):
# split = which parent in full path to return
cntrls = cmds.lsUI( controls = True, long = 1 )
qualified = []
result = []
for con in cntrls:
if cmds.control( con, exists = True ):
string = cmds.control( con, q = 1, ann = 1 )
if ann in string:
qualified.append( con )
print( qualified )
for item in qualified:
path = cmds.control( item, q = 1, fpn = 1 )
if panelTyp in path:
stf = path.split( '|' )
tmp = stf[len( stf ) - split]
result.append( cmds.control( tmp, q = 1, fpn = 1 ) )
else:
print( '______searching ', path )
return result
def findControlParent( control = '', split = 3 ):
# split = which parent in full path to return
path = cmds.control( control, q = 1, fpn = 1 )
stf = path.split( '|' )
tmp = stf[len( stf ) - split]
result = cmds.control( tmp, q = 1, fpn = 1 )
return result
def geButton( name = '', parent = '', attach = None, label = '', cmd = '', gap = 2, w = 26, h = 19, bg = [0.21, 0.21, 0.21], ann = '' ):
# "boldLabelFont", "smallBoldLabelFont", "tinyBoldLabelFont", "plainLabelFont", "smallPlainLabelFont",
# "obliqueLabelFont", "smallObliqueLabelFont", "fixedWidthFont" and "smallFixedWidthFont"
if not cmds.control( name, ex = 1 ):
cmd = cmd
# btn = cmds.iconTextButton(name, p=parent, style='textOnly', stp='python', c=cmd, al='center', h=h, bgc=bg, mw=1, w=w, label=label, ann=ann)
btn = cmds.button( name, p = parent, c = cmd, al = 'center', h = h, bgc = bg, w = w, label = label, ann = ann )
ac = [( btn, 'left', gap, attach )]
attachForm = [( btn, 'bottom', 3 )]
if attach:
cmds.formLayout( parent, e = True, ac = ac, attachForm = attachForm )
else:
attachForm = [( btn, 'left', 2 ), ( btn, 'bottom', 3 )]
cmds.formLayout( parent, e = True, attachForm = attachForm )
return btn
else:
print( '' )
cmds.deleteUI( name, control = True )
def geHeading( name = '', parent = '', attach = None, label = '', cmd = '', gap = 10, ann = '', bgc = [0.275, 0.275, 0.275], w = 45, h = 17 ):
if not cmds.control( name, ex = 1 ):
lab = cmds.text( name, l = label, fn = 'obliqueLabelFont', al = 'center', bgc = bgc, ebg = True, ann = ann, w = w, h = h )
ac = [( lab, 'left', gap, attach )]
attachForm = [( lab, 'bottom', 4 )]
if attach:
cmds.formLayout( parent, e = True, ac = ac, attachForm = attachForm )
else:
cmds.formLayout( parent, e = True, attachForm = attachForm )
return lab
else:
print( '' )
cmds.deleteUI( name, control = True )
def geField( name = '', parent = '', attach = None, label = '', cmd = '', w = 100, gap = 10, tx = '', ann = '' ):
if not cmds.control( name, ex = 1 ):
# field = cmds.textField( name, tx = tx, h = 21, w = w, cc = cmd, ec = cmd, ann = ann, aie = True )
field = cmds.textField( name, tx = tx, h = 21, w = w, ec = cmd, ann = ann, aie = True ) # no change command
ac = [( field, 'left', gap, attach )]
attachForm = [( field, 'left', 2 ), ( field, 'bottom', 2 )]
if attach:
cmds.formLayout( parent, edit = True, ac = ac, attachForm = attachForm )
else:
cmds.formLayout( parent, edit = True, attachForm = attachForm )
return field
else:
print( '' )
cmds.deleteUI( name, control = True )
def geFieldTx( v = '' ):
ui = GraphEditorButtonNames()
try:
tx = cmds.textField( ui.fil, q = 1, tx = 1 )
if not tx:
cmds.textField( ui.fil, e = 1, tx = v )
import graphFilter
reload( graphFilter )
graphFilter.graphEditorCMD()
else:
cmds.textField( ui.fil, e = 1, tx = '' )
import graphFilter
graphFilter.graphEditorCMD()
except:
pass
def colorOff():
return [0.97, 0.92, 0.04] # yellow
def colorOn():
return [0.13, 0.77, 0.11] # green
def graphEditorButtons( *args ):
# TODO: add button to distribute keys on selected curves
# TODO: use dif layout, window gets locked to a min size
# TODO: should work on all graph editors, needs to be smarter
# TODO: add isolate selected curves
# ui names
ui = GraphEditorButtonNames()
# check where to build
build = False
p = findControl( ann = 'Move Nearest Picked Key Tool', panelTyp = 'graphEditor', split = 3 )
remove = findControl( ann = 'Enter text to limit the listing', panelTyp = 'graphEditor', split = 2 )
pnl = cmds.getPanel( wf = True )
if len( p ) == 1:
build = True
pnl = 'graphEditor'
else:
if 'graphEditor' in pnl:
build = True
else:
cmds.warning( '-- Multiple graph editors... Focus on appropriate graph editor. --' )
return False
# build
if not cmds.control( ui.filD, ex = 1 ):
bgc = [0.4, 0.4, 0.4]
# bgc = [0.3, 0.48, 0.57]
bgcd = [0.97, 0.92, 0.04] # yellow
if build:
p = findControl( ann = 'Move Nearest Picked Key Tool', panelTyp = pnl, split = 3 )[0]
# print '_______ ', findControl(ann='Indicates that either text filter', panelTyp=pnl, split=2)
remove = findControl( ann = 'Enter text to limit the listing', panelTyp = pnl, split = 2 )[0]
cmds.control( remove, e = 1, m = 0 )
cmds.formLayout( p, e = 1, h = 52 )
cmds.setParent( p )
#
# "import webrImport as web\nds = web.mod('display_lib')\nds.geFieldTx('tx,ty,tz,rx,ry,rz')"
# build
item = geButton( name = ui.filD, parent = p, label = '::',
cmd = "import webrImport as web\nds = web.mod('display_lib')\nds.geFieldTx('tx,ty,tz,rx,ry,rz')",
gap = 2, w = 15, bg = [0.2, 0.3, 0.5], ann = "add default channel filter: tr*,ro*" )
item = geField( name = ui.fil, parent = p, attach = item,
cmd = "import webrImport as web\ngf = web.mod('graphFilter')\ngf.graphEditorCMD()",
gap = 2, w = 185, ann = "add channel filters by name and/or wild cards: 't*X'" )
item = geButton( name = ui.hldPre, parent = p, attach = item, label = '<--',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.holdCrv(postCurrent=False)",
gap = 5, bg = [0.4, 0.2, 0.2], ann = 'hold value of selected curve to the left' )
item = geButton( name = ui.hldAll, parent = p, attach = item, label = 'HOLD',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.holdCrv()",
gap = 0, w = 45, bg = [0.5, 0.2, 0.2], ann = 'hold value of selected curve' )
item = geButton( name = ui.hldPost, parent = p, attach = item, label = '-->',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.holdCrv(preCurrent=False)",
gap = 0, bg = [0.4, 0.2, 0.2], ann = 'hold value of selected curve to the right' )
item = geButton( name = ui.sclDwn, parent = p, attach = item, label = '-',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.scaleCrv(0.99)",
gap = 5, bg = [0.1, 0.4, 0.4], ann = 'scale curve:\npivot @ key = keys with same values selected\npivot @ 0 = keys with dif values selected\n' )
item = geHeading( name = ui.sclTx, parent = p, attach = item, label = 'SCALE', gap = 0, w = 45, bgc = [0.25, 0.4, 0.4] )
item = geButton( name = ui.sclUp, parent = p, attach = item, label = '+',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.scaleCrv(1.01)",
gap = 0, bg = [0.1, 0.4, 0.4], ann = 'scale curve:\npivot @ key = keys with same values selected\npivot @ 0 = keys with dif values selected\n' )
item = geButton( name = ui.flp, parent = p, attach = item, label = 'FLIP',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.scaleCrv(-1)",
gap = 0, w = 50, bg = [0.1, 0.4, 0.4] )
item = geButton( name = ui.movDwn, parent = p, attach = item, label = '-',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.moveValue(False)",
gap = 5, bg = [0.3, 0.3, 0.5], ann = 'nudge keys/curves down in value' )
item = geHeading( name = ui.movTx, parent = p, attach = item, label = 'VALUE', gap = 0, bgc = [0.35, 0.35, 0.5] )
item = geButton( name = ui.movUp, parent = p, attach = item, label = '+',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.moveValue(True)",
gap = 0, bg = [0.3, 0.3, 0.5], ann = 'nudge keys/curves up in value' )
item = geButton( name = ui.movTmLf, parent = p, attach = item, label = '<',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.moveTime(True)",
gap = 5, bg = [0.2, 0.3, 0.5], ann = 'move keys/curves to the left' )
item = geHeading( name = ui.movTmTx, parent = p, attach = item, label = 'TIME', gap = 0, bgc = [0.3, 0.35, 0.5] )
item = geButton( name = ui.movTmRt, parent = p, attach = item, label = '>',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.moveTime(False)",
gap = 0, bg = [0.2, 0.3, 0.5], ann = 'move keys/curves to the right' )
item = geHeading( name = ui.sclTmTx, parent = p, attach = item, label = 'SCALE TIME', w = 65 )
item = geField( name = ui.sclTmBy, parent = p, attach = item,
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.animScale(cmds.textField('scaleTimeBy',query=True,tx=True))",
gap = 2, w = 40, tx = 1.0, ann = 'Scale selected curves from the first frame of playback' )
w = 105
item = geButton( name = ui.sftSel, parent = p, attach = item, label = 'SOFT Select',
cmd = "import webrImport as web\ncss = web.mod('curveSoftSelect')\ncss.toggleSelJob()",
gap = 20, w = w, bg = colorOff() )
item = geButton( name = ui.sbfrm, parent = p, attach = item, label = 'SUB frame X',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.subframe()",
gap = 0, w = w, bg = bgc, ann = 'subframes to whole frames ' )
item = geButton( name = ui.unfy, parent = p, attach = item, label = 'UNIFY',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.unifyKeys()",
gap = 0, w = w, bg = bgcd )
item = geButton( name = ui.smth, parent = p, attach = item, label = 'SMOOTH',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.smoothKeys()",
gap = 0, w = w, bg = bgc )
item = geButton( name = ui.expTgl, parent = p, attach = item, label = 'COLLAPSE',
cmd = "import webrImport as web\ngf = web.mod('graphFilter')\ngf.toggleExpand()",
gap = 0, w = w, bg = bgcd )
item = geButton( name = ui.bkInfty, parent = p, attach = item, label = 'BAKE Results',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.bakeInfinity()",
gap = 0, w = w, bg = bgc )
item = geButton( name = ui.infntyLnr, parent = p, attach = item, label = 'LINEAR Infinity',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.setInfinityLinear()",
gap = 0, w = w, bg = bgcd, ann = 'make infinity linear' )
item = geButton( name = ui.infntyCns, parent = p, attach = item, label = 'CONSTANT Infinity',
cmd = "import webrImport as web\nac = web.mod('animCurve_lib')\nac.setInfinityConstant()",
gap = 0, w = w, bg = bgc, ann = 'make infinity constant' )
else:
# clean UI
d = ui.__dict__
try:
for s in d:
if cmds.control( str( d[s] ), ex = 1 ):
cmds.deleteUI( d[s], control = True )
except:
print( 'cant delete all', ui.__dict__ )
for item in p:
cmds.formLayout( item, e = 1, h = 28 )
for item in remove:
cmds.control( item, e = 1, m = 1 )
class GraphEditorButtonNames():
def __init__( self ):
# filter
self.filD = 'FilterDefault'
self.fil = 'graphFilter'
# hold
self.hldPre = 'holdPre'
self.hldAll = 'holdAll'
self.hldPost = 'holdPost'
# scale
self.sclDwn = 'scaleDown'
self.sclTx = 'scaleTxt'
self.sclUp = 'scaleUp'
self.flp = 'FlipCurves'
# value move
self.movDwn = 'moveDown'
self.movTx = 'moveValue'
self.movUp = 'moveUp'
# time move
self.movTmLf = 'moveLeft'
self.movTmTx = 'moveTime'
self.movTmRt = 'moveRight'
# scale time
self.sclTmTx = 'scale'
self.sclTmBy = 'scaleTimeBy'
# subframe out
self.sbfrm = 'subframeOut'
# unify keys
self.unfy = 'unifyKeys'
# bake infinity
self.bkInfty = 'bakeInfinity'
# smooth keys
self.smth = 'smoothKeys'
# soft seleft
self.sftSel = 'softSelKeys'
# expand toggleGeo
self.expTgl = 'expandAttrToggle'
# expand toggleGeo
self.infntyLnr = 'infntyLnrCrvs'
# expand toggleGeo
self.infntyCns = 'infntyCnsCrvs'
def toggleObjectDisplay( purpose ):
'''
purpose = 'anim'
purpose = 'cam'
'''
# get active and find type of panel
currentPanel = cmds.getPanel( withFocus = True )
panelType = cmds.getPanel( to = currentPanel )
if panelType == 'modelPanel':
# all off
cmds.modelEditor( currentPanel, e = True, alo = 0 )
cmds.modelEditor( currentPanel, e = True, imagePlane = 1 )
cmds.modelEditor( currentPanel, e = True, pluginShapes = 1 )
# arguments
if purpose == 'anim':
# specific on
cmds.modelEditor( currentPanel, e = True, nurbsCurves = 1 )
cmds.modelEditor( currentPanel, e = True, polymeshes = 1 )
cmds.modelEditor( currentPanel, e = True, locators = 1 )
cmds.modelEditor( currentPanel, e = True, cameras = 1 )
cmds.modelEditor( currentPanel, e = True, handles = 1 )
cmds.modelEditor( currentPanel, e = True, hud = 1 )
cmds.modelEditor( currentPanel, e = True, grid = 1 )
message( 'Panel display set for animation' )
elif purpose == 'cam':
# specific on
cmds.modelEditor( currentPanel, e = True, cameras = 1 )
cmds.modelEditor( currentPanel, e = True, polymeshes = 1 )
cmds.modelEditor( currentPanel, e = True, hud = 0 )
cmds.modelEditor( currentPanel, e = True, grid = 0 )
cmds.modelEditor( currentPanel, e = True, wireframeOnShaded = 0 )
message( 'Panel display set for camera' )
else:
message( 'Current panel is of the wrong type' )
def toggleGeo():
pnl = cmds.getPanel( withFocus = True )
if cmds.getPanel( to = pnl ) == 'modelPanel':
state = cmds.modelEditor( pnl, q = True, polymeshes = True )
if state:
cmds.modelEditor( pnl, e = True, polymeshes = 0 )
cmds.modelEditor( pnl, e = True, nurbsSurfaces = 0 )
cmds.modelEditor( pnl, e = True, pluginShapes = 0 )
else:
cmds.modelEditor( pnl, e = True, polymeshes = 1 )
cmds.modelEditor( pnl, e = True, nurbsSurfaces = 1 )
cmds.modelEditor( pnl, e = True, pluginShapes = 1 )
def toggleCurves():
pnl = cmds.getPanel( withFocus = True )
if cmds.getPanel( to = pnl ) == 'modelPanel':
state = cmds.modelEditor( pnl, q = True, nurbsCurves = True )
if state:
cmds.modelEditor( pnl, e = True, nurbsCurves = 0 )
cmds.modelEditor( pnl, e = True, locators = 0 )
else:
cmds.modelEditor( pnl, e = True, nurbsCurves = 1 )
cmds.modelEditor( pnl, e = True, locators = 1 )
def toggleCameras():
pnl = cmds.getPanel( withFocus = True )
if cmds.getPanel( to = pnl ) == 'modelPanel':
state = cmds.modelEditor( pnl, q = True, cameras = True )
if state:
cmds.modelEditor( pnl, e = True, cameras = 0 )
cmds.modelEditor( pnl, e = True, imagePlane = 0 )
# cmds.modelEditor( pnl, e = True, pluginShapes = 0 )
else:
cmds.modelEditor( pnl, e = True, cameras = 1 )
cmds.modelEditor( pnl, e = True, imagePlane = 1 )
# cmds.modelEditor( pnl, e = True, pluginShapes = 1 )
def toggleRes():
sel = cmds.ls( sl = True )
c = ':c_master_CTRL.'
attrHi = 'hiResGeoVis'
attrLo = 'loResGeoVis'
name = sel[0].split( ':' )[0]
if cmds.getAttr( name + c + attrHi ) == 1:
cmds.setAttr( name + c + attrHi, 0 )
cmds.setAttr( name + c + attrLo, 1 )
message( 'LO res' )
else:
cmds.setAttr( name + c + attrHi, 1 )
cmds.setAttr( name + c + attrLo, 0 )
message( 'HI res' )
def shapeSize( obj = None, mltp = 1 ):
'''\n
mltp = size multiplier of shape nodes
'''
if obj is None:
# make a list from selection
obj = cmds.ls( sl = True, l = True )
if obj:
shapeSize( obj, mltp )
else:
message( 'nothing selected' )
elif type( obj ) == list:
# no need to accomodate
for i in obj:
shapeSize( i, mltp )
else:
# obj must be a single item, make a list
obj = [obj]
# run the loop on list
for item in obj:
shape = cmds.listRelatives( item, s = True, f = True )
if shape:
for node in shape:
if 'SharedAttr' not in node:
cmds.scale( mltp, mltp, mltp, node + '.cv[*]' )
def locSize( lc, mltp = 1 ):
axis = ['X', 'Y', 'Z']
for axs in axis:
sz = cmds.getAttr( lc + 'Shape.localScale' + axs )
cmds.setAttr( lc + 'Shape.localScale' + axs, sz * mltp )
def changeColor( color = 17, shapes = False ):
sel = cmds.ls( sl = True )
if sel:
if shapes:
sel = shapeNodes()
for item in sel:
cmds.setAttr( item + '.overrideEnabled', 1 )
cmds.setAttr( item + '.overrideColor', color )
else:
message( 'Select an object' )
def shapeNodes():
sel = cmds.ls( sl = 1 )
shapes = []
if sel:
for s in sel:
shape = cmds.listRelatives( s, s = True, f = True )
if shape:
for shp in shape:
shapes.append( shp )
return shapes
def worldSpdExp( sel, attr ):
exp = "float $currentPos[] = `getAttr -t (frame) " + sel + ".worldMatrix`;\nfloat $lastPos[] = `getAttr -t (frame-1) " + sel + ".worldMatrix`;\nfloat $speed = abs(mag (<<0*" + sel + ".tx+$currentPos[12]-$lastPos[12], 0*" + sel + ".ty+$currentPos[13]-$lastPos[13], 0*" + sel + ".tz+$currentPos[14]-$lastPos[14]>>) );\nfloat $inKPH = $speed*24*60*60/100000;\n" + sel + "." + attr + " = $inKPH;"
return exp
def localSpdExp( sel, attr ):
exp = "float $lastPosX = `getAttr -t (frame-1) " + sel + ".tx`;\nfloat $lastPosY = `getAttr -t (frame-1) " + sel + ".ty`;\nfloat $lastPosZ = `getAttr -t (frame-1) " + sel + ".tz`;\nfloat $speed = abs(mag (<<" + sel + ".translateX-$lastPosX," + sel + ".translateY-$lastPosY," + sel + ".translateZ-$lastPosZ>>) );\nfloat $inKPH = $speed*24*60*60/100000;\n" + sel + "." + attr + " = $inKPH;"
return exp
def createSpeedAttr( sel, attr, exp ):
cmds.addAttr( sel, longName = attr, attributeType = 'float', k = 0 )
cmds.setAttr( sel + "." + attr, cb = 1 )
cmds.expression( o = sel, s = exp )
def speed( world = True, local = True ):
selected = cmds.ls( sl = True )
if len( selected ) != 0:
for sel in selected:
if world:
attr = 'worldSpeed'
exp = worldSpdExp( sel, attr )
if not cmds.attributeQuery( attr, node = sel, ex = True ):
createSpeedAttr( sel, attr, exp )
else:
cmds.warning( '-- Speed attr (' + attr + ') already exists - ' + sel + ' ! --' )
deleteUserAttr( sel = sel, att = attr )
return None
if local:
attr = 'localSpeed'
exp = localSpdExp( sel, attr )
if not cmds.attributeQuery( attr, node = sel, ex = True ):
createSpeedAttr( sel, attr, exp )
else:
cmds.warning( '-- Speed attr (' + attr + ') already exists - ' + sel + ' ! --' )
deleteUserAttr( sel = sel, att = attr )
return None
else:
message( 'Select an object.' )
def rpmExp( sel, attr ):
'''
exp = "
float $lastPosX = `getAttr -t (frame-1) " + sel + ".tx`;\nfloat $lastPosY = `getAttr -t (frame-1) " + sel + ".ty`;\n
float $lastPosZ = `getAttr -t (frame-1) " + sel + ".tz`;\n
float $speed = abs(mag (<<" + sel + ".translateX-$lastPosX," + sel + ".translateY-$lastPosY," + sel + ".translateZ-$lastPosZ>>) );\n
float $inKPH = $speed*24*60*60/100000;\n
" + sel + "." + attr + " = $inKPH;"
return exp
'''
pass
def createRpmAttr( sel, attr, exp ):
cmds.addAttr( sel, longName = attr, attributeType = 'float', k = 0 )
cmds.setAttr( sel + "." + attr, cb = 1 )
cmds.expression( o = sel, s = exp )
def rpmAvg( obj, attr ):
'''
3 frame average
'''
# 360/(fps * 60) = degrees
# 360/(24 * 60) = 0.25
frames = [cmds.currentTime( q = True ) - 1, cmds.currentTime( q = True ), cmds.currentTime( q = True ) + 1]
val = []
rpm = []
for frame in frames:
val.append( cmds.getAttr( obj + '.' + attr, t = frame ) )
list( set( val ) )
if len( val ) != 1:
rpm.append( ( val[0] - val[1] ) / 0.25 )
rpm.append( ( val[1] - val[2] ) / 0.25 )
# average of 2 values
return sum( rpm, 0.0 ) / len( rpm )
else:
return 0.0
def rpm( world = True, local = True ):
selected = cmds.ls( sl = True )
if len( selected ) != 0:
for sel in selected:
if world:
attr = 'worldSpeed'
exp = worldSpdExp( sel, attr )
if not cmds.attributeQuery( attr, node = sel, ex = True ):
createRpmAttr( sel, attr, exp )
else:
cmds.warning( '-- Speed attr (' + attr + ') already exists - ' + sel + ' ! --' )
deleteUserAttr( sel = sel, att = attr )
return None
if local:
attr = 'localSpeed'
exp = localSpdExp( sel, attr )
if not cmds.attributeQuery( attr, node = sel, ex = True ):
createRpmAttr( sel, attr, exp )
else:
cmds.warning( '-- Speed attr (' + attr + ') already exists - ' + sel + ' ! --' )
deleteUserAttr( sel = sel, att = attr )
return None
else:
message( 'Select an object.' )
def plotExp( sel, attr ):
exp = "float $nextPosX = `getAttr -t (frame+1) " + sel + ".rx`;\nfloat $lastPosX = `getAttr -t (frame-1) " + sel + ".rx`;\nfloat $speed = abs(mag (<<" + sel + ".rotateX,$lastPosX, $nextPosX>>) );\n" + sel + "." + attr + " = $speed;"
return exp
def createPlotAttr( sel, attr, exp ):
cmds.addAttr( sel, longName = attr, attributeType = 'float', k = 0 )
cmds.setAttr( sel + "." + attr, cb = 1 )
cmds.expression( o = sel, s = exp )
def plotAttr():
selected = cmds.ls( sl = True, fl = True )
if len( selected ) != 0:
sel = selected[0]
attr = 'plot'
exp = plotExp( sel, attr )
if not cmds.attributeQuery( attr, node = sel, ex = True ):
createPlotAttr( sel, attr, exp )
else:
cmds.warning( '-- Plotted attr (' + attr + ') already exists - ' + sel + ' ! --' )
deleteUserAttr( sel = sel, exp = True, att = attr )
return None
else:
message( 'Select an object.' )
def measureDis( obj1, obj2 ):
'''
math for distance between 2 objects
'''
p1 = cmds.xform( obj1, q = True, ws = True, t = True )
p2 = cmds.xform( obj2, q = True, ws = True, t = True )
v = [0, 0, 0]
v[0] = p1[0] - p2[0]
v[1] = p1[1] - p2[1]
v[2] = p1[2] - p2[2]
distance = v[0] * v[0] + v[1] * v[1] + v[2] * v[2]
from math import sqrt
distance = sqrt( distance )
return distance
def createDisAttr( sel, attr ):
'''
creates attr and applies expression
'''
cmds.addAttr( sel, longName = attr, attributeType = 'float', k = True )
cmds.setAttr( sel + "." + attr, cb = 1 )
def createDisNode( name = '' ):
dis = cmds.createNode( 'distanceBetween', name = name + '_dis' )
return dis
def distance( obj1 = None, obj2 = None ):
'''
assembles distance relationship
'''
selected = None
attr = 'distance'
st = []
disNode = None
if not obj1:
selected = cmds.ls( sl = True, fl = True )
else:
selected = [obj1, obj2]
if len( selected ) == 2:
st.append( cmds.attributeQuery( attr, node = selected[0], ex = True ) )
st.append( cmds.attributeQuery( attr, node = selected[1], ex = True ) )
st = list( set( st ) )
if len( st ) == 1:
st = st[0]
# toggle distance attr
for sel in selected:
if not st:
createDisAttr( sel, attr )
else:
# list
if not disNode:
disNode = clean( cmds.listConnections( sel + '.worldMatrix[0]', s = False, d = True, type = 'distanceBetween' ) )
cmds.deleteAttr( sel + '.' + attr )
# toggle distance node
if st:
if disNode:
cmds.delete( disNode )
else:
# connect to distance node
disNode = createDisNode( name = 'dstnc___' + selected[0] + '___' + selected[1] )
cmds.connectAttr( selected[0] + '.worldMatrix[0]', disNode + '.inMatrix1' )
cmds.connectAttr( selected[1] + '.worldMatrix[0]', disNode + '.inMatrix2' )
# feed distance attrs
cmds.connectAttr( disNode + '.distance', selected[0] + '.' + attr )
cmds.connectAttr( disNode + '.distance', selected[1] + '.' + attr )
# reselect objects
cmds.select( selected )
else:
message( 'one object already have a distance attr, aborting.' )
elif selected:
if cmds.attributeQuery( attr, node = selected[0], ex = True ):
# remove distance from pair, delete dis node
disNode = clean( cmds.listConnections( selected[0] + '.worldMatrix[0]', s = False, d = True, type = 'distanceBetween' ) )
if disNode:
nodes = clean( cmds.listConnections( disNode + '.distance', s = False, d = True ) )
if len( nodes ) == 2:
distance( obj1 = nodes[0], obj2 = nodes[1] )
else:
message( 'A pair to the selection was not found' )
else:
cmds.warning( '-- Select 2 objects to toggle distance attributes. --' )
def clean( obj ):
if obj:
if isinstance( obj, list ):
if len( obj ) == 1:
return obj[0]
else:
return list( set( obj ) )
else:
return obj
else:
return None
def deleteUserAttr( sel = None, att = '' ):
if sel is None:
sel = cmds.ls( sl = True )
if len( sel ) != 1:
message( 'Select one object' )
return None
else:
sel = sel[0]
selectedAttr = cmds.channelBox( 'mainChannelBox', q = True, sma = True )
# userAttr = cmds.listAttr(sel, ud=True)
if att != '':
cmds.deleteAttr( sel + '.' + att )
message( 'Attribute ' + att + ' deleted' )
return None
if selectedAttr is not None:
for attr in selectedAttr:
cmds.deleteAttr( sel + '.' + attr )
else:
message( 'Select an attribute(s) in the channelBox.' )
def rotateManip():
mode = cmds.manipRotateContext( 'Rotate', q = True, mode = True )
if mode == 0:
cmds.manipRotateContext( 'Rotate', e = True, mode = 2 )
message( 'Gimbal' )
elif mode == 2:
cmds.manipRotateContext( 'Rotate', e = True, mode = 0 )
message( 'Local' )
elif mode == 1:
cmds.manipRotateContext( 'Rotate', q = True, mode = 2 )
message( 'Gimbal' )
def translateManip():
mode = cmds.manipMoveContext( 'Move', q = True, mode = True )
# print mode
if mode == 0:
cmds.manipMoveContext( 'Move', e = True, mode = 1 )
message( 'Local' )
elif mode == 1:
cmds.manipMoveContext( 'Move', e = True, mode = 2 )
message( 'World' )
elif mode == 2:
cmds.manipMoveContext( 'Move', e = True, mode = 0 )
message( 'Object' )
def clearKey():
mel.eval( 'timeSliderClearKey;' )
def altFrame( *args ):
# cmds.nodeType(sel), object type
# optimize, real slow
pnl = cmds.getPanel( withFocus = True )
typ = cmds.getPanel( typeOf = pnl )
if typ == 'modelPanel':
sel = cmds.ls( sl = True, fl = True )
gs = ac.GraphSelection()
locs = []
if sel:
for item in sel:
if cmds.objectType( item, i = 'transform' ) or cmds.objectType( item, i = 'joint' ):
loc = cn.locator( obj = item, ro = 'zxy', X = 0.35, constrain = False, shape = False )[0]
locs.append( loc )
else:
try:
print( cmds.listRelatives( item, parent = True )[0], '_______________' )
loc = cn.locator( obj = cmds.listRelatives( item, parent = True )[0], ro = 'zxy', X = 0.35, constrain = False, shape = False )
print( loc, '_____________________' )
loc = loc[0]
locs.append( loc )
except:
message( 'didnt frame object: ' + item )
cmds.select( locs )
mel.eval( "fitPanel -selected;" )
cmds.delete( locs )
gs.reselect()
else:
message( 'select an object' )
else:
mel.eval( "fitPanel -selected;" )
def polyToggle():
pnl = cmds.getPanel( wf = 1 )
st = cmds.modelEditor( pnl, q = 1, polymeshes = 1 )
cmds.modelEditor( pnl, e = 1, polymeshes = ( not st ) )