Simple Maya script

Written by Samuel Rantaeskola, Product Expert, Simplygon

Disclaimer: The code in this post is written on version 10.3.5200 of Simplygon. Should you encounter this post running a different version, some of the API calls might differ. However, the core concepts should still remain valid.

Introduction

In this post, we'll guide you through using Simplygon's scripting support in Maya to transfer data from Maya to Simplygon, run a process, and bring the results back into Maya. This tutorial will provide a solid foundation for applying tips and tricks from other blogs directly within Maya.

Test asset

In our example, we'll use this ornate vase and reduce its poly count by 50%. The original asset contains 9,800 polygons:

Vase

Exporting

The first step is to export the asset from Maya so it can be processed in Simplygon. Below is a simple Python function that handles the export, using a temporary file to transfer the data:

def export_selection(sg: Simplygon.ISimplygon, temp_file: str) -> Simplygon.spScene:
    cmds.Simplygon(exp=temp_file)
    scene = sg.CreateScene()
    scene.LoadFromFile(temp_file)
    return scene

Importing

Now, let's set things up to bring the processed results back into Maya once the process is complete. The following Python function imports the processed scene back into Maya:

def import_process_results(scene: Simplygon.spScene, temp_file: str) -> None:
    scene.SaveToFile(temp_file)
    cmds.Simplygon(imp=temp_file, lma = True)

String it all together

Now that we can export data from Maya and import it back, we have everything in place to process the asset. Below is the function that handles the processing:

def process_selection(sg: Simplygon.ISimplygon, temp_file: str, reduction_ratio: float) -> None:
    scene = export_selection(sg, temp_file)
    pipeline = sg.CreateReductionPipeline()
    pipeline.GetReductionSettings().SetReductionTargetTriangleRatio(reduction_ratio)
    pipeline.RunScene(scene, Simplygon.EPipelineRunMode_RunInThisProcess) 
    import_process_results(scene, temp_file)

Testing our scripts

With everything set up, we can now proceed to process our test asset. Here are the final results: Results

The scripts

# Copyright (c) Microsoft Corporation. 
# Licensed under the MIT license. 
 
import maya.cmds as cmds
from simplygon10 import simplygon_loader
from simplygon10 import Simplygon
import os

def export_selection(sg: Simplygon.ISimplygon, temp_file: str) -> Simplygon.spScene:
    """
    Exports the current selection into a Simplygon scene.
    :param sg: Simplygon instance
    :param temp_file: a file that will used to intermediately pass the data into Simplygon
    :return: a Simplygon scene that contains the selection in the Simplygon format
    """     
    cmds.Simplygon(exp=temp_file)
    scene = sg.CreateScene()
    scene.LoadFromFile(temp_file)
    return scene

def import_process_results(scene: Simplygon.spScene, temp_file: str) -> None:
    """
    Imports the process results into the scene.
    :param sg: Simplygon instance
    :param temp_file: a file that will used to intermediately pass the data into Maya
    """     
    scene.SaveToFile(temp_file)
    # Lma = True means that materials coming in will be mapped to existing materials in Maya
    cmds.Simplygon(imp=temp_file, lma = True)

def process_selection(sg: Simplygon.ISimplygon, temp_file: str, reduction_ratio: float) -> None:
    """
    Reduces the selected objects to the specified percentage and returns the results back into Maya.
    :param sg: Simplygon instance
    :param temp_file: a file that will used to pass data back and forth
    :param reduction_ratio: how much to reduce the asset to
    """     
    scene = export_selection(sg, temp_file)
    pipeline = sg.CreateReductionPipeline()
    pipeline.GetReductionSettings().SetReductionTargetTriangleRatio(reduction_ratio)
    pipeline.RunScene(scene, Simplygon.EPipelineRunMode_RunInThisProcess) 
    import_process_results(scene, temp_file)
        

sg = simplygon_loader.init_simplygon()
print(sg.GetVersion())
temp_file = "c:/tmp/tmp.sb"
process_selection(sg, temp_file, 0.5)
# Do some clean up
os.remove(temp_file)
del sg
⇐ Back to all posts

Request 30-days free evaluation license

*
*
*
*
Industry
*

Request 30-days free evaluation license

*
*
*
*
Industry
*