Skip to content

extract_layout

ExtractLayout

Bases: Extractor

Extract a layout.

Source code in client/ayon_unreal/plugins/publish/extract_layout.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class ExtractLayout(publish.Extractor):
    """Extract a layout."""

    label = "Extract Layout"
    hosts = ["unreal"]
    families = ["layout"]
    optional = True

    def process(self, instance):
        # Define extract output file path
        staging_dir = self.staging_dir(instance)

        # Perform extraction
        self.log.info("Performing extraction..")

        # Check if the loaded level is the same of the instance
        current_level = ell.get_editor_world().get_path_name()
        assert current_level == instance.data.get("level"), \
            "Wrong level loaded"

        json_data = []
        project_name = instance.context.data["projectName"]
        eas = unreal.EditorActorSubsystem()
        sel_actors = eas.get_all_level_actors()
        members = set(instance.data.get("members", []))
        actors = [a for a in sel_actors if a.get_path_name() in members]
        for actor in actors:
            mesh = None
            # Check type the type of mesh
            if actor.get_class().get_name() == 'SkeletalMeshActor':
                mesh = actor.skeletal_mesh_component.skeletal_mesh
            elif actor.get_class().get_name() == 'StaticMeshActor':
                mesh = actor.static_mesh_component.static_mesh

            if mesh:
                # Search the reference to the Asset Container for the object
                path = unreal.Paths.get_path(mesh.get_path_name())
                filter = unreal.ARFilter(
                    class_names=["AyonAssetContainer"], package_paths=[path])
                ar = unreal.AssetRegistryHelpers.get_asset_registry()
                try:
                    asset_container = ar.get_assets(filter)[0].get_asset()
                except IndexError:
                    self.log.error("AssetContainer not found.")
                    return

                parent_id = eal.get_metadata_tag(asset_container, "parent")
                repre_id = eal.get_metadata_tag(asset_container, "representation")
                family = eal.get_metadata_tag(asset_container, "family")
                json_element = {}
                json_element["reference"] = str(repre_id)
                json_element["representation"] = str(repre_id)
                # TODO: remove the option after tweaking
                # the layout loader in blender
                if instance.data.get("export_blender", False):
                    blend = ayon_api.get_representation_by_name(
                        project_name, "blend", parent_id, fields={"id"}
                    )
                    blend_id = blend["id"]
                    json_element["reference"] = str(blend_id)
                instance_name = mesh.get_name()
                extension = instance_name.split("_")[-1]
                asset_name = re.match(f'(.+)_{extension}$', instance_name)
                json_element["version"] = str(parent_id)
                json_element["product_type"] = family
                json_element["instance_name"] = asset_name.group(1)
                json_element["asset_name"] = instance_name
                json_element["extension"] = extension
                transform = actor.get_actor_transform()
                json_element["host"] = self.hosts
                json_element["transform"] = {
                    "translation": {
                        "x": transform.translation.x,
                        "y": transform.translation.y,
                        "z": transform.translation.z
                    },
                    "rotation": {
                        "x": math.radians(transform.rotation.euler().x),
                        "y": math.radians(transform.rotation.euler().y),
                        "z": math.radians(transform.rotation.euler().z)
                    },
                    "scale": {
                        "x": transform.scale3d.x,
                        "y": transform.scale3d.y,
                        "z": transform.scale3d.z
                    }
                }
                json_element["transform_matrix"] = self.get_transform_matrix(transform)
                json_element["basis"] = self.get_basis_matrix()
                json_element["rotation"] = {
                    "x": transform.rotation.euler().x,
                    "y": transform.rotation.euler().y,
                    "z": transform.rotation.euler().z
                }
                json_data.append(json_element)

        json_filename = "{}.json".format(instance.name)
        json_path = os.path.join(staging_dir, json_filename)

        with open(json_path, "w+") as file:
            json.dump(json_data, fp=file, indent=2)

        if "representations" not in instance.data:
            instance.data["representations"] = []

        json_representation = {
            'name': 'json',
            'ext': 'json',
            'files': json_filename,
            "stagingDir": staging_dir,
        }
        instance.data["representations"].append(json_representation)

    def get_basis_matrix(self):
        """Get Identity matrix

        Returns:
            list: list of identity matrix
        """
        # Create an identity matrix
        identity_matrix = unreal.Matrix.IDENTITY

        basis_list = [
            [identity_matrix.x_plane.x, identity_matrix.x_plane.y,
            identity_matrix.x_plane.z, identity_matrix.x_plane.w],
            [identity_matrix.y_plane.x, identity_matrix.y_plane.y,
            identity_matrix.y_plane.z, identity_matrix.y_plane.w],
            [identity_matrix.z_plane.x, identity_matrix.z_plane.y,
            identity_matrix.z_plane.z, identity_matrix.z_plane.w],
            [identity_matrix.w_plane.x, identity_matrix.w_plane.y,
            identity_matrix.w_plane.z, identity_matrix.w_plane.w]
        ]
        return basis_list

    def get_transform_matrix(self, transform):
        """Get transform matrix for each actor

        Args:
            transform (Matrix): Actor's transformation

        Returns:
            list: Actor's transformation data
        """
        translation = [
            transform.translation.x,
            transform.translation.z,
            transform.translation.y
        ]
        rotation = [
            transform.rotation.euler().x,
            transform.rotation.euler().z,
            transform.rotation.euler().y
        ]
        scale = [
            transform.scale3d.x,
            transform.scale3d.z,
            transform.scale3d.y,
        ]
        transform = unreal.Transform(
            location=translation,
            rotation=rotation,
            scale=scale
        )
        transform_m_matrix = transform.to_matrix()
        transform_matrix = [
            [transform_m_matrix.x_plane.x, transform_m_matrix.x_plane.y,
             transform_m_matrix.x_plane.z, transform_m_matrix.x_plane.w],
            [transform_m_matrix.y_plane.x, transform_m_matrix.y_plane.y,
             transform_m_matrix.y_plane.z, transform_m_matrix.y_plane.w],
            [transform_m_matrix.z_plane.x, transform_m_matrix.z_plane.y,
             transform_m_matrix.z_plane.z, transform_m_matrix.z_plane.w],
            [transform_m_matrix.w_plane.x, transform_m_matrix.w_plane.y,
             transform_m_matrix.w_plane.z, transform_m_matrix.w_plane.w]
        ]
        return transform_matrix

