def get_invalid(cls, instance):
        """Returns the invalid transforms in the instance.

        This is the same as checking:
        - translate == [0, 0, 0] and rotate == [0, 0, 0] and
          scale == [1, 1, 1] and shear == [0, 0, 0]

        Returns:
            list: Transforms that are not identity matrix

        """
        from maya import cmds
        from reveries import lib

        invalid = list()

        _identity = [
            1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
            0.0, 0.0, 1.0
        ]
        _tolerance = 1e-30

        for transform in cmds.ls(instance, type="transform", long=True):

            matrix = cmds.xform(transform,
                                query=True,
                                matrix=True,
                                objectSpace=True)

            if not lib.matrix_equals(_identity, matrix, _tolerance):
                invalid.append(transform)

        return invalid
    def _collect_components_matrix(self, data, container):

        id_path = container_to_id_path(container)

        data["subMatrix"][id_path] = dict()
        data["hidden"][id_path] = list()

        members = cmds.sets(container["objectName"], query=True)
        transforms = cmds.ls(members, type="transform", referencedNodes=True)

        for transform in transforms:
            matrix = cmds.xform(transform,
                                query=True,
                                matrix=True,
                                objectSpace=True)

            if matrix_equals(matrix, DEFAULT_MATRIX):
                matrix = "<default>"

            address = utils.get_id(transform)
            data["subMatrix"][id_path][address] = matrix

            # Collect visbility with matrix
            visibility = cmds.getAttr(transform + ".visibility")
            if not visibility:
                # Only record hidden nodes
                data["hidden"][id_path].append(address)

        # Collect subseet group node's matrix
        subset_group = container["subsetGroup"]

        matrix = cmds.xform(subset_group,
                            query=True,
                            matrix=True,
                            objectSpace=True)

        if matrix_equals(matrix, DEFAULT_MATRIX):
            return

        name = subset_group.rsplit(":", 1)[-1]
        data["subMatrix"][id_path]["GROUP"] = {name: matrix}
