Skip to content

load_mesh

SubstanceLoadProjectMesh

Bases: LoaderPlugin

Load mesh for project

Source code in client/ayon_substancepainter/plugins/load/load_mesh.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
class SubstanceLoadProjectMesh(load.LoaderPlugin):
    """Load mesh for project"""

    product_types = {"*"}
    representations = {"abc", "fbx", "obj", "gltf", "usd", "usda", "usdc"}

    label = "Load mesh"
    order = -10
    icon = "code-fork"
    color = "orange"

    # Defined via settings
    project_templates = []

    def load(self, context, name, namespace, options=None):

        # Get user inputs
        result = SubstanceProjectConfigurationWindow.prompt(
            self.project_templates)
        if not result:
            # cancelling loader action
            return
        if not substance_painter.project.is_open():
            # Allow to 'initialize' a new project
            path = self.filepath_from_context(context)
            sp_settings = substance_painter.project.Settings(
                import_cameras=result["import_cameras"],
                normal_map_format=result["normal_map_format"],
                project_workflow=result["project_workflow"],
                tangent_space_mode=result["tangent_space_mode"],
                default_texture_resolution=result["default_texture_resolution"]
            )
            settings = substance_painter.project.create(
                mesh_file_path=path, settings=sp_settings
            )
        else:
            # Reload the mesh
            settings = substance_painter.project.MeshReloadingSettings(
                import_cameras=result["import_cameras"],
                preserve_strokes=result["preserve_strokes"])

            def on_mesh_reload(status: substance_painter.project.ReloadMeshStatus):  # noqa
                if status == substance_painter.project.ReloadMeshStatus.SUCCESS:  # noqa
                    self.log.info("Reload succeeded")
                else:
                    raise LoadError("Reload of mesh failed")

            path = self.filepath_from_context(context)
            substance_painter.project.reload_mesh(path,
                                                  settings,
                                                  on_mesh_reload)

        # Store container
        container = {}
        project_mesh_object_name = "_ProjectMesh_"
        imprint_container(container,
                          name=project_mesh_object_name,
                          namespace=project_mesh_object_name,
                          context=context,
                          loader=self)

        # We want store some options for updating to keep consistent behavior
        # from the user's original choice. We don't store 'preserve_strokes'
        # as we always preserve strokes on updates.
        container["options"] = {
            "import_cameras": result["import_cameras"],
        }

        set_container_metadata(project_mesh_object_name, container)

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

    def update(self, container, context):
        repre_entity = context["representation"]

        path = get_representation_path(repre_entity)

        # Reload the mesh
        container_options = container.get("options", {})
        settings = substance_painter.project.MeshReloadingSettings(
            import_cameras=container_options.get("import_cameras", True),
            preserve_strokes=True
        )

        def on_mesh_reload(status: substance_painter.project.ReloadMeshStatus):
            if status == substance_painter.project.ReloadMeshStatus.SUCCESS:
                self.log.info("Reload succeeded")
            else:
                raise LoadError("Reload of mesh failed")

        substance_painter.project.reload_mesh(path, settings, on_mesh_reload)

        # Update container representation
        object_name = container["objectName"]
        update_data = {
            "representation": repre_entity["id"],
            "project_name": context["project"]["name"]
        }
        set_container_metadata(object_name, update_data, update=True)

    def remove(self, container):

        # Remove OpenPype related settings about what model was loaded
        # or close the project?
        # TODO: This is likely best 'hidden' away to the user because
        #       this will leave the project's mesh unmanaged.
        remove_container_metadata(container["objectName"])

SubstanceProjectConfigurationWindow

Bases: QDialog

The pop-up dialog allows users to choose material duplicate options for importing Max objects when updating or switching assets.

Source code in client/ayon_substancepainter/plugins/load/load_mesh.py
 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
class SubstanceProjectConfigurationWindow(QtWidgets.QDialog):
    """The pop-up dialog allows users to choose material
    duplicate options for importing Max objects when updating
    or switching assets.
    """
    def __init__(self, project_templates):
        super(SubstanceProjectConfigurationWindow, self).__init__()
        self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint)

        self.configuration = None
        self.template_names = [template["name"] for template
                               in project_templates]
        self.project_templates = project_templates

        self.widgets = {
            "label": QtWidgets.QLabel(
                "Select your template for project configuration"),
            "template_options": QtWidgets.QComboBox(),
            "import_cameras": QtWidgets.QCheckBox("Import Cameras"),
            "preserve_strokes": QtWidgets.QCheckBox("Preserve Strokes"),
            "clickbox": QtWidgets.QWidget(),
            "combobox": QtWidgets.QWidget(),
            "buttons": QtWidgets.QDialogButtonBox(
                QtWidgets.QDialogButtonBox.Ok
                | QtWidgets.QDialogButtonBox.Cancel)
        }

        self.widgets["template_options"].addItems(self.template_names)

        template_name = self.widgets["template_options"].currentText()
        self._update_to_match_template(template_name)
        # Build clickboxes
        layout = QtWidgets.QHBoxLayout(self.widgets["clickbox"])
        layout.addWidget(self.widgets["import_cameras"])
        layout.addWidget(self.widgets["preserve_strokes"])
        # Build combobox
        layout = QtWidgets.QHBoxLayout(self.widgets["combobox"])
        layout.addWidget(self.widgets["template_options"])
        # Build buttons
        layout = QtWidgets.QHBoxLayout(self.widgets["buttons"])
        # Build layout.
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.widgets["label"])
        layout.addWidget(self.widgets["combobox"])
        layout.addWidget(self.widgets["clickbox"])
        layout.addWidget(self.widgets["buttons"])

        self.widgets["template_options"].currentTextChanged.connect(
            self._update_to_match_template)
        self.widgets["buttons"].accepted.connect(self.on_accept)
        self.widgets["buttons"].rejected.connect(self.on_reject)

    def on_accept(self):
        self.configuration = self.get_project_configuration()
        self.close()

    def on_reject(self):
        self.close()

    def _update_to_match_template(self, template_name):
        template = get_template_by_name(template_name, self.project_templates)
        self.widgets["import_cameras"].setChecked(template["import_cameras"])
        self.widgets["preserve_strokes"].setChecked(
            template["preserve_strokes"])

    def get_project_configuration(self):
        templates = self.project_templates
        template_name = self.widgets["template_options"].currentText()
        template = get_template_by_name(template_name, templates)
        template = copy.deepcopy(template)  # do not edit the original
        template["import_cameras"] = self.widgets["import_cameras"].isChecked()
        template["preserve_strokes"] = (
            self.widgets["preserve_strokes"].isChecked()
        )
        for key in ["normal_map_format",
                    "project_workflow",
                    "tangent_space_mode"]:
            template[key] = _convert(template[key])
        return template

    @classmethod
    def prompt(cls, templates):
        dialog = cls(templates)
        dialog.exec_()
        configuration = dialog.configuration
        dialog.deleteLater()
        return configuration