Beispiel #1
0
    def setupUi(self):
        self.setObjectName("controlFactory")

        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored,
                                           QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)

        # layouts
        mainLayout = QtWidgets.QVBoxLayout(self)
        vLayout_addCurve = QtWidgets.QVBoxLayout()
        hLayout_addCurve = QtWidgets.QHBoxLayout()
        vLayout_copyCurve = QtWidgets.QVBoxLayout()
        hLayout_copyCurve = QtWidgets.QHBoxLayout()
        vLayout_removeCurve = QtWidgets.QVBoxLayout()
        hLayout_removeCurve = QtWidgets.QHBoxLayout()
        vLayout_color = QtWidgets.QVBoxLayout()
        hLayout_color = QtWidgets.QHBoxLayout()
        vLayout_scale = QtWidgets.QVBoxLayout()
        hLayout_scale = QtWidgets.QHBoxLayout()
        vLayout_tweak = QtWidgets.QVBoxLayout()
        hLayout_tweak = QtWidgets.QHBoxLayout()
        vLayout_addGroups = QtWidgets.QVBoxLayout()
        hLayout_addOffsetGroups = QtWidgets.QHBoxLayout()

        vLayout_addCurve.addLayout(hLayout_addCurve)
        vLayout_copyCurve.addLayout(hLayout_copyCurve)
        vLayout_removeCurve.addLayout(hLayout_removeCurve)
        vLayout_color.addLayout(hLayout_color)
        vLayout_scale.addLayout(hLayout_scale)
        vLayout_tweak.addLayout(hLayout_tweak)
        vLayout_addGroups.addLayout(hLayout_addOffsetGroups)

        # buttons
        pb_addCurve = QtWidgets.QPushButton()
        pb_copyCurve = QtWidgets.QPushButton()
        pb_copyShapes = QtWidgets.QPushButton("Copy ControlShapes")
        pb_removeCurve = QtWidgets.QPushButton()
        pb_removeShapes = QtWidgets.QPushButton("Remove ControlShapes")
        pb_color = QtWidgets.QPushButton()
        pb_scale = QtWidgets.QPushButton()
        pb_tweak = QtWidgets.QPushButton()
        pb_addOffsetGroups = QtWidgets.QPushButton()
        pb_addGroups = QtWidgets.QPushButton("Add zero grp")
        pb_N = QtWidgets.QPushButton("N")
        pb_R = QtWidgets.QPushButton("R")
        pb_T = QtWidgets.QPushButton("T")
        pb_default = QtWidgets.QPushButton("X")
        pb_plusX = QtWidgets.QPushButton("+")
        pb_minusX = QtWidgets.QPushButton("-")
        pb_plusY = QtWidgets.QPushButton("+")
        pb_minusY = QtWidgets.QPushButton("-")
        pb_plusZ = QtWidgets.QPushButton("+")
        pb_minusZ = QtWidgets.QPushButton("-")
        pb_scaleDown = QtWidgets.QPushButton("-")
        pb_scaleUp = QtWidgets.QPushButton("+")
        pb_N.setSizePolicy(sizePolicy)
        pb_R.setSizePolicy(sizePolicy)
        pb_T.setSizePolicy(sizePolicy)
        pb_default.setSizePolicy(sizePolicy)

        pb_addCurve.setMaximumSize(QtCore.QSize(25, 25))
        pb_copyCurve.setMaximumSize(QtCore.QSize(25, 25))
        pb_removeCurve.setMaximumSize(QtCore.QSize(25, 25))
        pb_color.setMaximumSize(QtCore.QSize(25, 25))
        pb_scale.setMaximumSize(QtCore.QSize(25, 25))
        pb_tweak.setMaximumSize(QtCore.QSize(25, 25))
        pb_addOffsetGroups.setMaximumSize(QtCore.QSize(25, 25))
        pb_plusX.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_minusX.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_plusY.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_minusY.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_minusZ.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_plusZ.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_scaleDown.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_scaleUp.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_addCurve.setMinimumSize(QtCore.QSize(25, 25))
        pb_copyCurve.setMinimumSize(QtCore.QSize(25, 25))
        pb_removeCurve.setMinimumSize(QtCore.QSize(25, 25))
        pb_color.setMinimumSize(QtCore.QSize(25, 25))
        pb_scale.setMinimumSize(QtCore.QSize(25, 25))
        pb_tweak.setMinimumSize(QtCore.QSize(25, 25))
        pb_addOffsetGroups.setMinimumSize(QtCore.QSize(25, 25))

        # labels
        label_addCurve = QtWidgets.QLabel("Add Control Curve")
        label_addCurve.setFrameShape(QtWidgets.QFrame.NoFrame)
        label_copyCurve = QtWidgets.QLabel("Copy Control Curve")
        label_removeCurve = QtWidgets.QLabel("Remove Control Curve(s)")
        label_color = QtWidgets.QLabel("Color")
        label_scale = QtWidgets.QLabel("Scale")
        label_minScale = QtWidgets.QLabel("Min")
        label_maxScale = QtWidgets.QLabel("Max")
        label_title = QtWidgets.QLabel("Value")
        label_sliderValue = QtWidgets.QLabel("")
        label_rotation = QtWidgets.QLabel("Rotation")
        label_rtX = QtWidgets.QLabel("Rotate X")
        label_rtY = QtWidgets.QLabel("Rotate Y")
        label_rtZ = QtWidgets.QLabel("Rotate Z")
        label_addGroups = QtWidgets.QLabel("Add offset groups")
        label_minScale.setAlignment(QtCore.Qt.AlignCenter)
        label_maxScale.setAlignment(QtCore.Qt.AlignCenter)

        # minimize widgets
        widget_addCurve = QtWidgets.QWidget()
        widget_copy = QtWidgets.QWidget()
        widget_remove = QtWidgets.QWidget()
        widget_color = QtWidgets.QWidget()
        widget_scale = QtWidgets.QWidget()
        widget_tweak = QtWidgets.QWidget()
        widget_addGroups = QtWidgets.QWidget()

        vLayout_addCurve.addWidget(widget_addCurve)
        vLayout_copyCurve.addWidget(widget_copy)
        vLayout_removeCurve.addWidget(widget_remove)
        vLayout_color.addWidget(widget_color)
        vLayout_scale.addWidget(widget_scale)
        vLayout_tweak.addWidget(widget_tweak)
        vLayout_addGroups.addWidget(widget_addGroups)

        # checkbox
        cb_shapeOnly = QtWidgets.QCheckBox("ControlShape Only")
        cb_viewport = QtWidgets.QCheckBox("Viewport")
        cb_outliner = QtWidgets.QCheckBox("Outliner")
        cb_nonUniform = QtWidgets.QCheckBox("Non-uniform")
        cb_x = QtWidgets.QCheckBox("x")
        cb_y = QtWidgets.QCheckBox("y")
        cb_z = QtWidgets.QCheckBox("z")
        cb_zero = QtWidgets.QCheckBox("zero")
        cb_sdk = QtWidgets.QCheckBox("sdk")
        cb_offset = QtWidgets.QCheckBox("offset")
        cb_parent = QtWidgets.QCheckBox("parent")

        # line edits
        le_x = QtWidgets.QLineEdit()
        le_y = QtWidgets.QLineEdit()
        le_z = QtWidgets.QLineEdit()
        le_minScale = QtWidgets.QLineEdit()
        le_maxScale = QtWidgets.QLineEdit()
        le_minScale.setMaximumSize(QtCore.QSize(30, 16777215))
        le_maxScale.setMaximumSize(QtCore.QSize(30, 16777215))

        # slider
        hSlider_scaleFactor = QtWidgets.QSlider()

        # spacers
        spacerStart = QtWidgets.QSpacerItem(40, 20,
                                            QtWidgets.QSizePolicy.Expanding,
                                            QtWidgets.QSizePolicy.Minimum)
        spacerEnd = QtWidgets.QSpacerItem(40, 20,
                                          QtWidgets.QSizePolicy.Expanding,
                                          QtWidgets.QSizePolicy.Minimum)

        # add buttons to title layouts
        hLayout_addCurve.addWidget(pb_addCurve)
        hLayout_copyCurve.addWidget(pb_copyCurve)
        hLayout_removeCurve.addWidget(pb_removeCurve)
        hLayout_color.addWidget(pb_color)
        hLayout_scale.addWidget(pb_scale)
        hLayout_tweak.addWidget(pb_tweak)
        hLayout_addOffsetGroups.addWidget(pb_addOffsetGroups)

        # add labels to title layouts
        hLayout_addCurve.addWidget(label_addCurve)
        hLayout_copyCurve.addWidget(label_copyCurve)
        hLayout_removeCurve.addWidget(label_removeCurve)
        hLayout_color.addWidget(label_color)
        hLayout_scale.addWidget(label_scale)
        hLayout_tweak.addWidget(label_rotation)
        hLayout_addOffsetGroups.addWidget(label_addGroups)

        # addCurve layout
        vLayout_inner_addCurve = QtWidgets.QVBoxLayout(widget_addCurve)
        gLayout_addCurve = QtWidgets.QGridLayout()
        vLayout_inner_addCurve.addLayout(gLayout_addCurve)
        vLayout_inner_addCurve.addWidget(cb_shapeOnly)

        # copyCurve layout
        vLayout_copy = QtWidgets.QVBoxLayout(widget_copy)
        vLayout_copy.addWidget(pb_copyShapes)

        # removeCurve layout
        vLayout_remove = QtWidgets.QVBoxLayout(widget_remove)
        vLayout_remove.addWidget(pb_removeShapes)

        # color layout
        vLayout_colorWidget = QtWidgets.QVBoxLayout(widget_color)
        gLayout_color = QtWidgets.QGridLayout()
        gLayout_color.addWidget(pb_N, 0, 0, 1, 1)
        gLayout_color.addWidget(pb_R, 0, 2, 1, 1)
        gLayout_color.addWidget(pb_T, 0, 1, 1, 1)
        gLayout_color.addWidget(pb_default, 0, 3, 1, 1)
        hLayout_colorType = QtWidgets.QHBoxLayout()
        hLayout_colorType.addWidget(cb_viewport)
        hLayout_colorType.addWidget(cb_outliner)
        vLayout_colorWidget.addLayout(hLayout_colorType)
        vLayout_colorWidget.addLayout(gLayout_color)

        # scale layout
        vLayout_scaleWidget = QtWidgets.QVBoxLayout(widget_scale)
        vLayout_nonUniform = QtWidgets.QVBoxLayout()
        vLayout_nonUniform.addWidget(cb_nonUniform)
        hLayout_nonUniform = QtWidgets.QHBoxLayout()
        hLayout_nonUniform.setContentsMargins(30, -1, -1, -1)
        hLayout_nonUniform.addWidget(cb_x)
        hLayout_nonUniform.addWidget(cb_y)
        hLayout_nonUniform.addWidget(cb_z)
        vLayout_nonUniform.addLayout(hLayout_nonUniform)
        hLayout_upDown = QtWidgets.QHBoxLayout()
        vLayout_scaleFactor = QtWidgets.QVBoxLayout()
        hLayout_scaleFactor = QtWidgets.QHBoxLayout()
        vLayout_min = QtWidgets.QVBoxLayout()
        vLayout_min.addWidget(label_minScale)
        vLayout_min.addWidget(le_minScale)
        hLayout_scaleFactor.addLayout(vLayout_min)
        spacerItem = QtWidgets.QSpacerItem(40, 20,
                                           QtWidgets.QSizePolicy.Expanding,
                                           QtWidgets.QSizePolicy.Minimum)
        hLayout_scaleFactor.addItem(spacerItem)
        vLayout_sliderValue = QtWidgets.QVBoxLayout()
        vLayout_sliderValue.addWidget(label_title)
        vLayout_sliderValue.addWidget(label_sliderValue)
        hLayout_scaleFactor.addLayout(vLayout_sliderValue)
        spacerItem = QtWidgets.QSpacerItem(40, 20,
                                           QtWidgets.QSizePolicy.Expanding,
                                           QtWidgets.QSizePolicy.Minimum)
        hLayout_scaleFactor.addItem(spacerItem)
        vLayout_max = QtWidgets.QVBoxLayout()
        vLayout_max.addWidget(label_maxScale)
        vLayout_max.addWidget(le_maxScale)
        hLayout_scaleFactor.addLayout(vLayout_max)
        vLayout_scaleFactor.addLayout(hLayout_scaleFactor)
        hSlider_scaleFactor.setMinimum(1)
        hSlider_scaleFactor.setOrientation(QtCore.Qt.Horizontal)
        hSlider_scaleFactor.setTickPosition(QtWidgets.QSlider.NoTicks)
        vLayout_scaleFactor.addWidget(hSlider_scaleFactor)
        hLayout_upDown.addLayout(vLayout_scaleFactor)
        vLayout_btn = QtWidgets.QVBoxLayout()
        spacer = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum,
                                       QtWidgets.QSizePolicy.Preferred)
        vLayout_btn.addItem(spacer)
        hLayout_btn = QtWidgets.QHBoxLayout()
        hLayout_btn.addWidget(pb_scaleDown)
        hLayout_btn.addWidget(pb_scaleUp)
        vLayout_btn.addLayout(hLayout_btn)
        hLayout_upDown.addLayout(vLayout_btn)
        vLayout_scaleWidget.addLayout(vLayout_nonUniform)
        vLayout_scaleWidget.addLayout(hLayout_upDown)

        # tweak layout
        gLayout_tweak = QtWidgets.QGridLayout(widget_tweak)
        gLayout_tweak.addItem(spacerStart, 0, 0, 1, 1)
        gLayout_tweak.addWidget(pb_plusX, 0, 4, 1, 1)
        gLayout_tweak.addWidget(le_x, 0, 2, 1, 1)
        gLayout_tweak.addWidget(pb_minusX, 0, 3, 1, 1)
        gLayout_tweak.addWidget(label_rtX, 0, 1, 1, 1)
        gLayout_tweak.addItem(spacerEnd, 0, 5, 1, 1)
        gLayout_tweak.addWidget(label_rtY, 1, 1, 1, 1)
        gLayout_tweak.addWidget(label_rtZ, 2, 1, 1, 1)
        gLayout_tweak.addWidget(le_y, 1, 2, 1, 1)
        gLayout_tweak.addWidget(le_z, 2, 2, 1, 1)
        gLayout_tweak.addWidget(pb_minusY, 1, 3, 1, 1)
        gLayout_tweak.addWidget(pb_minusZ, 2, 3, 1, 1)
        gLayout_tweak.addWidget(pb_plusY, 1, 4, 1, 1)
        gLayout_tweak.addWidget(pb_plusZ, 2, 4, 1, 1)

        # add groups layout
        vLayout_addOffsetGroups = QtWidgets.QVBoxLayout(widget_addGroups)
        hLayout_addGroups = QtWidgets.QHBoxLayout()
        hLayout_addGroups.addWidget(cb_zero)
        hLayout_addGroups.addWidget(cb_sdk)
        hLayout_addGroups.addWidget(cb_offset)
        hLayout_addGroups.addWidget(cb_parent)
        vLayout_addOffsetGroups.addLayout(hLayout_addGroups)
        vLayout_addOffsetGroups.addWidget(pb_addGroups)

        line_1 = QtWidgets.QFrame()
        line_1.setFrameShape(QtWidgets.QFrame.HLine)
        line_1.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_2 = QtWidgets.QFrame()
        line_2.setFrameShape(QtWidgets.QFrame.HLine)
        line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_3 = QtWidgets.QFrame()
        line_3.setFrameShape(QtWidgets.QFrame.HLine)
        line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_4 = QtWidgets.QFrame()
        line_4.setFrameShape(QtWidgets.QFrame.HLine)
        line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_5 = QtWidgets.QFrame()
        line_5.setFrameShape(QtWidgets.QFrame.HLine)
        line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_6 = QtWidgets.QFrame()
        line_6.setFrameShape(QtWidgets.QFrame.HLine)
        line_6.setFrameShadow(QtWidgets.QFrame.Sunken)

        mainLayout.addLayout(vLayout_addCurve)
        mainLayout.addWidget(line_1)
        mainLayout.addLayout(vLayout_copyCurve)
        mainLayout.addWidget(line_2)
        mainLayout.addLayout(vLayout_removeCurve)
        mainLayout.addWidget(line_3)
        mainLayout.addLayout(vLayout_color)
        mainLayout.addWidget(line_4)
        mainLayout.addLayout(vLayout_scale)
        mainLayout.addWidget(line_5)
        mainLayout.addLayout(vLayout_tweak)
        mainLayout.addWidget(line_6)
        mainLayout.addLayout(vLayout_addGroups)

        self.data = {
            'buttons': {
                'addCurve': pb_addCurve,
                'copyCurve': pb_copyCurve,
                'copyShapes': pb_copyShapes,
                'removeCurve': pb_removeCurve,
                'removeShapes': pb_removeShapes,
                'color': pb_color,
                'scale': pb_scale,
                'tweak': pb_tweak,
                'addOffsetGroups': pb_addOffsetGroups,
                'addGroups': pb_addGroups,
                'N': pb_N,
                'R': pb_R,
                'T': pb_T,
                'X': pb_default,
                'plusX': pb_plusX,
                'minusX': pb_minusX,
                'plusY': pb_plusY,
                'minusY': pb_minusY,
                'plusZ': pb_plusZ,
                'minusZ': pb_minusZ,
                'scaleDown': pb_scaleDown,
                'scaleUp': pb_scaleUp
            },
            'layouts': {
                'addCurve': gLayout_addCurve,
                'color': gLayout_color
            },
            'widgets': {
                'addCurve': widget_addCurve,
                'copy': widget_copy,
                'remove': widget_remove,
                'color': widget_color,
                'scale': widget_scale,
                'tweak': widget_tweak,
                'addGroups': widget_addGroups
            },
            'checkbox': {
                'shapeOnly': cb_shapeOnly,
                'viewport': cb_viewport,
                'outliner': cb_outliner,
                'nonUniform': cb_nonUniform,
                'x': cb_x,
                'y': cb_y,
                'z': cb_z,
                'zero': cb_zero,
                'sdk': cb_sdk,
                'offset': cb_offset,
                'parent': cb_parent
            },
            'lineedit': {
                'x': le_x,
                'y': le_y,
                'z': le_z,
                'minScale': le_minScale,
                'maxScale': le_maxScale
            },
            'slider': {
                'scale': hSlider_scaleFactor
            },
            'label': {
                'slider': label_sliderValue
            }
        }