Example #3
0
    def update_variation(self, data_new, data_old, container, force=False):
        """
        """
        import maya.cmds as cmds
        from reveries.lib import matrix_equals
        from reveries.maya.lib import TRANSFORM_ATTRS

        assembly = container["subsetGroup"]

        current_matrix = cmds.xform(assembly,
                                    query=True,
                                    matrix=True,
                                    objectSpace=True)
        origin_matrix = data_old["matrix"]
        has_matrix_override = not matrix_equals(current_matrix,
                                                origin_matrix)

        if has_matrix_override and not force:
            self.log.warning("Matrix override preserved on %s",
                             assembly)
        elif self.has_input_connections(assembly, TRANSFORM_ATTRS):
            self.log.warning("Input connection preserved on %s",
                             assembly)
        else:
            new_matrix = data_new["matrix"]
            with self.keep_scale_pivot(assembly):
                cmds.xform(assembly, objectSpace=True, matrix=new_matrix)

        container_id_map = self.containers_by_id(
            # Look up container ids in one batch
            set(data_old["subMatrix"]).union(data_new["subMatrix"])
        )

        # Update matrix to components
        old_data_map = {t: (m, h, i) for t, m, h, i in
                        self.parse_sub_matrix(data_old, container_id_map)}

        for parsed in self.parse_sub_matrix(data_new, container_id_map):
            transform, sub_matrix, is_hidden, inherits = parsed

            if not transform:
                continue

            origin = old_data_map.get(transform, (None, False, None))
            origin_sub_matrix, origin_hidden, origin_inherits = origin

            _tag = transform
            if _tag == "<alembic>":
                abc = transform = is_hidden
                alembic = sub_matrix

                current_sub_matrix = [
                    cmds.getAttr(abc + ".speed"),
                    cmds.getAttr(abc + ".offset"),
                    cmds.getAttr(abc + ".cycleType"),
                ]
                current_hidden = None
                current_inherits = None
                attributes = ["speed", "offset", "cycleType"]

            else:
                current_sub_matrix = cmds.xform(transform,
                                                query=True,
                                                matrix=True,
                                                objectSpace=True)
                current_hidden = not cmds.getAttr(transform + ".visibility")
                current_inherits = cmds.getAttr(transform + ".it")
                attributes = TRANSFORM_ATTRS

            # Updating matrix
            if origin_sub_matrix:
                has_matrix_override = not matrix_equals(current_sub_matrix,
                                                        origin_sub_matrix)
            else:
                has_matrix_override = False

            if has_matrix_override and not force:
                self.log.warning("Sub-Matrix override preserved on %s",
                                 transform)
            elif self.has_input_connections(transform, attributes):
                self.log.warning("Input connection preserved on %s",
                                 transform)
            elif _tag == "<alembic>":
                self.set_attr(abc + ".speed", alembic[0])
                self.set_attr(abc + ".offset", alembic[1])
                self.set_attr(abc + ".cycleType", alembic[2])
            else:
                with self.keep_scale_pivot(transform):
                    cmds.xform(transform, objectSpace=True, matrix=sub_matrix)

            if _tag == "<alembic>":
                continue

            # Updating inheritsTransform
            if origin_inherits is not None:
                has_inherits_override = current_inherits != origin_inherits
            else:
                has_inherits_override = False

            if has_inherits_override and not force:
                self.log.warning("InheritsTransform override preserved on %s",
                                 transform)
            elif inherits is not None:
                if force:
                    if current_inherits and not inherits:
                        self.set_attr(transform + ".it", True)
                    elif not current_inherits and inherits:
                        self.set_attr(transform + ".it", False)
                else:
                    if origin_inherits and not inherits:
                        self.set_attr(transform + ".it", True)
                    elif not origin_inherits and inherits:
                        self.set_attr(transform + ".it", False)

            # Updating visibility
            if self.has_input_connections(transform, ["visibility"]):
                continue

            if origin_hidden:
                has_hidden_override = current_hidden != origin_hidden
            else:
                has_hidden_override = False

            if has_hidden_override and not force:
                self.log.warning("Visibility override preserved on %s",
                                 transform)
            elif force:
                if current_hidden and not is_hidden:
                    self.set_attr(transform + ".visibility", True)
                elif not current_hidden and is_hidden:
                    self.set_attr(transform + ".visibility", False)
            else:
                if origin_hidden and not is_hidden:
                    self.set_attr(transform + ".visibility", True)
                elif not origin_hidden and is_hidden:
                    self.set_attr(transform + ".visibility", False)
    def update_variation(self, data_new, data_old, container, force=False):
        """
        """
        import maya.cmds as cmds
        from reveries.lib import matrix_equals

        assembly = container["subsetGroup"]

        current_matrix = cmds.xform(assembly,
                                    query=True,
                                    matrix=True,
                                    objectSpace=True)
        origin_matrix = data_old["matrix"]
        has_matrix_override = not matrix_equals(current_matrix, origin_matrix)

        if has_matrix_override and not force:
            self.log.warning("Matrix override preserved on %s",
                             data_new["namespace"])
        else:
            new_matrix = data_new["matrix"]
            with self.keep_scale_pivot(assembly):
                cmds.xform(assembly, objectSpace=True, matrix=new_matrix)

        # Update matrix to components
        old_data_map = {
            t: (m, h)
            for t, m, h in self.parse_sub_matrix(data_old)
        }

        for parsed in self.parse_sub_matrix(data_new):
            transform, sub_matrix, is_hidden = parsed

            if not transform:
                continue

            current_sub_matrix = cmds.xform(transform,
                                            query=True,
                                            matrix=True,
                                            objectSpace=True)
            current_hidden = not cmds.getAttr(transform + ".visibility")

            origin_sub_matrix, origin_hidden = old_data_map.get(
                transform, (None, False))

            # Updating matrix
            if origin_sub_matrix:
                has_matrix_override = not matrix_equals(
                    current_sub_matrix, origin_sub_matrix)
            else:
                has_matrix_override = False

            if has_matrix_override and not force:
                self.log.warning("Sub-Matrix override preserved on %s",
                                 transform)
            else:
                with self.keep_scale_pivot(transform):
                    cmds.xform(transform, objectSpace=True, matrix=sub_matrix)

            # Updating visibility
            if origin_hidden:
                has_hidden_override = current_hidden != origin_hidden
            else:
                has_hidden_override = False

            if has_hidden_override and not force:
                self.log.warning("Visibility override preserved on %s",
                                 transform)
            elif force:
                if current_hidden and not is_hidden:
                    cmds.setAttr(transform + ".visibility", True)
                elif not current_hidden and is_hidden:
                    cmds.setAttr(transform + ".visibility", False)
            else:
                if origin_hidden and not is_hidden:
                    cmds.setAttr(transform + ".visibility", True)
                elif not origin_hidden and is_hidden:
                    cmds.setAttr(transform + ".visibility", False)
