SHARE
TWEET

Untitled

a guest Dec 11th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import maya.cmds as cmds
  2.  
  3. if cmds.window("MyWindow", exists=True):
  4.     cmds.deleteUI("MyWindow")
  5.  
  6. if cmds.windowPref("MyWindow", exists=True):
  7.     cmds.windowPref("MyWindow", remove=True)
  8.  
  9. myWin = cmds.window("MyWindow", title="Weapons", width=400, height=300, tlb=True)
  10.  
  11. cmds.showWindow("MyWindow")
  12.  
  13. mainLayout = cmds.columnLayout(parent=myWin, adj=1, cal='left', cat=('both', 5))
  14.  
  15. rigFrameLayout = cmds.frameLayout("MyRigFrameLayout", parent=mainLayout, collapse=1, label="Weapon Rig", collapsable=1, borderVisible=0)
  16.  
  17. btnMainLayout = cmds.columnLayout(p=rigFrameLayout, adj=False, cw=100, cat=('both', 1))
  18.  
  19. btnMainLoc = cmds.button("btnMainLoc_id", label="Create Main", parent=btnMainLayout, command="createWpnMainLocator()")
  20.  
  21.  
  22. wpnPartLayout = cmds.rowLayout("wpnPartLayout", parent=rigFrameLayout, numberOfColumns=2, nc=2, cw2=[200, 200],
  23.                                cat=[(1, 'both', 0), (2, 'both', 0)])
  24.  
  25. btnCreatePart = cmds.button("btn01_ID01", label="Create Part", parent=wpnPartLayout, command="createPartsOfWeapon()")
  26. createPartTxtField = cmds.textField("partNameID01", parent=wpnPartLayout, width=200, height=25, placeholderText="Part Name")
  27.  
  28. wpnPartChaildLayout = cmds.rowLayout("wpnPartChaildLayoutID", parent=rigFrameLayout, numberOfColumns=4,
  29.                                      cw4=[100, 100, 100, 100], cat=[(1, 'both', 0), (2, 'left', 0)])
  30.  
  31. countPartCheckBox = cmds.checkBox(l='Count of parts', parent=wpnPartChaildLayout, onCommand="enableCountParts()", offCommand="disableCountParts()")
  32. createPartTxtField = cmds.textField("textFieldID01", editable=False, parent=wpnPartChaildLayout, width=99, height=25, placeholderText="*")
  33.  
  34. btnBldSkeletonLayout = cmds.columnLayout(p=rigFrameLayout, adj=False, cat=('both', 1), rowSpacing=10)
  35.  
  36. btnBldSkeleton = cmds.button("btnBldSkeleton_id", label="Build Skeleton", parent=btnBldSkeletonLayout, command="createWpnMainLocator()")
  37. btnBldControls = cmds.button("btnBldControls_id", label="Build Controls", parent=btnBldSkeletonLayout, command="createWpnMainLocator()")
  38.  
  39. snapFrameLayout = cmds.frameLayout("MySnapFrameLayout", parent=mainLayout, collapse=1, label="Snap Weapon", collapsable=1, borderVisible=0)
  40.  
  41. animationFrameLayout = cmds.frameLayout("MyAnimationFrameLayout", parent=mainLayout, collapse=1, label="Animation Weapon", collapsable=1, borderVisible=0)
  42.  
  43. exportFrameLayout = cmds.frameLayout("MyExportFrameLayout", parent=mainLayout, collapse=1, label="Export Animation", collapsable=1, borderVisible=0)
  44.  
  45.  
  46. def enableCountParts():
  47.     cmds.textField("textFieldID01", e=True, editable=True)
  48.    
  49.    
  50. def disableCountParts():
  51.     field = cmds.textField("textFieldID01", e=True, editable=False)
  52.     empty = field.replace(field, "")
  53.     emptyCountParts = cmds.textField("textFieldID01", e=True, text=empty)
  54.  
  55.  
  56. def createWpnMainLocator():
  57.     cmds.spaceLocator(name='wpn_Main_loc')
  58.     cmds.setAttr('wpn_Main_loc.rotateX', 90)
  59.     cmds.setAttr('wpn_Main_loc.rotateY', 0)
  60.     cmds.setAttr('wpn_Main_loc.rotateZ', 180)
  61.  
  62.  
  63. def createPartsOfWeapon():
  64.    
  65.     partName = cmds.textField("partNameID01", q=True, text=True)
  66.     countParts = cmds.textField("textFieldID01", q=True, text=True)
  67.            
  68.     if len(partName) == 0:
  69.    
  70.         cmds.warning('Enter some name of weapon part')
  71.         pass
  72.    
  73.     elif cmds.objExists('wpn_Main_loc'):
  74.        
  75.         if countParts:
  76.            
  77.             listLoc = []
  78.        
  79.             for i in range(int(countParts)):
  80.            
  81.                 locatorParts = cmds.spaceLocator(name=partName + "_" + str(i+1) + "_loc")
  82.                 cmds.setAttr(partName + "_" + str(i+1) + "_loc" + '.rotateX', 90)
  83.                 cmds.setAttr(partName + "_" + str(i+1) + "_loc" + '.rotateY', 0)
  84.                 cmds.setAttr(partName + "_" + str(i+1) + "_loc" + '.rotateZ', 180)
  85.                                
  86.                 '''
  87.                 partTX = cmds.getAttr(partName + "_" + str(i+1) + "_loc" + '.rotateX')
  88.                 partTY = cmds.getAttr(partName + "_" + str(i+1) + "_loc" + '.rotateY')
  89.                 partTZ = cmds.getAttr(partName + "_" + str(i+1) + "_loc" + '.rotateZ')
  90.                 mainTX = cmds.getAttr('wpn_Main_loc.translateX')
  91.                 mainTY = cmds.getAttr('wpn_Main_loc.translateY')
  92.                 mainTZ = cmds.getAttr('wpn_Main_loc.translateZ')
  93.                
  94.                 connectCurv = cmds.curve(d=1, p=[(mainTX, mainTY, mainTZ), (partTX, partTY, partTZ)], name=partName + "_" + str(i+1) + "_loc" + "_crv")
  95.                
  96.                 cmds.connectAttr(partName + "_" + str(i+1) + "_loc" + '.translate', partName + "_" + str(i+1) + "_loc" + "_crv" + '.controlPoints[0]', force=True)
  97.                 cmds.connectAttr('wpn_Main_loc.translate', partName + "_" + str(i+1) + "_loc" + "_crv" + '.controlPoints[1]', force=True)
  98.                 '''
  99.                 listLoc.append(locatorParts)
  100.                
  101.             for i, obj in enumerate(listLoc):
  102.                 parentObj = obj
  103.                 childObj = "parentObj" + str(i+1)
  104.                
  105.                 cmds.parent((i+1) + "_loc", partName + "_" + str(i) + "_loc")
  106.            
  107.            
  108.                    
  109.             pass
  110.        
  111.         else:
  112.            
  113.             locatorParts = cmds.spaceLocator(name=partName + "_loc")
  114.             cmds.setAttr(partName + "_loc" + '.rotateX', 90)
  115.             cmds.setAttr(partName + "_loc" + '.rotateY', 0)
  116.             cmds.setAttr(partName + "_loc" + '.rotateZ', 180)
  117.            
  118.             partTX = cmds.getAttr(partName + "_loc" + '.rotateX')
  119.             partTY = cmds.getAttr(partName + "_loc" + '.rotateY')
  120.             partTZ = cmds.getAttr(partName + "_loc" + '.rotateZ')
  121.             mainTX = cmds.getAttr('wpn_Main_loc.translateX')
  122.             mainTY = cmds.getAttr('wpn_Main_loc.translateY')
  123.             mainTZ = cmds.getAttr('wpn_Main_loc.translateZ')
  124.            
  125.             connectCurv = cmds.curve(d=1, p=[(mainTX, mainTY, mainTZ), (partTX, partTY, partTZ)], name=partName + "_loc" + "_crv")
  126.            
  127.             cmds.connectAttr(partName + "_loc" + '.translate', partName + "_loc" + "_crv" + '.controlPoints[0]', force=True)
  128.             cmds.connectAttr('wpn_Main_loc.translate', partName + "_loc" + "_crv" + '.controlPoints[1]', force=True)
  129.        
  130.             return connectCurv, locatorParts
  131.    
  132.     else:
  133.        
  134.         cmds.warning('no "wpn_Main_loc" exists')
  135.                
  136.  
  137. templates = createPartsOfWeapon()
  138.  
  139.  
  140. def cleanScene(tempParts):
  141.     cmds.delete('wpn_Main_loc')
  142.  
  143.     for t in tempParts:
  144.         cmds.delete(t)
  145.  
  146.  
  147. cleanScene(templates)
  148.  
  149.  
  150. def buildSkeleton():
  151.     if cmds.objExists('*_loc'):
  152.  
  153.         locators = cmds.ls('*loc*', transforms=True)
  154.         cmds.select(cl=True)
  155.  
  156.         for l in locators:
  157.             cmds.select(cl=True)
  158.             locatorPosTranslation = cmds.xform(l, q=True, translation=True, ws=True)
  159.             locatorPosRotation = cmds.xform(l, q=True, rotation=True, ws=True)
  160.             splitLocNames = l.replace('_loc', '')
  161.             rigJointNames = 'rig_bn_' + splitLocNames
  162.             rigJoints = cmds.joint(radius=1, orientation=locatorPosRotation, position=locatorPosTranslation,
  163.                                    name=rigJointNames)
  164.             cmds.select(cl=True)
  165.             skinJointNames = 'bn_' + splitLocNames
  166.             skinJoints = cmds.joint(radius=1, orientation=locatorPosRotation, position=locatorPosTranslation,
  167.                                     name=skinJointNames)
  168.             cmds.select(cl=True)
  169.             cmds.parentConstraint(rigJoints, skinJoints, mo=False)
  170.  
  171.         allJoints = cmds.ls(type='joint')
  172.  
  173.         listRigJoints = []
  174.         listSkinJoints = []
  175.  
  176.         for a in allJoints:
  177.  
  178.             if not 'rig_' in a:
  179.                 listSkinJoints.append(a)
  180.  
  181.             if 'rig_' in a:
  182.                 listRigJoints.append(a)
  183.  
  184.         for l in listRigJoints:
  185.  
  186.             if not 'wpn_Main' in l:
  187.                 cmds.parent(l, 'rig_bn_wpn_Main')
  188.  
  189.         for l in listSkinJoints:
  190.  
  191.             if not 'wpn_Main' in l:
  192.                 cmds.parent(l, 'bn_wpn_Main')
  193.  
  194.         cleanScene(templates)
  195.  
  196.  
  197.  
  198.     else:
  199.         cmds.warning('no "wpn_Main_loc" exists')
  200.  
  201.  
  202. buildSkeleton()
  203.  
  204.  
  205. def buildControls():
  206.     listJoints = cmds.ls('rig*')
  207.  
  208.     for l in listJoints:
  209.  
  210.         if 'wpn_Main' not in l:
  211.  
  212.             controlName = l.replace('rig_bn_', '')
  213.             partControl = cmds.curve(name=controlName + '_ctr',
  214.                                      p=[(-1, 1, -1), (-1, 1, 1), (1, 1, 1), (1, 1, -1), (-1, 1, -1),
  215.                                         (-1, -1, -1), (1, -1, -1), (1, 1, -1), (1, 1, 1), (1, -1, 1),
  216.                                         (1, -1, -1), (-1, -1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1),
  217.                                         (-1, 1, 1), (-1, -1, 1)], d=1,
  218.                                      k=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
  219.             cmds.group(name=controlName + '_offset_grp')
  220.             ctrZeroGrp = cmds.group(name=controlName + '_grp_0')
  221.             snap = cmds.parentConstraint(l, ctrZeroGrp, mo=False)
  222.             cmds.setAttr(controlName + '_ctr' + '.overrideEnabled', 1)
  223.             cmds.setAttr(controlName + '_ctr' + '.overrideColor', 6)
  224.             cmds.delete(snap)
  225.             cmds.parentConstraint(partControl, l, mo=False)
  226.  
  227.             if 'magazine' in partControl:
  228.                 cmds.addAttr(partControl, at='enum', keyable=True, en='World: wpn_Main: L_Hand: R_Hand',
  229.                              ln='Parent_Space')
  230.  
  231.         if 'wpn_Main' in l:
  232.  
  233.             controlName = l.replace('rig_bn_', '')
  234.             c1 = cmds.circle(nr=[1, 0, 0], c=(0, 0, 4), r=1, d=3, s=8, ch=False)[0]
  235.             c2 = cmds.circle(nr=[0, 1, 0], c=(0, 0, 4), r=1, d=3, s=8, ch=False)[0]
  236.             c3 = cmds.circle(nr=[0, 0, 1], c=(0, 0, 4), r=1, d=3, s=8, ch=False)[0]
  237.             n1 = cmds.curve(d=1, p=[(0, 0, 0), (0, 0, 3)])
  238.             mainControl = cmds.createNode('transform', name=controlName + '_ctr')
  239.  
  240.             for c in c1, c2, c3, n1:
  241.                 cmds.parent(cmds.listRelatives(c, s=True)[0], control, r=True, s=True)
  242.                 cmds.delete(c)
  243.             cmds.setAttr(mainControl + '.overrideEnabled', 1)
  244.             cmds.setAttr(mainControl + '.overrideColor', 17)
  245.             cmds.select(clear=True)
  246.             ctrMainZeroGrp = cmds.createNode('transform', name=controlName + '_grp_0')
  247.             cmds.parent(mainControl, ctrMainZeroGrp)
  248.             snapCtrMainZeroGrp = cmds.parentConstraint(l, ctrMainZeroGrp, mo=False)
  249.             cmds.delete(snapCtrMainZeroGrp)
  250.             cmds.parentConstraint(mainControl, l, mo=True)
  251.  
  252.     # find wpn name
  253.     nameWpnList = cmds.ls('*_wpn_geo')
  254.     nameWpnGeo = ' '.join(map(str, nameWpnList))
  255.     wpnName = nameWpnGeo.split('_wpn_geo')[0]
  256.  
  257.     # buildControlsStructure
  258.     allControlGrp = cmds.ls('*_grp_0')
  259.     cmds.group(allControlGrp, name=wpnName + '_rig_grp')
  260.     excludePartList = {'wpn_Main_grp_0', 'magazine_grp_0'}
  261.     wpnPartCtrGrp = [_ for _ in allControlGrp if _ not in excludePartList]
  262.  
  263.     for a in wpnPartCtrGrp:
  264.  
  265.         if 'wpn_Main' and 'magazine' not in a:
  266.             cmds.parent(a, 'wpn_Main_ctr')
  267.     cmds.select(clear=True)
  268.  
  269.  
  270. buildControls()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top