Beispiel #2
0
    def initialize(self):
        # add validators
        self.data['lineedit']['x'].setValidator(
            QtGui.QDoubleValidator(0, 180, 2))
        self.data['lineedit']['y'].setValidator(
            QtGui.QDoubleValidator(0, 180, 2))
        self.data['lineedit']['z'].setValidator(
            QtGui.QDoubleValidator(0, 180, 2))

        self.data['lineedit']['minScale'].setValidator(
            QtGui.QDoubleValidator(1.0, 100, 2))
        self.data['lineedit']['maxScale'].setValidator(
            QtGui.QDoubleValidator(1.0, 100, 2))

        # add buttons - add control curve
        dirs = os.listdir(ctrls.CONTROLS_DIRECTORY)
        col = 6
        row = 0
        count = 0
        self.mapperAddCurve = QtCore.QSignalMapper()
        for fileName in dirs:
            if fileName.endswith('.json'):
                if count == 6:
                    count = 0
                    row += 1
                btn = QtWidgets.QPushButton()
                btn.setToolTip(fileName)
                iconName = fileName.replace('.json', '.png')
                if iconName in dirs:
                    iconPath = os.path.join(ctrls.CONTROLS_DIRECTORY, iconName)
                    btn.setIcon(QtGui.QIcon(iconPath))
                    btn.setIconSize(QtCore.QSize(30, 30))
                self.data['layouts']['addCurve'].addWidget(btn, row, count)
                count += 1
                self.mapperAddCurve.setMapping(btn, fileName)
                btn.clicked.connect(self.mapperAddCurve.map)
        self.mapperAddCurve.mapped['QString'].connect(self.addCtrlShape)

        # add buttons - color
        self.mapperColor = QtCore.QSignalMapper()
        col = 8
        row = 0
        count = 4
        for i in xrange(1, 32):
            if count == col:
                count = 0
                row += 1
            rgb = cmds.colorIndex(i, q=True)
            r, g, b = rgb[0], rgb[1], rgb[2]
            btn = QtWidgets.QPushButton()
            btn.btnIndex = count
            btn.setAutoFillBackground(True)
            values = "{r}, {g}, {b}, {a}".format(r=r * 255,
                                                 g=g * 255,
                                                 b=b * 255,
                                                 a=255)
            btn.setStyleSheet("QPushButton { background-color: rgba(" +
                              values + "); }")
            self.data['layouts']['color'].addWidget(btn, row, count)
            count += 1
            self.mapperColor.setMapping(btn, i)
            btn.clicked.connect(self.mapperColor.map)
        self.mapperColor.mapped['int'].connect(self.colorShapes)
