Skip to content

Commit 39847c7

Browse files
committed
Improve importer
Added EditorScenePostImportPlugin which makes it possible to set subdivision options in any scene importer. Also renamed to TopologyDataImporter since now usable with any mesh and file format and not just gltf and quads.
1 parent f3b03e6 commit 39847c7

12 files changed

+249
-257
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
### Importing
1010

11-
After installing the addon and enabling it in the settings you can select the custom **Godot Subdiv Importer** in the import settings for any glb or gltf file.
11+
After installing the addon and enabling it in the settings you can select the custom **Godot Subdiv Importer** in the import settings for any glb or gltf file. You can also use the **subdivision settings in any scene importer** to be able to make use of all the features of the 3d import pipeline from Godot.
1212
There you will see three options:
1313

1414
| SubdivMeshInstance3D | BakedSubdivMesh | ArrayMesh |
Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
@tool
22
extends EditorPlugin
33

4-
var import_plugin
4+
var resource_import_plugin
5+
var editor_scene_import_modifier
56

67
func _enter_tree():
7-
import_plugin=preload("res://addons/godot_subdiv/quad_import_plugin.gd").new()
8-
add_import_plugin(import_plugin)
9-
8+
#direct import plugin for gltf, comment out if you don't want the custom Godot Subdiv Importer
9+
resource_import_plugin=preload("res://addons/godot_subdiv/resource_import_plugin.gd").new()
10+
add_import_plugin(resource_import_plugin)
11+
12+
#This adds custom options to scene importer, remove if you don't want subdivision options for every mesh
13+
editor_scene_import_modifier=preload("res://addons/godot_subdiv/scene_import_modifier_plugin.gd").new()
14+
add_scene_post_import_plugin(editor_scene_import_modifier, true)
1015

1116
func _exit_tree():
12-
remove_import_plugin(import_plugin)
13-
import_plugin = null
17+
remove_import_plugin(resource_import_plugin)
18+
resource_import_plugin = null
19+
20+
remove_scene_post_import_plugin(editor_scene_import_modifier)
21+
editor_scene_import_modifier=null

project/addons/godot_subdiv/plugin.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
name="Godot Subdiv"
44
description="Fast subdivision in Godot with opensubdiv."
55
author="tefusion"
6-
version="0.3"
6+
version="0.3.1"
77
script="godot_subdiv.gd"

project/addons/godot_subdiv/quad_converter_post_import.gd

Lines changed: 0 additions & 17 deletions
This file was deleted.

