Godot_bridge/addon/operators/export2godot.py
2023-03-29 12:43:53 +02:00

113 lines
3.8 KiB
Python

import bpy
import os
import re
from ..common import addon
from ..common import validate_name
class KH_OT_export2godot(bpy.types.Operator):
"""Export whole collections to Godot throught glTF format"""
bl_idname = "kh.export2godot"
bl_label = "Export to Godot project"
bl_options = {'REGISTER', 'UNDO'}
def invoke(self, context, event):
return self.execute(context)
def execute(self, context):
prefs = addon.get_prefs()
print("Root collection : {}".format(prefs.root_collection))
print("Godot path : {}".format(prefs.godot_project_path))
print("Blender repository path : {}".format(
prefs.blender_repository_path))
def save_file_beforehand():
self.report(
{"WARNING"}, "File must be saved first - skipping export")
bpy.ops.wm.save_mainfile('INVOKE_AREA')
return {"CANCELLED"}
def create_destination_folder():
"""
Define the destination folder and creates it if nonexistent.
Return the path name
"""
final_path = bpy.data.filepath.replace(
prefs.blender_repository_path, prefs.godot_project_path)
gltf_path = os.path.splitext(final_path)[0]
if not os.path.isdir(gltf_path):
os.makedirs(gltf_path)
return gltf_path
def recurLayerCollection(layerColl, collName):
""" Activate the selected collection for export"""
found = None
if (layerColl.name == collName):
return layerColl
for layer in layerColl.children:
found = recurLayerCollection(layer, collName)
if found:
return found
def export_content(scene_collection):
def check_name(collection):
is_valid = validate_name.validate_name(collection.name)
if not is_valid:
self.report({"WARNING"},
"Name {} is not valid - skipping export".format(collection.name))
return {"CANCELLED"}
def export_to_file(tscn_collection):
print("Exporting {}".format(tscn_collection.name))
filename = tscn_collection.name
# Export collection - parameters : https://docs.blender.org/api/current/bpy.ops.export_scene.html?highlight=export_scene#bpy.ops.export_scene.gltf
print("Exporting to : {}/{}".format(gltf_path, filename))
print("Exporting textures to : {}_{}_textures".format((os.path.splitext(os.path.basename(gltf_path)))[0], filename))
bpy.ops.export_scene.gltf(
filepath="{}/{}".format(gltf_path, filename),
export_format="GLTF_SEPARATE", # Export glTF Separate (.gltf + .bin + textures), Exports multiple files, with separate JSON, binary and texture data
export_texture_dir="{}_{}_textures".format((os.path.splitext(os.path.basename(gltf_path)))[0], filename), # Textures folder
export_copyright=prefs.licence,
use_active_collection = True,
use_renderable = True,
export_cameras=False,
export_lights=False,
export_apply=True # Applique les modifiers
)
tscn_collections = [ coll for coll in bpy.data.collections if scene_collection.user_of_id(coll)]
for tscn in tscn_collections:
print("--------------------")
check_name(tscn)
# Activate proper collection for export
layer_collection = bpy.context.view_layer.layer_collection
layerColl = recurLayerCollection(layer_collection, tscn.name)
if layerColl.exclude:
print("{} is not activated - not exported".format(tscn.name))
else:
print("Set active collection to {}".format(tscn.name))
bpy.context.view_layer.active_layer_collection = layerColl
export_to_file(tscn)
if not bpy.data.is_saved:
save_file_beforehand()
else:
try:
scn_col = bpy.data.collections[prefs.root_collection]
except KeyError:
self.report({"WARNING"},
"No \"{}\" root collection in the file - skipping export".format(prefs.root_collection))
return {"CANCELLED"}
# Create the proper destination path
gltf_path = create_destination_folder()
export_content(scn_col)
return {"FINISHED"}