Example #5
0
    def _collect_components_matrix(self, data, container):
        from maya import cmds
        from reveries.lib import DEFAULT_MATRIX, matrix_equals
        from reveries.maya import utils as maya_utils
        from reveries.maya import hierarchy

        id_path = hierarchy.container_to_id_path(container)

        data["subMatrix"][id_path] = dict()
        data["inheritsTransform"][id_path] = dict()
        data["hidden"][id_path] = dict()

        nodes = cmds.sets(container["objectName"], query=True, nodesOnly=True)

        # Alembic, If any..
        # (NOTE) Shouldn't be extracted here with matrix, need decouple
        if container["loader"] == "PointCacheReferenceLoader":
            abc = cmds.ls(nodes, type="AlembicNode")
            if abc:
                abc = abc[0]  # Should have one and only one alembic node
                data["alembic"][id_path] = [
                    cmds.getAttr(abc + ".speed"),
                    cmds.getAttr(abc + ".offset"),
                    cmds.getAttr(abc + ".cycleType"),
                ]

        # Transform Matrix
        #
        transforms = cmds.ls(nodes, type="transform", referencedNodes=True)
        transforms = set(transforms) - set(cmds.ls(transforms, type=["joint"]))

        for transform in transforms:
            matrix = cmds.xform(transform,
                                query=True,
                                matrix=True,
                                objectSpace=True)

            if matrix_equals(matrix, DEFAULT_MATRIX):
                matrix = "<default>"

            address = maya_utils.get_id(transform)
            short = transform.split("|")[-1].split(":")[-1]
            # (NOTE) New data model for duplicated AvalonID..
            #   Use transform node's short name as a buffer for AvalonID
            #   duplication..
            if address not in data["subMatrix"][id_path]:
                data["subMatrix"][id_path][address] = dict()
            data["subMatrix"][id_path][address][short] = matrix

            # Collect `inheritsTransform`...
            inherits = cmds.getAttr(transform + ".inheritsTransform")
            if address not in data["inheritsTransform"][id_path]:
                # (NOTE) New data model for duplicated AvalonID..
                data["inheritsTransform"][id_path][address] = dict()
            data["inheritsTransform"][id_path][address][short] = inherits

            # Collect visbility with matrix
            visibility = cmds.getAttr(transform + ".visibility")
            if not visibility:
                # Only record hidden nodes
                if address not in data["hidden"][id_path]:
                    # (NOTE) New data model for duplicated AvalonID..
                    data["hidden"][id_path][address] = list()
                data["hidden"][id_path][address].append(short)

        # Collect subseet group node's matrix
        subset_group = container["subsetGroup"]

        matrix = cmds.xform(subset_group,
                            query=True,
                            matrix=True,
                            objectSpace=True)
        inherits = cmds.getAttr(subset_group + ".inheritsTransform")

        name = subset_group.rsplit(":", 1)[-1]
        data["subMatrix"][id_path]["GROUP"] = {name: matrix}
        data["inheritsTransform"][id_path]["GROUP"] = {name: inherits}