Skip to content

Instantly share code, notes, and snippets.

@pfeodrippe
Created April 17, 2024 02:22
Show Gist options
  • Save pfeodrippe/0578484bcd9280399408417b1e236220 to your computer and use it in GitHub Desktop.
Save pfeodrippe/0578484bcd9280399408417b1e236220 to your computer and use it in GitHub Desktop.
import bpy
from bpy.app.handlers import persistent
import os
# This method will select everything, apply all the transforms,
# export and then undo both (transform apply and select all).
@persistent
def VY__export_models(file):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.ed.undo_push(message = "SELECT ALL - SCRIPT")
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
bpy.ops.ed.undo_push(message = "APPLY EVERYTHING - SCRIPT!")
# https://docs.blender.org/api/current/bpy.ops.export_scene.html#bpy.ops.export_scene.gltf
bpy.ops.export_scene.gltf(
filepath=bpy.path.abspath("//models.glb"),
export_format='GLB',
use_active_collection=False,
export_apply=True,
use_visible=True,
)
bpy.ops.ed.undo()
bpy.ops.ed.undo()
# A list containing our methods for the save_post handler.
save_post_operators= [
VY__export_models,
]
# List containing the method names.
save_post_operators_names = [v.__name__ for v in save_post_operators]
# Remove existing handler matching the methods (if any) so we don't have
# duplications.
for f in bpy.app.handlers.save_post:
if f.__name__ in save_post_operators_names:
bpy.app.handlers.save_post.remove(f)
# Then add the operators
# https://docs.blender.org/api/current/bpy.app.handlers.html
for f in save_post_operators:
bpy.app.handlers.save_post.append(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment