Skip to content

load_model_fbx

FbxModelLoader

Bases: LoaderPlugin

Fbx Model Loader.

Source code in client/ayon_max/plugins/load/load_model_fbx.py
 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
class FbxModelLoader(load.LoaderPlugin):
    """Fbx Model Loader."""

    product_types = {"model"}
    representations = {"fbx"}
    order = -9
    icon = "code-fork"
    color = "white"

    def load(self, context, name=None, namespace=None, data=None):
        from pymxs import runtime as rt
        filepath = self.filepath_from_context(context)
        filepath = os.path.normpath(filepath)
        rt.FBXImporterSetParam("Animation", False)
        rt.FBXImporterSetParam("Cameras", False)
        rt.FBXImporterSetParam("Mode", rt.Name("create"))
        rt.FBXImporterSetParam("Preserveinstances", True)
        rt.importFile(
            filepath, rt.name("noPrompt"), using=rt.FBXIMP)

        namespace = unique_namespace(
            name + "_",
            suffix="_",
        )
        selections = rt.GetCurrentSelection()

        for selection in selections:
            selection.name = f"{namespace}:{selection.name}"

        return containerise(
            name, selections, context,
            namespace, loader=self.__class__.__name__)

    def update(self, container, context):
        from pymxs import runtime as rt

        repre_entity = context["representation"]
        path = os.path.normpath(self.filepath_from_context(context))
        node_name = container["instance_node"]
        node = rt.getNodeByName(node_name)
        if not node:
            rt.Container(name=node_name)
        namespace, _ = get_namespace(node_name)

        node_list = get_previous_loaded_object(node)
        rt.Select(node_list)
        prev_fbx_objects = rt.GetCurrentSelection()
        transform_data = object_transform_set(prev_fbx_objects)
        for prev_fbx_obj in prev_fbx_objects:
            if rt.isValidNode(prev_fbx_obj):
                rt.Delete(prev_fbx_obj)

        rt.FBXImporterSetParam("Animation", False)
        rt.FBXImporterSetParam("Cameras", False)
        rt.FBXImporterSetParam("Mode", rt.Name("create"))
        rt.FBXImporterSetParam("Preserveinstances", True)
        rt.importFile(path, rt.name("noPrompt"), using=rt.FBXIMP)
        current_fbx_objects = rt.GetCurrentSelection()
        fbx_objects = []
        for fbx_object in current_fbx_objects:
            fbx_object.name = f"{namespace}:{fbx_object.name}"
            fbx_objects.append(fbx_object)
            fbx_transform = f"{fbx_object}.transform"
            if fbx_transform in transform_data.keys():
                fbx_object.pos = transform_data[fbx_transform] or 0
                fbx_object.scale = transform_data[
                    f"{fbx_object}.scale"] or 0

        with maintained_selection():
            rt.Select(node)
        update_custom_attribute_data(node, fbx_objects)

        lib.imprint(container["instance_node"], {
            "representation": repre_entity["id"],
            "project_name": context["project"]["name"]
        })

    def switch(self, container, context):
        self.update(container, context)

    def remove(self, container):
        from pymxs import runtime as rt
        node = rt.GetNodeByName(container["instance_node"])
        remove_container_data(node)