Beispiel #3
0
def exportSkinWeight(exportPath, meshes, namespace=False):
    """
    Export skinWeights of a list of meshes to a json file.

    Args:
        exportPath: The file path where a json file is saved.
        meshes: A list of mesh nodes.
        namespace: True to export with namespace. False to export without namespace.

    Returns: True if export succeeds. False if export fails.
    """
    data = {}
    if not meshes:
        LOG.error('Meshes input {0} is not valid.'.format(meshes))
        return False

    for mesh in meshes:
        skinCluster = getSkinCluster(mesh)
        if not skinCluster:
            LOG.warning('Mesh {} has no skinCluster, skipping... '.format(mesh))
            continue
        skinNorm = cmds.getAttr('%s.normalizeWeights' % skinCluster)

        # get the MFnSkinCluster for clusterName
        selList = om.MSelectionList()
        selList.add(skinCluster)
        clusterNode = om.MObject()
        selList.getDependNode(0, clusterNode)
        skinFn = oma.MFnSkinCluster(clusterNode)

        # get the MDagPath for all influence
        infDags = om.MDagPathArray()
        skinFn.influenceObjects(infDags)

        # {
        #     "mesh_name": {
        #         "weights": {
        #             "vert id": {
        #                 "influence id": weight,
        #                 "influence id": weight
        #             }
        #         },
        #         "infs": [inf1, inf2, inf3, inf4, ...],
        #         "skinCluster": skinCluster_name
        #     }
        # }

        infIds = {}
        infs = []
        unique = True
        for i in xrange(infDags.length()):
            infPath = infDags[i].partialPathName()
            if '|' in infPath:
                LOG.warning('Influence of {}: "{}" is not have a unique name.'.format(mesh, infDags[i].fullPathName()))
                unique = False
            infId = int(skinFn.indexForInfluenceObject(infDags[i]))
            infIds[infId] = i
            infs.append(infPath)
        if not unique:
            LOG.warning('{} skincluster export is skipped. Please make sure all influence names are unique'.format(mesh))
            continue

        # get the MPlug for the weightList and weights attributes
        wlPlug = skinFn.findPlug('weightList')
        wPlug = skinFn.findPlug('weights')
        wlAttr = wlPlug.attribute()
        wAttr = wPlug.attribute()
        wInfIds = om.MIntArray()

        # progressBar visualization
        total = wlPlug.numElements()
        progressBar = QtWidgets.QProgressBar()
        progressBar.setMinimumSize(QtCore.QSize(450, 40))
        progressBar.setMinimum(1)
        progressBar.setMaximum(total)
        progressBar.setWindowTitle('Exporting skincluster: {}'.format(mesh))
        progressBar.show()
        completed = 0

        weights = {}
        for vId in xrange(wlPlug.numElements()):
            vWeights = {}
            # tell the weights attribute which vertex id it represents
            wPlug.selectAncestorLogicalIndex(vId, wlAttr)

            # get the indice of all non-zero weights for this vert
            wPlug.getExistingArrayAttributeIndices(wInfIds)

            # create a copy of the current wPlug
            infPlug = om.MPlug(wPlug)

            completed += 1
            progressBar.setValue(completed)
            for infId in wInfIds:
                # tell the infPlug it represents the current influence id
                infPlug.selectAncestorLogicalIndex(infId, wAttr)

                # add this influence and its weight to this verts weights
                try:
                    vWeights[infIds[infId]] = infPlug.asDouble()
                except KeyError:
                    # assumes a removed influence
                    pass
            weights[vId] = vWeights

        if namespace:
            meshName = mesh
        else:
            meshName = mesh.split(':')[-1]

        data[meshName] = {'weights': weights, 'infs': infs, 'skinCluster': skinCluster, 'nw': skinNorm}

    with open(exportPath, 'w') as outfile:
        try:
            if data:
                json.dump(data, outfile, sort_keys=True, indent=4)
                LOG.info('Exported skin weights for mesh {0}.'.format(meshes))
                return True
            else:
                LOG.info('No valid skinCluster is found for mesh {0}.'.format(meshes))
                return False
        except:
            LOG.error('Unable to export skinWeight data to {0}.'.format(exportPath))
            return False
