Untitled

 avatar
unknown
plain_text
15 days ago
2.2 kB
4
Indexable
import bpy
from ayon_core.pipeline import publish
from ayon_blender.api import plugin
from pathlib import Path


class ExtractLook(plugin.BlenderExtractor, publish.OptionalPyblishPluginMixin):
    """Extract Look from Ayon-tracked objects and publish them as LookFiles."""
    label = "Extract Look File"
    hosts = ["blender"]
    families = ["look"]
    optional = True

    def process(self, instance):
        """Extracts materials and saves them into a dedicated .blend file."""
        if not self.is_active(instance.data):
            return

        # Define extract output file path using Ayon's staging directory
        stagingdir = Path(self.staging_dir(instance))
        folder_name = instance.data.get("folderEntity", {}).get("name", "UnknownFolder")
        product_name = instance.data.get("productName", "UnknownProduct")
        instance_name = f"{folder_name}_{product_name}"
        filename = f"{instance_name}.blend"
        export_path = stagingdir / filename

        # Ensure the export directory exists
        stagingdir.mkdir(parents=True, exist_ok=True)

        # Save the current Blender file to the export path
        bpy.ops.wm.save_as_mainfile(filepath=str(export_path))

        # Store extraction data for Ayon
        instance.data["representations"] = [{
            "name": "look",
            "ext": "blend",
            "files": filename,
            "stagingDir": str(stagingdir),
        }]

        self.log.info(f"Extracted materials to {export_path}")

    def get_ayon_materials(self, instance):
        """Finds all materials linked to objects with Ayon metadata."""
        ayon_materials = []
        for mat in bpy.data.materials:
            if "avalon" in mat and mat["avalon"].get("productType") == "look":
                ayon_materials.append(mat)
        return ayon_materials

    def clean_scene(self):
        """Removes all objects, meshes, and collections except materials."""
        bpy.ops.object.select_all(action='SELECT')
        bpy.ops.object.delete(use_global=False)
        bpy.ops.outliner.orphans_purge()
        self.log.info("Scene cleaned, only materials remain.")
Editor is loading...
Leave a Comment