KhronosGroup / glTF-Blender-IO

Blender glTF 2.0 importer and exporter
https://docs.blender.org/manual/en/latest/addons/import_export/scene_gltf2.html
Apache License 2.0
1.45k stars 306 forks source link

constant interpolation ignored in export, scale to zero introduces frame with weird negative scale #2236

Closed TimKraemer closed 1 week ago

TimKraemer commented 1 month ago

Describe the bug Whenever I try to export this example scene (a cube with a linear translation animation and a constant scale animation from 1,1,1 to 0,0,0 at the end) the exporter is giving me two LINEAR interpolations:

{
    "asset":{
        "generator":"Khronos glTF Blender I/O v4.1.63",
        "version":"2.0"
    },
...
    "animations":[
        {
            "channels":[
                {
                    "sampler":0,
                    "target":{
                        "node":0,
                        "path":"translation"
                    }
                },
                {
                    "sampler":1,
                    "target":{
                        "node":0,
                        "path":"scale"
                    }
                }
            ],
            "name":"CubeAction",
            "samplers":[
                {
                    "input":4,
                    "interpolation":"LINEAR",
                    "output":5
                },
                {
                    "input":4,
                    "interpolation":"LINEAR",
                    "output":6
                }
            ]
        }
    ],
...
}

when I then manually change the second one to a STEP interpolation, it gives me a weird scale for one frame with x: -1.083, y: -1.083, z: -1.083 before it scales to zero.

To Reproduce

Steps to reproduce the behavior:

  1. Download the example blender file
  2. Export as gltf2.0 with Operator Presets (optional export als gltf to see the source)

Expected behavior Ideally linear interpolation should get exported as "interpolation":"LINEAR", while constant interpolation get's exported as "interpolation":"STEP" independent from each other. Since it's easy to write a script to do this manually, the second problem, that one frame leads to a weird scale of -1.083 should be prevented completely.

.blend file/ .gltf (mandatory) Example blender file and output gltf with already modified STEP interpolation: simple_test.zip

Version

Additional context related: #2137

TimKraemer commented 1 month ago

in case someone is interested to replace "interpolation":"LINEAR" with "interpolation":"STEP" for every scale animation, I'm using this python script to do so:

import json
import sys

def modify_gltf(input_path, output_path):
    # Load the GLTF file
    with open(input_path, 'r') as file:
        gltf = json.load(file)

    # Iterate over the animations
    for animation in gltf.get('animations', []):
        for channel in animation.get('channels', []):
            target = channel.get('target', {})
            # Check if the target path is "scale"
            if target.get('path') == 'scale':
                sampler_index = channel.get('sampler')
                if sampler_index is not None:
                    sampler = animation.get('samplers', [])[sampler_index]
                    # Change the interpolation to "STEP"
                    if sampler:
                        sampler['interpolation'] = 'STEP'

    # Save the modified GLTF file
    with open(output_path, 'w') as file:
        json.dump(gltf, file, indent=2)

    print(f"GLTF file has been modified and saved as '{output_path}'")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python modify_gltf.py <input_path> <output_path>")
    else:
        input_path = sys.argv[1]
        output_path = sys.argv[2]
        modify_gltf(input_path, output_path)
julienduroure commented 1 month ago

Hello,

julienduroure commented 1 week ago

More than a month without any answer, closing this ticket. Feel free to comment and provide the .blend file if you want me to have a look