Godex importer - reeseschultz/godex GitHub Wiki

When working with Godex, you may want your level uses the entities. Using the following script, it's possible to import a map and use the Godex Entities.

To do that, you have to set the below script as Post Import Script, as shown here:

Screenshot from 2021-04-15 19-11-16

This is the result:

Screenshot from 2021-04-15 18-48-40

## License MIT

@tool
extends EditorScenePostImport


func post_import(scene):
	## Process the scene
	var godex_version = convert_scene_to_godex(scene)
	return godex_version


func convert_scene_to_godex(scene_root):
	if scene_root == null:
		return

	var godex_scene_root = null

	if scene_root is Node2D:
		godex_scene_root = Entity2D.new()
	else:
		godex_scene_root = Entity3D.new()

	var root = godex_scene_root
	convert_to_godex(scene_root, godex_scene_root, root)

	return godex_scene_root


func convert_to_godex(node, godex_node, root):
	if node == null or godex_node == null:
		return

	# Convert the child to Godex
	for child in node.get_children():
		var gchild = null
		if child is Node2D:
			gchild = Entity2D.new()
		elif child is CollisionShape3D:
			# The shapes are converted when the StaticBody3D is detected, nothing more to do at this point
			continue
		else:
			gchild = Entity3D.new()

		godex_node.add_child(gchild)
		gchild.set_owner(root)
		convert_to_godex(child, gchild, root)

	# Now convert this node to Godex
	# Set the name
	godex_node.name = node.name
	# Add the child, so the Hierarchy transformation is resolved
	godex_node.add_component("Child")

	if node is Node3D:
		# Set transform
		godex_node.add_component("TransformComponent", {"transform": node.get_transform()})

		if node is MeshInstance3D:
			# Set the mesh
			godex_node.add_component("MeshComponent", {"mesh": node.mesh})
		elif node is StaticBody3D:
			godex_node.add_component("BtRigidBody", {"body_mode": 3}) # 3 is Static mode

			# Search the CollisionShape from the child and add the shape component accordingly.
			for i in range(node.get_child_count()):
				var c = node.get_child(i)
				if c is CollisionShape3D:
					if c.shape is BoxShape3D:
						var shared_res := SharedComponentResource.new()
						shared_res.init("BtBox")
						shared_res.set("half_extents", c.shape.size * 0.5)
						godex_node.add_component("BtBox", {"resource": shared_res})
					elif c.shape is ConvexPolygonShape3D:
						var shared_res := SharedComponentResource.new()
						shared_res.init("BtConvex")
						shared_res.set("points", c.shape.points)
						godex_node.add_component("BtConvex", {"resource": shared_res})
					elif c.shape is ConcavePolygonShape3D:
						var shared_res := SharedComponentResource.new()
						shared_res.init("BtTrimesh")
						shared_res.set("faces", c.shape.get_faces())
						godex_node.add_component("BtTrimesh", {"resource": shared_res})
					else:
						print("[ERROR][GodexImporter] This shape type is not supported: " + c.shape.get_class())
				
					# Shape found, nothing more to search
					break

Convert just RigidBody3D

If you want to convert only RigidBody3D to godex, you can use this instead:

## License MIT

@tool
extends EditorScenePostImport


var materials: Dictionary = {}


func _post_import(scene):
	## Process the scene
	var godex_version = convert_scene_to_godex(scene)
	return godex_version


func convert_scene_to_godex(scene_root):
	if scene_root == null:
		return

	var godex_scene_root = null

	if scene_root is Node2D:
		godex_scene_root = Node2D.new()
	else:
		godex_scene_root = Node3D.new()

	var root = godex_scene_root
	convert_to_godex(scene_root, godex_scene_root, root)

	return godex_scene_root


func convert_to_godex(node, godex_node, root):
	if node == null or godex_node == null:
		return

	# Convert the child to Godex
	for child in node.get_children():
		var gchild = null
		if child is StaticBody3D:
			# Converts only RigidBody to entity.
			gchild = Entity3D.new()
		elif child is CollisionShape3D:
			# The shape are converted when the StaticBody3D is detected, nothing more to do at this point
			continue
		else:
			gchild = child.duplicate()

		godex_node.add_child(gchild)
		gchild.set_owner(root)
		convert_to_godex(child, gchild, root)

	# Now convert this node to godex
	# Set the name
	godex_node.name = node.name

	if not (godex_node is Entity3D or godex_node is Entity2D):
		# This is not a godex node, nothing more to do here.
		return

	# Add the child, so the Hierarchy transformation is resolved
	godex_node.add_component("Child")

	if node is StaticBody3D:
		# Set transform
		godex_node.add_component("TransformComponent", {"transform": node.get_transform()})
		godex_node.add_component("BtRigidBody", {"body_mode": 3}) # 3 is Static mode

		# Search the CollisionShape from the child and add the shape component accordingly.
		for i in range(node.get_child_count()):
			var c = node.get_child(i)
			if c is CollisionShape3D:
				if c.shape is BoxShape3D:
					var shared_res := SharedComponentResource.new()
					shared_res.init("BtBox")
					shared_res.set("half_extents", c.shape.size * 0.5)
					godex_node.add_component("BtBox", {"resource": shared_res})
				elif c.shape is ConvexPolygonShape3D:
					var shared_res := SharedComponentResource.new()
					shared_res.init("BtConvex")
					shared_res.set("points", c.shape.points)
					godex_node.add_component("BtConvex", {"resource": shared_res})
				elif c.shape is ConcavePolygonShape3D:
					var shared_res := SharedComponentResource.new()
					shared_res.init("BtTrimesh")
					shared_res.set("faces", c.shape.get_faces())
					godex_node.add_component("BtTrimesh", {"resource": shared_res})
				else:
					print("[ERROR][GodexImporter] This shape type is not supported: " + c.shape.get_class())
				
				# Shape found, nothing more to search
				break