Before exporting to FBX I modify the object's name, location, parent, etc.
All the exporting happens in a for loop of selected objects.
So I need to restore it after every export.
Example script:
import bpy
from bpy import context
import os
import random
filepath = "C:/Users/karan/Desktop/"
override = context.copy()
for object in context.selected_objects:
# It's a simple modification, but I will have some complex modifications.
object.location = (random.random(), random.random(), random.random())
# export
override['selected_objects'] = [object]
with context.temp_override(**override):
bpy.ops.export_scene.fbx(filepath=os.path.join(filepath, f"{object.name}.fbx"), check_existing=False, use_selection=True)
# restore
bpy.ops.wm.revert_mainfile()
I get this error
ReferenceError: StructRNA of type Object has been removed
How to fix this?
And how to add a progress bar while exporting?
import bpy
import os
import random
class EXPORT_OT_export_fbx(bpy.types.Operator):
"""Tooltip"""
bl_idname = "export.fbx"
bl_label = "Export FBX"
def execute(self, context):
filepath = "C:/Users/karan/Desktop/"
names = [obj.name for obj in context.selected_objects]
for name in names:
object = bpy.data.objects[name]
object.location = (random.random(), random.random(), random.random())
override = bpy.context.copy()
override['selected_objects'] = [object]
with context.temp_override(**override):
bpy.ops.export_scene.fbx(filepath=os.path.join(filepath, f"{object.name}.fbx"), use_selection=True)
# restore
bpy.ops.wm.revert_mainfile()
return {'FINISHED'}
class EXPORT_PT_export_fbx(bpy.types.Panel):
bl_label = "Export"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Export"
def draw(self, context):
layout = self.layout
if context.scene.export_progress >= 0:
layout.enabled = False
layout.prop(context.scene, 'export_progress', slider=True)
else:
layout.operator("export.fbx")
def register():
bpy.utils.register_class(EXPORT_OT_export_fbx)
bpy.utils.register_class(EXPORT_PT_export_fbx)
bpy.types.Scene.export_progress = bpy.props.IntProperty(name = 'Exporting...', subtype = 'PERCENTAGE', min = -1, soft_min = 0, soft_max = 100, max = 100, default = -1)
def unregister():
bpy.utils.unregister_class(EXPORT_OT_export_fbx)
bpy.utils.unregister_class(EXPORT_PT_export_fbx)
del bpy.types.Scene.export_progress
if name == "main":
register()


object = bpy.data.objects.get('Cube')in the forloop to fix that.overridealso need. – X Y Jul 01 '23 at 03:18context.selected_objects, I updated my question. – Karan Jul 01 '23 at 03:31The panel method may not work because you restore the file every time. – X Y Jul 01 '23 at 05:21
blfmethod. – Karan Jul 01 '23 at 05:43