project/addons/godot_subdiv/quad_import_plugin.gd

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# import_plugin.gd
2+
# This plugin adds the custom godot-subdiv.importer and will not change the scene importer at all.
3+
@tool
4+
extends EditorImportPlugin
5+
6+
7+
func _get_importer_name():
8+
return "godot-subdiv.importer"
9+
10+
func _get_visible_name():
11+
return "Godot Subdiv Importer"
12+
13+
func _get_recognized_extensions():
14+
return ["glb", "gltf"]
15+
16+
func _get_save_extension():
17+
return "scn"
18+
19+
func _get_resource_type():
20+
return "PackedScene"
21+
22+
func _get_preset_count():
23+
return 1
24+
25+
func _get_preset_name(i):
26+
return "Default"
27+
28+
func _get_priority():
29+
return 2
30+
31+
func _get_import_order():
32+
return IMPORT_ORDER_SCENE
33+
34+
func _get_option_visibility(path, option_name, options):
35+
return true
36+
37+
func _get_import_options(path, preset_index):
38+
var options=[
39+
{
40+
"name": "import_as",
41+
"property_hint": PROPERTY_HINT_ENUM,
42+
"default_value": "SubdivMeshInstance3D",
43+
"hint_string": "SubdivMeshInstance3D,BakedSubdivMesh (bake at runtime),ArrayMesh (bake at import)"
44+
},
45+
{
46+
"name": "sudbivision_level",
47+
"default_value": 0,
48+
"property_hint": PROPERTY_HINT_RANGE,
49+
"hint_string": "0,6"
50+
}
51+
]
52+
return options
53+
54+
func _import(source_file, save_path, options, platform_variants, gen_files):
55+
var gltf := GLTFDocument.new()
56+
var gltf_state := GLTFState.new()
57+
gltf.append_from_file(source_file, gltf_state, 0, 0)
58+
var node=gltf.generate_scene(gltf_state)
59+
#set root name, otherwise throws name is empty
60+
node.name=source_file.get_file().get_basename()
61+
62+
var subdiv_level=options["sudbivision_level"]
63+
64+
var subdiv_converter=preload("res://addons/godot_subdiv/subdiv_converter.gd").new(options["import_as"], subdiv_level)
65+
66+
if node!=null:
67+
subdiv_converter.convert_importer_mesh_instances_recursively(node)
68+
else:
69+
print("GLTF importer failed, so could not run convert code")
70+
71+
var packed=PackedScene.new()
72+
packed.pack(node)
73+
node.free()
74+
75+
ResourceSaver.save(packed, save_path+".scn")
76+
77+
78+
79+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@tool
2+
extends EditorScenePostImportPlugin
3+
4+
func _get_import_options(path: String)->void:
5+
add_import_option_advanced(TYPE_STRING,
6+
"subdivision/import_as",
7+
"ImporterMesh (bake at import)",
8+
PROPERTY_HINT_ENUM,
9+
"SubdivMeshInstance3D,BakedSubdivMesh (bake at runtime),ImporterMesh (bake at import)")
10+
11+
add_import_option_advanced(TYPE_INT,
12+
"subdivision/subdivision_level",
13+
0,
14+
PROPERTY_HINT_RANGE,
15+
"0,6")
16+
17+
func _pre_process(scene: Node):
18+
var subdiv_import_option=get_option_value("subdivision/import_as")
19+
var subdiv_level=get_option_value("subdivision/subdivision_level")
20+
var subdiv_converter=preload("res://addons/godot_subdiv/subdiv_converter.gd").new(subdiv_import_option, subdiv_level)
21+
if scene!=null:
22+
subdiv_converter.convert_importer_mesh_instances_recursively(scene)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
var importer: TopologyDataImporter
3+
var import_mode: int
4+
var subdiv_level: int
5+
6+
func _init(import_mode_string: String, p_subdiv_level: int):
7+
self.importer=TopologyDataImporter.new()
8+
self.import_mode=_convert_string_to_import_mode(import_mode_string)
9+
self.subdiv_level=p_subdiv_level
10+
11+
func _convert_string_to_import_mode(enum_string: String) -> int:
12+
match enum_string:
13+
"SubdivMeshInstance3D":
14+
return TopologyDataImporter.SUBDIV_MESHINSTANCE
15+
"BakedSubdivMesh (bake at runtime)":
16+
return TopologyDataImporter.BAKED_SUBDIV_MESH
17+
"ArrayMesh (bake at import)":
18+
return TopologyDataImporter.ARRAY_MESH
19+
"ImporterMesh (bake at import)":
20+
return TopologyDataImporter.IMPORTER_MESH
21+
_:
22+
return -1
23+
24+
func convert_importer_mesh_instances_recursively(node: Node):
25+
for i in node.get_children():
26+
convert_importer_mesh_instances_recursively(i)
27+
if i is ImporterMeshInstance3D:
28+
importer.convert_importer_meshinstance_to_subdiv(i, import_mode, subdiv_level)
29+
elif i is AnimationPlayer:
30+
if import_mode==TopologyDataImporter.SUBDIV_MESHINSTANCE:
31+
convert_animation_blend_shape_tracks(i)
32+
33+
#blend shape tracks don't work with SubdivMeshInstance3D
34+
func convert_animation_blend_shape_tracks(anim_player: AnimationPlayer):
35+
for animation_library_name in anim_player.get_animation_library_list():
36+
var anim_lib:AnimationLibrary=anim_player.get_animation_library(animation_library_name)
37+
for animation_name in anim_lib.get_animation_list():
38+
var animation: Animation=anim_lib.get_animation(animation_name)
39+
for track_idx in range(0, animation.get_track_count()):
40+
if animation.track_get_type(track_idx)==Animation.TYPE_BLEND_SHAPE:
41+
var fake_anim=animation.add_track(Animation.TYPE_VALUE)
42+
var old_track_path=animation.track_get_path(track_idx)
43+
var node_name=String(old_track_path.get_name(0))
44+
var blend_shape_name=String(old_track_path.get_subname(0))
45+
var new_track_path=node_name+":"+"blend_shapes/"+blend_shape_name
46+
animation.track_set_path(fake_anim, NodePath(new_track_path))
47+
48+
for key_idx in range(0, animation.track_get_key_count(track_idx)):
49+
var val=animation.track_get_key_value(track_idx, key_idx)
50+
var time=animation.track_get_key_time(track_idx, key_idx)
51+
var transition=animation.track_get_key_transition(track_idx, key_idx)
52+
animation.track_insert_key(fake_anim, time, val, transition)
53+
animation.track_swap(track_idx, fake_anim)
54+
animation.remove_track(fake_anim)

project/demo/AnimatedMorphCube.glb.import

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[remap]
22

3-
importer="godot-subdiv.importer"
3+
importer="scene"
4+
importer_version=1
45
type="PackedScene"
56
uid="uid://bvl48vdcumgtb"
67
path="res://.godot/imported/AnimatedMorphCube.glb-6c280e5795b13de9b6e86a05158490f6.scn"
@@ -12,5 +13,19 @@ dest_files=["res://.godot/imported/AnimatedMorphCube.glb-6c280e5795b13de9b6e86a0
1213

1314
[params]
1415

15-
import_as="SubdivMeshInstance3D"
16-
sudbivision_level=2
16+
nodes/root_type="Node3D"
17+
nodes/root_name="Scene Root"
18+
nodes/apply_root_scale=true
19+
nodes/root_scale=1.0
20+
meshes/ensure_tangents=true
21+
meshes/generate_lods=true
22+
meshes/create_shadow_meshes=true
23+
meshes/light_baking=1
24+
meshes/lightmap_texel_size=0.2
25+
skins/use_named_skins=true
26+
animation/import=true
27+
animation/fps=30
28+
import_script/path=""
29+
_subresources={}
30+
subdivision/import_as="SubdivMeshInstance3D"
31+
subdivision/subdivision_level=3

0 commit comments

Comments
 (0)