question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

User extensions: empty collections and null

See original GitHub issue

Any key-value pair whose value is {}, [], or None gets scrubbed out of the glTF data when exporting. That means a user-extension can’t do things like this:

    "extensions": { "KHR_materials_unlit": {} }
    // gets turned into "extensions": {}

    "extensions": { "my_ext": { "foo": [] } }
    // gets turned into "extensions": { "my_ext": {} }

The way the in-tree code works around this is to use a hard-coded whitelist of key-names that shouldn’t be scrubbed.

https://github.com/KhronosGroup/glTF-Blender-IO/blob/3ab37ba38a3ae483d69a029f979286ded8b9b94b/addons/io_scene_gltf2/blender/exp/gltf2_blender_export.py#L108-L115

That’s not very flexible and it doesn’t help user extensions anyway. Here’s my proposed fix:

  • Add some new objects you should use wherever you want an empty collection/null

    EmptyDict = object()  # use this where you want an empty dict
    EmptyList = object()  # use this where you want an empty list
    Null = object()  # use this where you want a null
    
  • Make from_extension believe that EmptyDict is a dict so it will allow it in the "extensions" object.

  • Change __fix_json to convert EmptyDict into {} etc. Get rid of the whitelist.

  • Wherever you want to put an empty dict, just use EmptyDict instead. Example:

    extensions['KHR_materials_unlit'] = Extension(
        'KHR_materials_unlit',
        EmptyDict,
        required=False,
    )
    

What’s everyone think?

cc @jjcasmar

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
scurestcommented, Jan 10, 2020

They just do it the same way they import everything else

class glTF2ExportUserExtension:
    def __init__(self):
        import io_scene_gltf2
        self.Extension = io_scene_gltf2.io.com.gltf2_io_extensions.Extension
        self.EmptyDict = io_scene_gltf2.io.com.gltf2_io.EmptyDict
        
    def gather_material_hook(self, material, blender_material, export_settings):
        if material.extensions is None:
            material.extensions = {}
        material.extensions['KHR_materials_unlit'] = self.Extension(
            name='KHR_materials_unlit',
            extension=self.EmptyDict,
            required=False,
        )
1reaction
jjcasmarcommented, Jan 10, 2020

I agree. I have the same doubts when I first looks at the __fix_json. I think your solution is a good one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ways of handling null collections and AddRange
I could use Enumerable.Empty() which will only allocate a new array the first time around, but that just isn't as neat and tidy....
Read more >
How to create an Extension method for a list to return null if ...
I have multiple lists of different objects. for all objects I want to return null if it's an empty list or ...
Read more >
How to Implement Lazy Default-If-Empty Functionality on ...
Solution to the problem is to provide custom implementation for the DefaultIfEmpty extension method, which receives a lambda instead of actual ...
Read more >
Empty arrays and collections should be returned instead of ...
In-IDE. Code Quality and Security in your IDE with SonarLint. IDE extension that lets you fix coding issues before they exist! Discover SonarLint ......
Read more >
null - JavaScript - MDN Web Docs - Mozilla
The null value represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found