Created
August 1, 2023 18:44
-
-
Save eviltrout/c33ed2d0d20089d392d7e6661a36e174 to your computer and use it in GitHub Desktop.
Blender Script for Automatically Copying files to a Godot Game Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This accompanies my video here: https://www.youtube.com/watch?v=v23xZHJdtLE | |
# Mainly meant for learning purposes not for something to use unless you really know what | |
# you're doing. Licensed as MIT. | |
import bpy | |
import pathlib | |
import os | |
SKELLIG_MESH_DIR = "C:\\YOUR_GAME_PATH\\meshes\\" | |
ASSET_PATH_SEGMENT = "blender\\" | |
VALID_TYPES = [ | |
"characters", | |
"levels" | |
] | |
class SkelligExportScene(bpy.types.Operator): | |
"""Tooltip""" | |
bl_idname = "object.skellig_export" | |
bl_label = "Skellig Export Scene" | |
@classmethod | |
def poll(cls, context): | |
return context.active_object is not None | |
def execute(self, context): | |
self.export(context) | |
return {'FINISHED'} | |
def export(self, context): | |
path = bpy.path.abspath("//") | |
# Make sure it has skellig-assets\blender in there | |
assets_path_idx = path.find(ASSET_PATH_SEGMENT) | |
if assets_path_idx == -1: | |
self.report({"ERROR"}, "Can't run skellig export from this path") | |
return | |
mesh_type = path[assets_path_idx + len(ASSET_PATH_SEGMENT):-1] | |
export_dir = SKELLIG_MESH_DIR + mesh_type | |
if not os.path.exists(export_dir): | |
self.report({"ERROR"}, export_dir + " does not exist") | |
return | |
if not any([x for x in VALID_TYPES if mesh_type.startswith(x)]): | |
self.report({"ERROR"}, "Don't know how to export " + mesh_type) | |
return | |
filename = pathlib.Path(bpy.data.filepath).stem | |
export_path = export_dir + "\\" + filename + ".glb" | |
shown = [] | |
placeholders = [] | |
for ob in context.scene.objects: | |
if ob.name.endswith("-colonly") and not(ob.visible_get()): | |
ob.hide_set(False) | |
shown.append(ob) | |
if ob.name.endswith("-placeholder") and ob.visible_get(): | |
ob.hide_set(True) | |
placeholders.append(ob) | |
result = bpy.ops.export_scene.gltf( | |
filepath=export_path, | |
export_materials="EXPORT", | |
export_image_format="NONE", | |
use_visible=True, | |
export_apply=True | |
) | |
for ob in placeholders: | |
ob.hide_set(False) | |
for ob in shown: | |
ob.hide_set(True) | |
self.report({"INFO"}, "Exported " + filename) | |
def menu_func(self, context): | |
self.layout.operator(SkelligExportScene.bl_idname, text=SkelligExportScene.bl_label) | |
def register(): | |
bpy.utils.register_class(SkelligExportScene) | |
bpy.types.VIEW3D_MT_object.append(menu_func) | |
def unregister(): | |
bpy.utils.unregister_class(SkelligExportScene) | |
bpy.types.VIEW3D_MT_object.remove(menu_func) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment