102 lines
3 KiB
Python
102 lines
3 KiB
Python
|
|
import bpy
|
|
from bpy.types import Object
|
|
from pathlib import Path
|
|
from contextlib import contextmanager
|
|
|
|
if True:
|
|
blend_folder = Path(bpy.path.abspath("//"))
|
|
while blend_folder.resolve().stem not in ("", "blends"):
|
|
blend_folder /= ".."
|
|
EXPORT_PATH = str((blend_folder / "../fox.glb").resolve())
|
|
EXPORT_ANIMATIONS_PATH = str(
|
|
(blend_folder / "../fox_animations.glb").resolve())
|
|
import sys
|
|
sys.path.append(str(blend_folder.resolve()))
|
|
from shared_export import clear_selection, apply_modifier, sort_uv_layers, apply_bone_groups, select_collection, reverted, delete_uv_layer, apply_gn_attr
|
|
|
|
|
|
def prepare_modifiers(fox: Object, foxrig: Object):
|
|
geo = fox.modifiers["Fur"]
|
|
geo.show_viewport = True
|
|
apply_modifier(fox, geo)
|
|
apply_bone_groups(foxrig.data, fox)
|
|
|
|
|
|
def gltf_export(filename: str):
|
|
bpy.context.view_layer.update()
|
|
bpy.ops.export_scene.gltf(
|
|
filepath=filename,
|
|
export_format="GLB",
|
|
use_selection=True,
|
|
export_vertex_color="ACTIVE",
|
|
export_apply=False,
|
|
export_animation_mode="ACTIONS",
|
|
export_anim_slide_to_zero=True,
|
|
export_optimize_animation_size=False,
|
|
export_optimize_animation_keep_anim_armature=False,
|
|
export_def_bones=True,
|
|
export_animations=True,
|
|
)
|
|
|
|
|
|
@reverted
|
|
def export():
|
|
fox = bpy.data.objects["Fox"]
|
|
foxrig = bpy.data.objects["FoxRig"]
|
|
collection = bpy.data.collections["export"]
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.context.view_layer.update()
|
|
clear_selection(bpy.context)
|
|
select_collection(bpy.context, collection)
|
|
prepare_modifiers(fox, foxrig)
|
|
sort_uv_layers(fox.data)
|
|
# write_vertex_id_to_uv(fox)
|
|
gltf_export(EXPORT_PATH)
|
|
|
|
|
|
def vertex_id_mesh_info(helper):
|
|
return helper.VertexIDMeshInfo(
|
|
resource_path="res://fox.tscn",
|
|
node_path="FoxRig/Skeleton3D/Fox",
|
|
)
|
|
|
|
|
|
def write_vertex_id_to_uv(obj: Object):
|
|
import bmesh
|
|
mesh = obj.data
|
|
mesh.uv_layers.active = mesh.uv_layers[1]
|
|
|
|
bm = bmesh.new()
|
|
bm.from_mesh(mesh)
|
|
uv_layer = bm.loops.layers.uv.active
|
|
for face in bm.faces:
|
|
for loop in face.loops:
|
|
loop[uv_layer].uv.x = loop.vert.index
|
|
bm.to_mesh(obj.data)
|
|
|
|
|
|
@reverted
|
|
def export_animations():
|
|
mesh = bpy.data.objects["Fox"]
|
|
rig = bpy.data.objects["FoxRig"]
|
|
bpy.ops.object.mode_set(mode="OBJECT")
|
|
bpy.context.view_layer.update()
|
|
clear_selection(bpy.context)
|
|
rig.select_set(True)
|
|
mesh.select_set(True)
|
|
mesh.modifiers["Fur"].show_viewport = False
|
|
|
|
rig.data.pose_position = "POSE"
|
|
bpy.context.view_layer.update()
|
|
bpy.ops.export_scene.gltf(
|
|
filepath=EXPORT_ANIMATIONS_PATH,
|
|
export_format="GLB",
|
|
use_selection=True,
|
|
export_apply=False,
|
|
export_animation_mode="ACTIONS",
|
|
export_anim_slide_to_zero=True,
|
|
export_optimize_animation_size=False,
|
|
export_optimize_animation_keep_anim_armature=False,
|
|
export_def_bones=True,
|
|
)
|