get_basis_matrix()

Get Identity matrix

Returns:

Name Type Description
list

list of identity matrix

Source code in client/ayon_unreal/plugins/publish/extract_layout.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
def get_basis_matrix(self):
    """Get Identity matrix

    Returns:
        list: list of identity matrix
    """
    # Create an identity matrix
    identity_matrix = unreal.Matrix.IDENTITY

    basis_list = [
        [identity_matrix.x_plane.x, identity_matrix.x_plane.y,
        identity_matrix.x_plane.z, identity_matrix.x_plane.w],
        [identity_matrix.y_plane.x, identity_matrix.y_plane.y,
        identity_matrix.y_plane.z, identity_matrix.y_plane.w],
        [identity_matrix.z_plane.x, identity_matrix.z_plane.y,
        identity_matrix.z_plane.z, identity_matrix.z_plane.w],
        [identity_matrix.w_plane.x, identity_matrix.w_plane.y,
        identity_matrix.w_plane.z, identity_matrix.w_plane.w]
    ]
    return basis_list

get_transform_matrix(transform)

Get transform matrix for each actor

Parameters:

Name Type Description Default
transform Matrix

Actor's transformation

required

Returns:

Name Type Description
list

Actor's transformation data

Source code in client/ayon_unreal/plugins/publish/extract_layout.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def get_transform_matrix(self, transform):
    """Get transform matrix for each actor

    Args:
        transform (Matrix): Actor's transformation

    Returns:
        list: Actor's transformation data
    """
    translation = [
        transform.translation.x,
        transform.translation.z,
        transform.translation.y
    ]
    rotation = [
        transform.rotation.euler().x,
        transform.rotation.euler().z,
        transform.rotation.euler().y
    ]
    scale = [
        transform.scale3d.x,
        transform.scale3d.z,
        transform.scale3d.y,
    ]
    transform = unreal.Transform(
        location=translation,
        rotation=rotation,
        scale=scale
    )
    transform_m_matrix = transform.to_matrix()
    transform_matrix = [
        [transform_m_matrix.x_plane.x, transform_m_matrix.x_plane.y,
         transform_m_matrix.x_plane.z, transform_m_matrix.x_plane.w],
        [transform_m_matrix.y_plane.x, transform_m_matrix.y_plane.y,
         transform_m_matrix.y_plane.z, transform_m_matrix.y_plane.w],
        [transform_m_matrix.z_plane.x, transform_m_matrix.z_plane.y,
         transform_m_matrix.z_plane.z, transform_m_matrix.z_plane.w],
        [transform_m_matrix.w_plane.x, transform_m_matrix.w_plane.y,
         transform_m_matrix.w_plane.z, transform_m_matrix.w_plane.w]
    ]
    return transform_matrix