Skip to content

extract_camera

Extract camera from Unreal.

ExtractCamera

Bases: Extractor

Extract a camera.

Source code in client/ayon_unreal/plugins/publish/extract_camera.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
class ExtractCamera(publish.Extractor):
    """Extract a camera."""

    label = "Extract Camera"
    hosts = ["unreal"]
    families = ["camera"]
    optional = True

    def process(self, instance):
        ar = unreal.AssetRegistryHelpers.get_asset_registry()

        # Define extract output file path
        staging_dir = self.staging_dir(instance)
        fbx_filename = "{}.fbx".format(instance.name)

        # Perform extraction
        self.log.info("Performing extraction..")
        # Check if the loaded level is the same of the instance
        if UNREAL_VERSION.major == 5:
            world = unreal.UnrealEditorSubsystem().get_editor_world()
        else:
            world = unreal.EditorLevelLibrary.get_editor_world()
        current_level = world.get_path_name()
        assert current_level == instance.data.get("level"), \
            "Wrong level loaded"

        for member in instance.data.get('members'):
            data = ar.get_asset_by_object_path(member)
            if UNREAL_VERSION.major == 5:
                is_level_sequence = (
                    data.asset_class_path.asset_name == "LevelSequence")
            else:
                is_level_sequence = (data.asset_class == "LevelSequence")

            if is_level_sequence:
                sequence = data.get_asset()
                with select_camera(sequence):
                    if UNREAL_VERSION.major == 5:
                        params = None
                        if UNREAL_VERSION.minor >= 4:
                            params = unreal.SequencerExportFBXParams(
                                world=world,
                                root_sequence=sequence,
                                sequence=sequence,
                                bindings=sequence.get_bindings(),
                                fbx_file_name=os.path.join(staging_dir, fbx_filename)
                            )
                        else:
                            params = unreal.SequencerExportFBXParams(
                                world=world,
                                root_sequence=sequence,
                                sequence=sequence,
                                bindings=sequence.get_bindings(),
                                master_tracks=get_tracks(sequence),
                                fbx_file_name=os.path.join(staging_dir, fbx_filename)
                            )
                        unreal.SequencerTools.export_level_sequence_fbx(params)
                    elif UNREAL_VERSION.major == 4 and UNREAL_VERSION.minor == 26:
                        unreal.SequencerTools.export_fbx(
                            world,
                            sequence,
                            sequence.get_bindings(),
                            unreal.FbxExportOption(),
                            os.path.join(staging_dir, fbx_filename)
                        )
                    else:
                        # Unreal 5.0 or 4.27
                        unreal.SequencerTools.export_level_sequence_fbx(
                            world,
                            sequence,
                            sequence.get_bindings(),
                            unreal.FbxExportOption(),
                            os.path.join(staging_dir, fbx_filename)
                        )

                    if not os.path.isfile(os.path.join(staging_dir, fbx_filename)):
                        raise RuntimeError("Failed to extract camera")

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

        fbx_representation = {
            'name': 'fbx',
            'ext': 'fbx',
            'files': fbx_filename,
            'clipIn': instance.data["clipIn"],
            'clipOut': instance.data["clipOut"],
            "stagingDir": staging_dir,
        }
        instance.data["representations"].append(fbx_representation)