Beispiel #4
0
def importSkinWeight(importPath, meshes, namespace=False):
    """
    Import skinWeight from a json file, to a list of meshes.
    To a whole mesh, or selected vertices.

    Args:
        filepath: The file path where a json file is loaded.
        meshes: A list of mesh nodes. If [], import all available meshes.
        namespace: True to respect imported namespace data. False to ignore any namespaces.

    Returns: True if import succeeds. False if import fails.
    """

    with open(importPath) as infile:
        try:
            data = json.load(infile)
            LOG.info('Loaded skinWeight data from {0}.'.format(importPath))
        except:
            LOG.error('Unable to load skinWeight data from {0}.'.format(importPath))
            return False

    if not meshes:
        meshes = data.keys()

    for mesh in meshes:
        selectedIndice = []
        selectedVerts = [v.split('.vtx')[-1] for v in cmds.ls(sl=1, fl=1) if (('.vtx' in v) and (mesh in v))]
        for v in selectedVerts:
            id = re.findall('\\d+', v)[0]
            selectedIndice.append(id)

        if namespace:
            if not (mesh in data):
                LOG.warning('Unable to find mesh data for "{0}"'.format(mesh))
                continue
            meshName = mesh
        else:
            shortName = mesh.split(':')[-1]
            meshName = ''
            for m in data.keys():
                if m.split(':')[-1] == shortName:
                    meshName = m
            if not meshName:
                LOG.warning('Unable to find mesh data for "{0}"'.format(shortName))
                continue

        # if mesh is not in current scene, skip
        if not cmds.objExists(meshName):
            continue

        weights = data[meshName]['weights']
        infs = data[meshName]['infs']
        skinClusterName = data[meshName]['skinCluster']
        skinNorm = data[meshName]['nw']

        if cmds.polyEvaluate(mesh, v=1) != len(weights.keys()):
            LOG.warning('Mesh "{0}": Vertex number does not match with the imported skinCluster "{1}"'.format(
                mesh, skinClusterName))

        # progressBar visualization
        total = len(weights.items())
        progressBar = QtWidgets.QProgressBar()
        progressBar.setMinimumSize(QtCore.QSize(450, 40))
        progressBar.setMinimum(1)
        progressBar.setMaximum(total)
        progressBar.setWindowTitle('Importing skincluster: {}'.format(mesh))
        progressBar.show()
        completed = 0

        # vertices selection
        if selectedIndice:
            # get skinCluster
            currentName = getSkinCluster(mesh)
            # check if skinCluster exists
            if not currentName:
                LOG.error('Mesh "{0}": SkinCluster missing selected vertices'.format(mesh))
                return False
            # check the name of skinCluster
            elif currentName != skinClusterName:
                LOG.warning('SkinCluster "{0}": Name does not match with the imported skinCluster "{1}"'.format(currentName, skinClusterName))

            # check the number of influences
            currentInfs = cmds.skinCluster(currentName, q=1, inf=1)
            if len(currentInfs) != len(infs):
                LOG.warning('SkinCluster "{0}": Influence number does not match with the imported skinCluster "{1}"'.format(currentName, skinClusterName))

            # unlock influences used by skincluster
            for inf in infs:
                if not cmds.objExists(inf):
                    LOG.warning('Unable to find influence "{0}"]'.format(inf))
                    continue
                cmds.setAttr('%s.liw' % inf, 0)

            for vertId in selectedIndice:
                if not (vertId in weights):
                    LOG.info('Unable to find weight data for {0}.vtx[{1}]'.format(mesh, vertId))
                    continue
                    # TODO
                    # deal with vertices with missing weight data: missing data for vertId
                    # currently acting awkward
                weightData = weights[vertId]
                wlAttr = '%s.weightList[%s]' % (currentName, vertId)
                completed += 1
                progressBar.setValue(completed)
                for infId, infValue in weightData.items():
                    wAttr = '.weights[%s]' % infId
                    cmds.setAttr(wlAttr + wAttr, infValue)
            return True

        # get skinCluster
        if getSkinCluster(mesh):
            cmds.delete(getSkinCluster(mesh))
        create(mesh, infs, name=skinClusterName, nw=skinNorm)

        # normalize needs turned off for the prune to work
        if skinNorm != 0:
            cmds.setAttr('%s.normalizeWeights' % skinClusterName, 0)
        cmds.skinPercent(skinClusterName, mesh, nrm=False, prw=100)

        # restore normalize setting
        cmds.setAttr('%s.normalizeWeights' % skinClusterName, skinNorm)

        # apply weights
        for vertId, weightData in weights.items():
            wlAttr = '%s.weightList[%s]' % (skinClusterName, vertId)
            completed += 1
            progressBar.setValue(completed)
            for infId, infValue in weightData.items():
                wAttr = '.weights[%s]' % infId
                cmds.setAttr(wlAttr + wAttr, infValue)

    return True
