Skip to content

load_model_obj

ObjLoader

Bases: LoaderPlugin

Obj Loader.

Source code in client/ayon_max/plugins/load/load_model_obj.py
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
class ObjLoader(load.LoaderPlugin):
    """Obj Loader."""

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

    def load(self, context, name=None, namespace=None, data=None):
        from pymxs import runtime as rt

        filepath = os.path.normpath(self.filepath_from_context(context))
        self.log.debug("Executing command to import..")

        rt.Execute(f'importFile @"{filepath}" #noPrompt using:ObjImp')

        namespace = unique_namespace(
            name + "_",
            suffix="_",
        )
        # create "missing" container for obj import
        selections = rt.GetCurrentSelection()
        # get current selection
        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)
        namespace, _ = get_namespace(node_name)
        node_list = get_previous_loaded_object(node)
        rt.Select(node_list)
        previous_objects = rt.GetCurrentSelection()
        transform_data = object_transform_set(previous_objects)
        for prev_obj in previous_objects:
            if rt.isValidNode(prev_obj):
                rt.Delete(prev_obj)

        rt.Execute(f'importFile @"{path}" #noPrompt using:ObjImp')
        # get current selection
        selections = rt.GetCurrentSelection()
        for selection in selections:
            selection.name = f"{namespace}:{selection.name}"
            selection_transform = f"{selection}.transform"
            if selection_transform in transform_data.keys():
                selection.pos = transform_data[selection_transform] or 0
                selection.scale = transform_data[
                    f"{selection}.scale"] or 0
        update_custom_attribute_data(node, selections)
        with maintained_selection():
            rt.Select(node)

        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)