A simple reduction script in Blender
Disclaimer: The code in this post is written on version 9.1 of Simplygon. If you encounter this post at a later stage, some of the API calls might have changed. However, the core concepts should still remain valid.
This blog will give you a simple starting point into scripting with Simplygon's Python API in Blender. Before we get cranking with scripting we need make sure that Simplygon runs in Blender. Getting started with Simplygon in Blender gives you a rapid overview process. In short the steps are:
- Install Simplygon on your machine. This will automatically add the Simplygon plug-in to your Blender installation.
- Enable the Simplygon plug-in in Blender Preferences->Add-ons in the Community section. Now you can go ahead and use the Simplygon UI in Blender.
Using Simplygon's Python API does not require any setup, it is added to the Python environment and can be used straight away.
Creating your first script
We will create a simple reduction script in the Scripting section of Blender and run the script on this asset.
Open the script section and write the following imports into a new script.
import os import bpy from simplygon import simplygon_loader from simplygon import Simplygon
With these packages we got everything we need to write our simple reduction script.
The reduce function
We will set up a reduce function that executes the reduction process on the current selection and imports back the results from the reduction process.
def reduce_selection(sg, ratio):
The input variables are sg, an initialized Simplygon instance, and ratio, the amount we want to reduce the asset to.
Intermediate file exporting
The Simplygon plug-in for Blender uses glTF as an intermediate format, which is what we will do in this script as well. We will need a intermediate file through which we can pass the data. Let's add that and the export from Blender.
file_path = 'c:/tmp/_intermediate.glb' bpy.ops.export_scene.gltf(filepath = file_path, use_selection=True)
The reduction pipeline
Now that we have exported the data from Blender we can bring that in to Simplygon. First we want to set up a reduction pipeline that we will run the reduction with. We will modify the triangle ratio on the reduction settings of the pipeline object to the desired reduction ratio. Here's the code that sets all this up.
pipeline = sg.CreateReductionPipeline() pipeline.GetReductionSettings().SetReductionTargetTriangleRatio(ratio)
Running the process
With the pipeline setup we can go ahead and process the object straight from the intermediate file and pipe the results back the same way. You could alternatively load the file into a Simplygon scene object using the scene importer class. The latter would be the best approach if you wanted to modify the scene prior to running the reduction. In our case, we just want to run a simple reduction so we add this line to our function and run straight from file.
pipeline.RunSceneFromFile(file_path, file_path, Simplygon.EPipelineRunMode_RunInThisProcess)
The pipeline run mode is worth mentioning. It is possible to distribute Simplygon process to a remote computer, using either the built in Grid functionality or through other distribution infrastructures. This makes sense when you are batching through large number of assets, which isn't the case right now. We'll just wait for the process to finish on this machine.
Initiating the process
Finally, the process is now done and we can import the results back into Blender and remove the intermediate file.
That's it. We now have a reduction function that we can use to process an object within Blender. Now we just need to initialize Simplygon and call the reduction function like this.
sg = simplygon_loader.init_simplygon() reduce_selection(sg, 0.5) sg = None
The last line makes sure that Simplygon is unloaded from the Python environment. It's always a good habit to add that, as it ensures that you don't have several Simplygon instances when running multiple scripts. Now we can run the process, to produce a 50% reduction of the asset we showed earlier.
# Copyright (c) Microsoft Corporation. # Licensed under the MIT license. import os import bpy from simplygon import simplygon_loader from simplygon import Simplygon def reduce_selection(sg, ratio): file_path = 'c:/tmp/_intermediate.glb' bpy.ops.export_scene.gltf(filepath = file_path, use_selection=True) pipeline = sg.CreateReductionPipeline() pipeline.GetReductionSettings().SetReductionTargetTriangleRatio(ratio) pipeline.RunSceneFromFile(file_path, file_path, Simplygon.EPipelineRunMode_RunInThisProcess) bpy.ops.import_scene.gltf(filepath=file_path) os.remove(file_path) sg = simplygon_loader.init_simplygon() reduce_selection(sg, 0.5) sg = None