Advertisement
Guest User

zSlider 1.1

a guest
Aug 22nd, 2021
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.00 KB | None | 0 0
  1. ### zSlider 1.1 by heyw ###
  2. ###changes ####
  3. ### Updated to use matrix world space coordinates. This allows the script to work on parented or animated cameras.
  4. ### 1.1 ppdateby Mars ####
  5. # Usage:
  6.    
  7. #     1. Use the following python command to load the script or add it to your userSetup.py and restart maya
  8.  
  9. #        import zSlider
  10.  
  11. #     2. In the Hotkey Editor create 2 Runtime Commands with the start/stop
  12. #     functions(the language should be set to python):
  13.  
  14. #         zSlider.start()
  15.  
  16. #         and
  17.  
  18. #         zSlider.stop()
  19.  
  20. #     set the entry with 'zSliderStop()' to 'On Release'
  21. #     Assign both commands to the same key.
  22.  
  23. #     3. Press and hold the hotkey while moving the mouse left to right to slide the
  24. #     current selection towards/away from the camera.
  25.  
  26. #     4. The start has function 3 optional flags, these are the default values
  27.  
  28. #         zSlider.start(slideMode=0, slideSpeed=1000, doScaling=0)
  29.  
  30. #     slideMode=0
  31. #         the selection slides linearly with the amount of mouse movement
  32. #     slideMode=1
  33. #         the sliding has an exponential feel, as objects get closer they slide
  34. #         slower, further they slide faster
  35.  
  36. #     'slideSpeed' is the how many pixels the mouse must move to slide the selection
  37. #     a given distance, larger values = slower sliding.
  38.  
  39. #     doScaling=0
  40. #         scaling is disabled
  41. #     doScaling=1
  42. #         the object is scaled in proportion to the distance it's moved. The result is the
  43. #         object maintains its apparent size from the camera's perspective as it slides.
  44.  
  45. #     These can be used to assign multiple hotkeys with different behavior. ie. for general use
  46. #     assigning the 'a' key with:
  47.  
  48. #         zSlider.start(slideSpeed=500)
  49.  
  50. #     then for fine tuning assigning shift+a with:
  51.  
  52. #         zSlider.start(slideSpeed=4000)
  53.  
  54. #     IMPORTANT: If the tool gets stuck on use the following mel command to kill all script jobs:
  55.  
  56. #         scriptJob -ka;
  57.    
  58.  
  59. import maya.cmds as cmds
  60. import maya.mel as mel
  61. from ctypes import windll, Structure, c_long, byref
  62.  
  63.  
  64. class POINT(Structure):
  65.     _fields_ = [("x", c_long), ("y", c_long)]
  66.  
  67. def getCursorPos():
  68.     pt = POINT()
  69.     windll.user32.GetCursorPos(byref(pt))
  70.     return([pt.x,pt.y])
  71.    
  72. def start(slideMode=0, slideSpeed=8000, doScaling=0):
  73.    
  74.     # Find camera and get ws position
  75.     panel = cmds.getPanel(underPointer=True)
  76.    
  77.     if panel not in cmds.getPanel(type='modelPanel'):
  78.         print('model panel not found')
  79.         return
  80.     cam = cmds.modelPanel(panel,q=True,cam=True)
  81.     camPos = cmds.getAttr(cam+'.worldMatrix')
  82.    
  83.     # Store current offset for selected objects
  84.     sel = cmds.ls(sl=True)
  85.     selOffsets = []
  86.     for item in sel:
  87.         pos = cmds.getAttr(item+'.worldMatrix')
  88.         scale = cmds.xform(item,q=True,s=True,r=True)
  89.         selOffsets.append([pos[12]-camPos[12],pos[13]-camPos[13],pos[14]-camPos[14], scale[0],scale[1],scale[2]])
  90.         print selOffsets
  91.        
  92.     mouseX = getCursorPos()[0]
  93.    
  94.     cmds.undoInfo(openChunk=True)
  95.    
  96.     job = cmds.scriptJob(ie=('tick('+str(mouseX)+', '+str(camPos)+', '+str(map(lambda x: str(x), sel))+', '+str(selOffsets)+', '+str(slideMode)+', '+str(float(slideSpeed))+', '+str(doScaling)+')'));
  97.     mel.eval('global int $zSliderJob='+str(job))
  98.    
  99. def stop():
  100.     job = mel.eval('$tempMelVar=$zSliderJob')
  101.     cmds.scriptJob(kill=job)
  102.     cmds.undoInfo(closeChunk=True)
  103.    
  104. def tick(mouseX, camPos, sel, offsets, mode, speed, doScaling):
  105.     slideMode = 0
  106.     s = 1+((mouseX-getCursorPos()[0])/speed)
  107.     if slideMode is 0: f = s
  108.     if slideMode is 1: f = 1/s
  109.    
  110.     for x, offset in enumerate(offsets):
  111.         newPos = [f*offset[0]+camPos[12],f*offset[1]+camPos[13],f*offset[2]+camPos[14]]
  112.         newScale = [s*offset[3],s*offset[4],s*offset[5]]
  113.         cmds.move(newPos[0],newPos[1],newPos[2], sel[x],ws=True,spr=True)
  114.         if doScaling:
  115.             cmds.scale(newScale[0],newScale[1],newScale[2], sel[x])
  116.            
  117.            
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement