poly-hammer / BlenderTools

Blender addons that improve the game development workflow between Blender and Unreal.
https://poly-hammer.github.io/BlenderTools/
MIT License
234 stars 15 forks source link

Send2UE - Runtime Manipulation of FBX Exporter #57

Closed JoshQuake closed 2 months ago

JoshQuake commented 3 months ago

Required change for Extensions compliance: https://extensions.blender.org/approval-queue/send2ue/#activity-2139

Run-time manipulating the existing FBX exporter isn't good practice.) Ideally fixes could be submitted up-stream.

At a minimum the state of the FBX exporter must be restored once the export has completed.
JoshQuake commented 3 months ago

https://github.com/poly-hammer/BlenderTools/blob/1ed59eda9219485f0eded3b91b88422b771f0144/src/addons/send2ue/core/io/fbx_b3.py#L564

Are these not doing exactly that? 🤔

JoshQuake commented 3 months ago

Added clarification:

@Joshimals ah, my mistake then. Any error in the patched code will fail to restore the previous state.

A temporary context can be used to handle temporary overrides that survive exceptions.

Suggest to split up export and the override into two functions to avoid the override logic being mixed in with setup/teardown. You may want to use contextlib.contextmanager which makes this convenient.

jack-yao91 commented 3 months ago

I think doing a

try:
    self = type(
        'Send2UeExportFBX',
        (object,),
        {'report': report_error}
    )
    export_fbx_bin.save(self, bpy.context, **keywords)
finally:
    export_fbx_bin.fbx_animations_do = original_fbx_animations_do
    export_fbx_bin.fbx_data_armature_elements = original_fbx_data_armature_elements
    export_fbx_bin.fbx_data_object_elements = original_fbx_data_object_elements
    export_fbx_bin.fbx_data_bindpose_element = original_fbx_data_bindpose_element

Would achieve this and ensure it is always restored