[Tuto] How to use Pixyz SDK function in the Pixyz Unity Plugin

Hello dear community,

We had the chance to meet some of you during the Brighton Industry User Day last week, and a team reported an issue they experienced with matrix transformations after importing their files. After some investigation, we found this problem could be easily addressed with a few calls to the Pixyz SDK. However, the relevant functions were not directly exposed as Action in the Pixyz plugin.

This is a perfect example to introduce how to call Pixyz’s C# SDK functions using the plugin.

Firstly, I want to thank the team for traveling to meet us in person, taking the time to explain their issue, and allowing me to publicly share a couple of screenshots featuring their data. :slightly_smiling_face:


The Process Overview

The overall process will be very similar to creating a generic Custom Action, but in this case, we’re creating a Pixyz Custom Action.


The Issue:

After importing a file, they end up with a prefab that has the correct appearance and placement. However, every node beneath the root node has rotations and scales applied. If we try to set every transformation matrix to Identity, the object (in this case, the boat) loses its correct orientation and positioning.

This suggests it’s not a case of transformations compensating for each other across different nodes, but rather a situation where the vertices are improperly placed and the transformations are being used to compensate for this misalignment.


The Solution:

The solution involves overriding each vertex by multiplying them with the transformation matrix, then setting the matrix to Identity. Fortunately, there’s already a function in the Pixyz SDK for this purpose — it’s called resetTransform. However, this function is not (yet) wrapped directly as an Action, meaning we can’t call it automatically in the import process via the RuleSet mechanism.

To address this, we need to create a Custom PixyzAction.


Step-by-Step Implementation:

1. Create the Pixyz Custom Action Script:
Right-click in the Inspector, go to Create -> Pixyz -> C# Custom Pixyz Action, and name the action script BakeTransformation


2. Edit the Script Using Your IDE:
Open the script in your IDE and start implementing the Pixyz SDK functionality. In this case, delete all the parameters with the [UserParameter] attribute because we don’t need them.


Understanding the Logic:
The core functionality will go in the Run method. Unlike regular Custom Actions that operate on Unity GameObjects, Pixyz Custom Actions work on Occurrences (a Pixyz concept representing the ID of an element). The Run method is executed using the context on which the action is placed — for example, your selection or the object imported if the rule is applied via a Ruleset.
In a Ruleset, each action is executed sequentially, passing the output of one action as the input for the next.

Behind the scenes, the Pixyz plugin converts Unity data into Pixyz’s internal data structure and synchronizes it back to Unity once the process is complete. PixyzAction abstract class include properties you can override to control the synchronization process. However, this example is straightforward, and those additional adjustments won’t significantly impact performance, so I’ll save that for a future tutorial.

Instead of iterating through all the input Occurrences and call resetTransform on them. We will retrieve the root of the Pixyz context, and call this function with the recursive parameter set to True. That way, we let Pixyz handle the iterative process for us. As Pixyz is vastly multithreaded, it will perform faster than a naive C# loop.
Your BakeTransformation script should look like this:

#if PIXYZ_PLUGIN_FOR_UNITY
using UnityEngine;
using UnityEditor.PixyzPlugin4Unity.Actions;
using UnityEngine.Pixyz.Scene;

// Ensure your class name matches your file name
public class BakeTransformation : PixyzAction
{
    public override int Id => 27854066;
    public override string MenuPathRuleEngine => "Custom/BakeTransformation";
    public override string MenuPathToolbox => "Custom/BakeTransformation";
    public override string Tooltip => "";
    public override string Icon => null;
    public override int Priority => 15001;

    protected override OccurrenceList Run(OccurrenceList input)
    {
        uint root = Scene.GetRoot();
        Scene.ResetTransform(root , true, true, true);
        return input;
    }
}
#endif

3. Test Your Custom Action:
You can now call your new custom action either from the toolbar or by adding it to the Import stage of your project (in this case, the boat).


That’s it! Your Pixyz Custom Action is ready to use.

Let us know if you have any questions or feedback. Happy coding! :rocket:

More info here: Actions | Asset Transformer Toolkit ex Pixyz | 3.1.2

6 Likes

Thanks for the detailed walkthrough! This solution using resetTransform via a custom PixyzAction makes a lot of sense.

Quick question:
If we wanted to apply resetTransform only to specific nodes (rather than recursively from the root), is there a recommended way to filter or select specific Occurrences within the Run method before calling the transformation? Would that impact performance significantly compared to the recursive approach from the root?

Thanks in advance!

Hi,
The root, is not correlated to the scene node. It’s the root of the execution context. So in case you are running this action from the toolbar. It will only apply on the GameObjects selected in your scene.
If you are using this Action in a ruleset. Then you can use any Filter action before, to keep the wanted nodes.

This subtelty of the content of “root” mainly comes from the fact that we do not convert all the unity scene to a Pixyz scene. We only transfert what is needed to speed up the conversion process for the action. The “what is needed” is called the context, and can ne either: the whole scene, a prefab, a list of selected gameObjects, an imported model, or the output of a previous action.