Beispiel #5
0
    def setupUi(self):
        self.setObjectName("jointFactory")

        mainLayout= QtWidgets.QVBoxLayout(self)

        # buttons
        pb_toggle = QtWidgets.QPushButton("Toggle Local Axes Visibility")
        pb_orientJoints = QtWidgets.QPushButton("Orient Joints")
        pb_planarOrientJoints = QtWidgets.QPushButton("Planar Orient Joints for 3")
        pb_minusX = QtWidgets.QPushButton("-")
        pb_plusX = QtWidgets.QPushButton("+")
        pb_minusY = QtWidgets.QPushButton("-")
        pb_plusY = QtWidgets.QPushButton("+")
        pb_minusZ = QtWidgets.QPushButton("-")
        pb_plusZ = QtWidgets.QPushButton("+")
        pb_insertJoints = QtWidgets.QPushButton("Apply")
        pb_copyJointOrient = QtWidgets.QPushButton("Copy Joint Orientation")
        pb_turnOffLRA = QtWidgets.QPushButton("Turn Off All LRA")

        pb_minusX.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_plusX.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_minusY.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_plusY.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_minusZ.setMaximumSize(QtCore.QSize(30, 16777215))
        pb_plusZ.setMaximumSize(QtCore.QSize(30, 16777215))

        # radio buttons
        rb_worldUpAxisX = QtWidgets.QRadioButton("x")
        rb_worldUpAxisY = QtWidgets.QRadioButton("y")
        rb_worldUpAxisZ = QtWidgets.QRadioButton("z")
        rb_aimAxisX = QtWidgets.QRadioButton("x")
        rb_aimAxisY = QtWidgets.QRadioButton("y")
        rb_aimAxisZ = QtWidgets.QRadioButton("z")
        rb_upAxisX = QtWidgets.QRadioButton("x")
        rb_upAxisY = QtWidgets.QRadioButton("y")
        rb_upAxisZ = QtWidgets.QRadioButton("z")

        rbg_worldUpAxis = QtWidgets.QButtonGroup(self)
        rbg_worldUpAxis.addButton(rb_worldUpAxisX)
        rbg_worldUpAxis.addButton(rb_worldUpAxisY)
        rbg_worldUpAxis.addButton(rb_worldUpAxisZ)
        rbg_aimAxis = QtWidgets.QButtonGroup(self)
        rbg_aimAxis.addButton(rb_aimAxisX)
        rbg_aimAxis.addButton(rb_aimAxisY)
        rbg_aimAxis.addButton(rb_aimAxisZ)
        rbg_upAxis = QtWidgets.QButtonGroup(self)
        rbg_upAxis.addButton(rb_upAxisX)
        rbg_upAxis.addButton(rb_upAxisY)
        rbg_upAxis.addButton(rb_upAxisZ)

        hLayout_worldUpAxis = QtWidgets.QHBoxLayout()
        hLayout_worldUpAxis.addWidget(rb_worldUpAxisX)
        hLayout_worldUpAxis.addWidget(rb_worldUpAxisY)
        hLayout_worldUpAxis.addWidget(rb_worldUpAxisZ)
        hLayout_aimAxis = QtWidgets.QHBoxLayout()
        hLayout_aimAxis.addWidget(rb_aimAxisX)
        hLayout_aimAxis.addWidget(rb_aimAxisY)
        hLayout_aimAxis.addWidget(rb_aimAxisZ)
        hLayout_upAxis = QtWidgets.QHBoxLayout()
        hLayout_upAxis.addWidget(rb_upAxisX)
        hLayout_upAxis.addWidget(rb_upAxisY)
        hLayout_upAxis.addWidget(rb_upAxisZ)

        # checkbox
        cb_orientChildren = QtWidgets.QCheckBox("Orient children")
        cb_orient2World = QtWidgets.QCheckBox("Orient Joint to World")

        cb_orientChildren.setLayoutDirection(QtCore.Qt.LeftToRight)
        cb_orient2World.setLayoutDirection(QtCore.Qt.LeftToRight)

        # combo box
        combo_upAxis = QtWidgets.QComboBox()
        combo_upAxis.addItem("+")
        combo_upAxis.addItem("-")
        combo_aimAxis = QtWidgets.QComboBox()
        combo_aimAxis.addItem("+")
        combo_aimAxis.addItem("-")
        combo_worldUpAxis = QtWidgets.QComboBox()
        combo_worldUpAxis.addItem("+")
        combo_worldUpAxis.addItem("-")

        # labels
        label_upAxis = QtWidgets.QLabel("Up Axis")
        label_worldUpAxis = QtWidgets.QLabel("World Up Axis:")
        label_aimAxis = QtWidgets.QLabel("Aim Axis:")
        label_x = QtWidgets.QLabel("x")
        label_y = QtWidgets.QLabel("y")
        label_z = QtWidgets.QLabel("z")
        label_insertJoints = QtWidgets.QLabel("Insert Joints:")
        label_upAxis.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter)
        label_worldUpAxis.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter)
        label_aimAxis.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter)

        # line edits
        le_x = QtWidgets.QLineEdit()
        le_y = QtWidgets.QLineEdit()
        le_z = QtWidgets.QLineEdit()
        le_insertJoints = QtWidgets.QLineEdit()

        spacerStart = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        spacerEnd = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        gLayout_orient = QtWidgets.QGridLayout()
        gLayout_orient.setContentsMargins(-1, -1, 0, -1)
        gLayout_orient.addLayout(hLayout_worldUpAxis, 4, 2, 1, 1)
        gLayout_orient.addLayout(hLayout_aimAxis, 2, 2, 1, 1)
        gLayout_orient.addWidget(cb_orientChildren, 0, 2, 1, 1)
        gLayout_orient.addWidget(combo_upAxis, 3, 3, 1, 1)
        gLayout_orient.addItem(spacerStart, 2, 0, 1, 1)
        gLayout_orient.addItem(spacerEnd, 2, 4, 1, 1)
        gLayout_orient.addWidget(cb_orient2World, 1, 2, 1, 1)
        gLayout_orient.addWidget(label_upAxis, 3, 1, 1, 1)
        gLayout_orient.addWidget(combo_worldUpAxis, 4, 3, 1, 1)
        gLayout_orient.addWidget(combo_aimAxis, 2, 3, 1, 1)
        gLayout_orient.addWidget(label_worldUpAxis, 4, 1, 1, 1)
        gLayout_orient.addWidget(label_aimAxis, 2, 1, 1, 1)
        gLayout_orient.addLayout(hLayout_upAxis, 3, 2, 1, 1)

        spacerEnd = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        spacerStart = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        gLayout_tweak = QtWidgets.QGridLayout()
        gLayout_tweak.addWidget(label_x, 0, 1, 1, 1)
        gLayout_tweak.addWidget(le_z, 2, 2, 1, 1)
        gLayout_tweak.addWidget(le_y, 1, 2, 1, 1)
        gLayout_tweak.addWidget(label_y, 1, 1, 1, 1)
        gLayout_tweak.addWidget(le_x, 0, 2, 1, 1)
        gLayout_tweak.addWidget(label_z, 2, 1, 1, 1)
        gLayout_tweak.addItem(spacerEnd, 0, 6, 1, 1)
        gLayout_tweak.addItem(spacerStart, 0, 0, 1, 1)
        gLayout_tweak.addWidget(pb_minusX, 0, 3, 1, 1)
        gLayout_tweak.addWidget(pb_plusX, 0, 4, 1, 1)
        gLayout_tweak.addWidget(pb_minusY, 1, 3, 1, 1)
        gLayout_tweak.addWidget(pb_plusY, 1, 4, 1, 1)
        gLayout_tweak.addWidget(pb_minusZ, 2, 3, 1, 1)
        gLayout_tweak.addWidget(pb_plusZ, 2, 4, 1, 1)

        line_1 = QtWidgets.QFrame()
        line_1.setFrameShape(QtWidgets.QFrame.HLine)
        line_1.setFrameShadow(QtWidgets.QFrame.Sunken)
        line_2 = QtWidgets.QFrame()
        line_2.setFrameShape(QtWidgets.QFrame.HLine)
        line_2.setFrameShadow(QtWidgets.QFrame.Sunken)

        spacerStart = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        spacerEnd = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        hLayout_insert = QtWidgets.QHBoxLayout()
        hLayout_insert.setContentsMargins(0, -1, 0, -1)
        hLayout_insert.addItem(spacerStart)
        hLayout_insert.addWidget(label_insertJoints)
        hLayout_insert.addWidget(le_insertJoints)
        hLayout_insert.addWidget(pb_insertJoints)
        hLayout_insert.addItem(spacerEnd)

        mainLayout.addWidget(pb_toggle)
        mainLayout.addLayout(gLayout_orient)
        mainLayout.addWidget(pb_orientJoints)
        mainLayout.addWidget(pb_planarOrientJoints)
        mainLayout.addWidget(line_1)
        mainLayout.addLayout(gLayout_tweak)
        mainLayout.addWidget(line_2)
        mainLayout.addLayout(hLayout_insert)
        mainLayout.addWidget(pb_copyJointOrient)
        mainLayout.addWidget(pb_turnOffLRA)
        mainLayout.setStretch(0, 1)

        self.data = {
            'buttons': {
                'toggle': pb_toggle,
                'orientJoints': pb_orientJoints,
                'planarOrientJoints': pb_planarOrientJoints,
                'minusX': pb_minusX,
                'plusX': pb_plusX,
                'minusY': pb_minusY,
                'plusY': pb_plusY,
                'minusZ': pb_minusZ,
                'plusZ': pb_plusZ,
                'insertJoints': pb_insertJoints,
                'copyJointOrient': pb_copyJointOrient,
                'turnOffLRA': pb_turnOffLRA
            },
            'radio': {
                'worldUpAxisX': rb_worldUpAxisX,
                'worldUpAxisY': rb_worldUpAxisY,
                'worldUpAxisZ': rb_worldUpAxisZ,
                'aimAxisX': rb_aimAxisX,
                'aimAxisY': rb_aimAxisY,
                'aimAxisZ': rb_aimAxisZ,
                'upAxisX': rb_upAxisX,
                'upAxisY': rb_upAxisY,
                'upAxisZ': rb_upAxisZ
            },
            'rbg': {
                'worldUpAxis': rbg_worldUpAxis,
                'aimAxis': rbg_aimAxis,
                'upAxis': rbg_upAxis
            },
            'checkbox': {
                'orientChildren': cb_orientChildren,
                'orient2World': cb_orient2World
            },
            'combo': {
                'upAxis': combo_upAxis,
                'aimAxis': combo_aimAxis,
                'worldUpAxis': combo_worldUpAxis
            },
            'lineedit': {
                'x': le_x,
                'y': le_y,
                'z': le_z,
                'insertJoints': le_insertJoints
            }
        }