SHARE
TWEET

Untitled

a guest Oct 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###############################################################################
  2. # Name:
  3. #   baker.py
  4. #
  5. # Author:
  6. #   Hyuk Ko (kohyuk91@gmail.com)
  7. #
  8. # Copyright (C) 2019 Hyuk Ko. All rights reserved.
  9. ###############################################################################
  10.  
  11.  
  12. # Use this code to launch Baker:
  13. """  
  14. import baker
  15. try:
  16.     b.close()
  17.     b.deleteLater()
  18. except:
  19.     pass
  20. b = baker.Baker()
  21. """
  22.  
  23.  
  24. import maya.cmds as mc
  25. import maya.OpenMaya as om
  26. import maya.OpenMayaUI as omui
  27. from Qt import QtCore, QtGui, QtWidgets, QtCompat
  28.  
  29. import traceback
  30.  
  31. class Baker(QtWidgets.QDialog):
  32.     @classmethod
  33.     def maya_main_window(cls):
  34.         main_window_ptr = omui.MQtUtil.mainWindow()
  35.         return QtCompat.wrapInstance(long(main_window_ptr), QtWidgets.QWidget)
  36.  
  37.     def __init__(self):
  38.         super(Baker, self).__init__(self.maya_main_window())
  39.         self.setMaximumWidth(200)
  40.  
  41.         self.setWindowTitle("Baker")
  42.         self.setWindowFlags(self.windowFlags() ^ QtCore.Qt.WindowContextHelpButtonHint)
  43.         #self.setStyleSheet("QDialog {background-color: rgb(25, 25, 25);} QGroupBox {background-color: rgb(35, 35, 35); border: 10px solid rgb(35, 35, 35);}")
  44.  
  45.         self.create_widgets()
  46.         self.create_layouts()
  47.         self.create_connections()
  48.  
  49.         self.show()
  50.  
  51.     def create_widgets(self):
  52.         self.start_frame_lb = QtWidgets.QLabel("Start")
  53.         self.start_frame_lb.setAlignment(QtCore.Qt.AlignCenter)
  54.         self.end_frame_lb = QtWidgets.QLabel("End")
  55.         self.end_frame_lb.setAlignment(QtCore.Qt.AlignCenter)
  56.  
  57.         self.start_frame_le = QtWidgets.QLineEdit()
  58.         self.start_frame_le.setText(str(int(mc.playbackOptions(q=True, minTime=True))))
  59.         self.this_frame_update_btn = QtWidgets.QPushButton()
  60.         self.this_frame_update_btn.setIcon(QtGui.QIcon(":teSetKeyframe.png"))
  61.         self.end_frame_le = QtWidgets.QLineEdit()
  62.         self.end_frame_le.setText(str(int(mc.playbackOptions(q=True, maxTime=True))))
  63.  
  64.         self.start_frame_update_btn = QtWidgets.QPushButton()
  65.         self.start_frame_update_btn.setIcon(QtGui.QIcon(":timestart.png"))
  66.         self.reset_update_btn = QtWidgets.QPushButton()
  67.         self.reset_update_btn.setIcon(QtGui.QIcon(":refresh.png"))
  68.         self.end_frame_update_btn = QtWidgets.QPushButton()
  69.         self.end_frame_update_btn.setIcon(QtGui.QIcon(":timeend.png"))
  70.  
  71.         self.point_x_cb = QtWidgets.QCheckBox("X")
  72.         self.point_x_cb.toggle()
  73.         self.point_y_cb = QtWidgets.QCheckBox("Y")
  74.         self.point_y_cb.toggle()
  75.         self.point_z_cb = QtWidgets.QCheckBox("Z")
  76.         self.point_z_cb.toggle()
  77.  
  78.         self.orient_x_cb = QtWidgets.QCheckBox("X")
  79.         self.orient_x_cb.toggle()
  80.         self.orient_y_cb = QtWidgets.QCheckBox("Y")
  81.         self.orient_y_cb.toggle()
  82.         self.orient_z_cb = QtWidgets.QCheckBox("Z")
  83.         self.orient_z_cb.toggle()
  84.  
  85.         self.scale_x_cb = QtWidgets.QCheckBox("X")
  86.         self.scale_x_cb.toggle()
  87.         self.scale_y_cb = QtWidgets.QCheckBox("Y")
  88.         self.scale_y_cb.toggle()
  89.         self.scale_z_cb = QtWidgets.QCheckBox("Z")
  90.         self.scale_z_cb.toggle()
  91.  
  92.         self.rotateOrder_inherit_rb = QtWidgets.QRadioButton("Inherit")
  93.         self.rotateOrder_inherit_rb.toggle()
  94.         self.rotateOrder_xyz_rb = QtWidgets.QRadioButton("XYZ")
  95.         self.rotateOrder_yzx_rb = QtWidgets.QRadioButton("YZX")
  96.         self.rotateOrder_zxy_rb = QtWidgets.QRadioButton("ZXY")
  97.         self.rotateOrder_xzy_rb = QtWidgets.QRadioButton("XZY")
  98.         self.rotateOrder_yxz_rb = QtWidgets.QRadioButton("YXZ")
  99.         self.rotateOrder_zyx_rb = QtWidgets.QRadioButton("ZYX")
  100.  
  101.         self.apply_btn = QtWidgets.QPushButton("Apply")
  102.         self.bake_btn = QtWidgets.QPushButton("Bake")
  103.  
  104.     def create_layouts(self):
  105.         frame_range_groupbox = QtWidgets.QGroupBox("Range")
  106.         frame_range_layout = QtWidgets.QGridLayout()
  107.         frame_range_layout.addWidget(self.start_frame_lb, 0, 0)
  108.         frame_range_layout.addWidget(self.end_frame_lb, 0, 2)
  109.         frame_range_layout.addWidget(self.start_frame_le, 1, 0)
  110.         frame_range_layout.addWidget(self.this_frame_update_btn, 1, 1)
  111.         frame_range_layout.addWidget(self.end_frame_le, 1, 2)
  112.         frame_range_layout.addWidget(self.start_frame_update_btn, 2, 0)
  113.         frame_range_layout.addWidget(self.reset_update_btn, 2, 1)
  114.         frame_range_layout.addWidget(self.end_frame_update_btn, 2, 2)
  115.         frame_range_groupbox.setLayout(frame_range_layout)
  116.  
  117.         self.point_groupbox = QtWidgets.QGroupBox("Point")
  118.         point_layout = QtWidgets.QHBoxLayout()
  119.         point_layout.addWidget(self.point_x_cb)
  120.         point_layout.addWidget(self.point_y_cb)
  121.         point_layout.addWidget(self.point_z_cb)
  122.         self.point_groupbox.setLayout(point_layout)
  123.         self.point_groupbox.setCheckable(True)
  124.         self.point_groupbox.setChecked(True)
  125.  
  126.         self.orient_groupbox = QtWidgets.QGroupBox("Orient")
  127.         orient_layout = QtWidgets.QHBoxLayout()
  128.         orient_layout.addWidget(self.orient_x_cb)
  129.         orient_layout.addWidget(self.orient_y_cb)
  130.         orient_layout.addWidget(self.orient_z_cb)
  131.         self.orient_groupbox.setLayout(orient_layout)
  132.         self.orient_groupbox.setCheckable(True)
  133.         self.orient_groupbox.setChecked(True)
  134.  
  135.         self.scale_groupbox = QtWidgets.QGroupBox("Scale")
  136.         scale_layout = QtWidgets.QHBoxLayout()
  137.         scale_layout.addWidget(self.scale_x_cb)
  138.         scale_layout.addWidget(self.scale_y_cb)
  139.         scale_layout.addWidget(self.scale_z_cb)
  140.         self.scale_groupbox.setLayout(scale_layout)
  141.         self.scale_groupbox.setCheckable(True)
  142.         self.scale_groupbox.setChecked(True)
  143.  
  144.         rotateOrder_groupbox = QtWidgets.QGroupBox("Rotate Order")
  145.         rotateOrder_layout = QtWidgets.QGridLayout()
  146.         rotateOrder_layout.addWidget(self.rotateOrder_inherit_rb, 0, 0, 1, 0)
  147.         rotateOrder_layout.addWidget(self.rotateOrder_xyz_rb, 1, 0)
  148.         rotateOrder_layout.addWidget(self.rotateOrder_yzx_rb, 1, 1)
  149.         rotateOrder_layout.addWidget(self.rotateOrder_zxy_rb, 1, 2)
  150.         rotateOrder_layout.addWidget(self.rotateOrder_xzy_rb, 2, 0)
  151.         rotateOrder_layout.addWidget(self.rotateOrder_yxz_rb, 2, 1)
  152.         rotateOrder_layout.addWidget(self.rotateOrder_zyx_rb, 2, 2)
  153.         rotateOrder_groupbox.setLayout(rotateOrder_layout)
  154.  
  155.         bake_groupbox = QtWidgets.QGroupBox()
  156.         bake_layout = QtWidgets.QHBoxLayout()
  157.         bake_layout.addWidget(self.apply_btn)
  158.         bake_layout.addWidget(self.bake_btn)
  159.         bake_groupbox.setLayout(bake_layout)
  160.  
  161.         main_layout = QtWidgets.QVBoxLayout(self)
  162.         main_layout.addWidget(frame_range_groupbox)
  163.         main_layout.addWidget(self.point_groupbox)
  164.         main_layout.addWidget(self.orient_groupbox)
  165.         main_layout.addWidget(self.scale_groupbox)
  166.         main_layout.addWidget(rotateOrder_groupbox)
  167.         main_layout.addWidget(bake_groupbox)
  168.  
  169.     def create_connections(self):
  170.         self.this_frame_update_btn.clicked.connect(lambda: self.update("this"))
  171.         self.start_frame_update_btn.clicked.connect(lambda: self.update("start"))
  172.         self.reset_update_btn.clicked.connect(lambda: self.update("reset"))
  173.         self.end_frame_update_btn.clicked.connect(lambda: self.update("end"))
  174.         self.apply_btn.clicked.connect(lambda: self.bake("apply"))
  175.         self.bake_btn.clicked.connect(lambda: self.bake("bake"))
  176.  
  177.     def update(self, mode):
  178.         if mode == "this":
  179.             self.start_frame_le.setText(str(int(mc.currentTime(q=True))))
  180.             self.end_frame_le.setText(str(int(mc.currentTime(q=True))))
  181.         if mode == "start":
  182.             self.start_frame_le.setText(str(int(mc.currentTime(q=True))))
  183.         if mode == "end":
  184.             self.end_frame_le.setText(str(int(mc.currentTime(q=True))))
  185.         if mode == "reset":
  186.             self.start_frame_le.setText(str(int(mc.playbackOptions(q=True, minTime=True))))
  187.             self.end_frame_le.setText(str(int(mc.playbackOptions(q=True, maxTime=True))))
  188.  
  189.     def bake(self, mode):
  190.         mc.undoInfo(openChunk=True)
  191.         try:
  192.             # Get List of selected Transform Nodes
  193.             selTransList = mc.ls(selection=True, transforms=True, long=True)
  194.  
  195.             locList = []
  196.             pcList = []
  197.             ocList = []
  198.             scList = []
  199.             for trans in selTransList:
  200.                 # Create Locator
  201.                 loc = mc.spaceLocator(name="baked_loc_#")[0]
  202.                 locList.append(loc)
  203.  
  204.                 # Set Rotate Order
  205.                 if self.rotateOrder_inherit_rb.isChecked(): mc.setAttr(loc+".rotateOrder", mc.getAttr(trans+".rotateOrder"))
  206.                 if self.rotateOrder_xyz_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 0)
  207.                 if self.rotateOrder_yzx_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 1)
  208.                 if self.rotateOrder_zxy_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 2)
  209.                 if self.rotateOrder_xzy_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 3)
  210.                 if self.rotateOrder_yxz_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 4)
  211.                 if self.rotateOrder_zyx_rb.isChecked(): mc.setAttr(loc+".rotateOrder", 5)
  212.  
  213.                 if self.point_groupbox.isChecked():
  214.                     pskip = ""
  215.                     if self.point_x_cb.isChecked() == False: pskip += ", skip='x'"
  216.                     if self.point_y_cb.isChecked() == False: pskip += ", skip='y'"
  217.                     if self.point_z_cb.isChecked() == False: pskip += ", skip='z'"
  218.                     exec("pc = mc.pointConstraint(trans, loc, maintainOffset=False {0})".format(pskip))
  219.                     pcList.append(pc)
  220.                 if self.orient_groupbox.isChecked():
  221.                     oskip = ""
  222.                     if self.orient_x_cb.isChecked() == False: skip += ", skip='x'"
  223.                     if self.orient_y_cb.isChecked() == False: skip += ", skip='y'"
  224.                     if self.orient_z_cb.isChecked() == False: skip += ", skip='z'"
  225.                     exec("oc = mc.orientConstraint(trans, loc, maintainOffset=False {0})".format(oskip))
  226.                     ocList.append(oc)
  227.                 if self.scale_groupbox.isChecked():
  228.                     sskip = ""
  229.                     if self.scale_x_cb.isChecked() == False: skip += ", skip='x'"
  230.                     if self.scale_y_cb.isChecked() == False: skip += ", skip='y'"
  231.                     if self.scale_z_cb.isChecked() == False: skip += ", skip='z'"
  232.                     exec("sc = mc.scaleConstraint(trans, loc, maintainOffset=False {0})".format(sskip))
  233.                     scList.append(sc)
  234.  
  235.             # Bake
  236.             if self.start_frame_le.text() != self.end_frame_le.text(): # If Start & End Frame is not same, Bake.
  237.                 for loc in locList:
  238.                     mc.bakeResults(loc, attribute=["tx","ty","tz","rx","ry","rz","sx","sy","sz"], time=(self.start_frame_le.text(), self.end_frame_le.text()))
  239.  
  240.             # Delete Constraints
  241.             for pc in pcList:
  242.                 try:
  243.                     mc.delete(pc)
  244.                 except:
  245.                     pass
  246.             for oc in ocList:
  247.                 try:
  248.                     mc.delete(oc)
  249.                 except:
  250.                     pass
  251.             for sc in scList:
  252.                 try:
  253.                     mc.delete(sc)
  254.                 except:
  255.                     pass
  256.  
  257.             mc.select(clear=True)
  258.  
  259.         except:
  260.             traceback.print_exc()
  261.             om.MGlobal.displayError("Error occurred. See script editor for details.")
  262.  
  263.         mc.undoInfo(closeChunk=True)
  264.  
  265.         ## Close Window ##
  266.         if mode == "bake":
  267.             self.close()
  268.             self.deleteLater()
  269.  
  270. if __name__ == "__main__":
  271.     try:
  272.         baker.close()
  273.         baker.deleteLater()
  274.     except:
  275.         pass
  276.  
  277.     baker = Baker()
  278.     baker.show()
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