extends Object

# for Godot

# March-7-2016

# PlanetKiller exporter script

# computer-aided game creation enrichment

# Place on a node, set the apropriate function, run scene

# should automate most of the more obnoxious tasks

# ver 1.0

# checks for existing resources

# minor opimizations planned

# member variables here, example:

# var a=2

# var b="textvar"

func save_meshes ( folder , ext ) : # needs top-level parent node

var mesh

var path

for i in get_children ( ) :

if i. is_type ( "MeshInstance" ) :

mesh = i. get_mesh ( )

path = "res://" +folder+ "/" +i. get_name ( ) + "." +ext

if ( ! check_file ( path ) ) :

print ( mesh. get_path ( ) )

ResourceSaver. save ( path , mesh )

#print(ResourceSaver.save(path, mesh, 1))

func get_dir ( Folder ) :

var newDir = Directory. new ( )

var openDir = newDir. open ( "res://" +Folder+ "/" )

var dirStart = newDir. list_dir_begin ( )

return newDir

pass

func check_file ( fileDir ) :

var d2 = Directory. new ( )

return d2. file_exists ( fileDir )

pass

# should have mesh node with a default material

func fixedmat_gen ( TexFolder , outFolder , imgExt , exportExt ) :

var newDir = get_dir ( TexFolder )

var file = newDir. get_next ( )

while ( file != "" ) :

var fileExt = file . extension ( )

var fileName = file . split ( "." ) [ 0 ]

var matOver = self . get_material_override ( )

var fullPath = "res://" +TexFolder+ "/" + file

var exPath = "res://" +outFolder+ "/" +fileName+ "." +exportExt

if ( fileExt == imgExt ) :

if ( ! check_file ( exPath ) ) :

print ( file )

matOver. set_texture ( 0 , ResourceLoader. load ( fullPath ) )

matOver. set_path ( exPath )

ResourceSaver. save ( exPath , matOver )

file = newDir. get_next ( )

pass

func scene_exporter ( sceneTop , meshFile , fileName , outputFolder , exportExt ) :

#var exPath = "res://"+outputFolder+"/"+file

var packed_scene = PackedScene. new ( )

#var fileName = meshFile.split( ".")[0]

print ( fileName )

var exPath = "res://" +outputFolder+ "/" +fileName+ "." +exportExt

print ( exPath )

for i in sceneTop. get_children ( ) :

i. set_owner ( sceneTop )

if i. is_type ( "MeshInstance" ) :

#var name = i.get_mesh().get_name()

#print(i.get_mesh().get_name())

sceneTop. set_name ( fileName+ "Body" )

i. set_name ( fileName )

i. set_mesh ( ResourceLoader. load ( meshFile ) )

elif i. is_type ( "CollisionShape" ) :

i. set_name ( fileName+ "CS" )

var newChild = self . get_child ( 0 )

print ( self . get_child ( 0 ) )

print ( sceneTop )

if ( ! check_file ( exPath ) ) :

packed_scene. pack ( sceneTop )

packed_scene. set_path ( exPath )

ResourceSaver. save ( exPath , packed_scene )

pass

# put on top-level node, exports first child and children

func scene_save ( meshFolder , outputFolder , exportExt ) :

var newDir = get_dir ( meshFolder )

var file = newDir. get_next ( )

var sceneTop = get_tree ( ) . get_current_scene ( ) . get_child ( 0 )

while ( file != "" ) :

var fullPath = "res://" +meshFolder+ "/" + file

var fileName = file . split ( "." ) [ 0 ]

scene_exporter ( sceneTop , fullPath , fileName , outputFolder , exportExt )

file = newDir. get_next ( )

pass

# put on an animation player node

func save_anims ( folder , ext ) :

#anm

var path

var animList = self . get_animation_list ( )

for i in animList:

var anim = self . get_animation ( i )

#print(i)

#print(anim)

path = "res://" +folder+ "/" +i+ "." +ext

if ( ! check_file ( path ) ) :

ResourceSaver. save ( path , anim )

print ( i )

print ( anim )

pass

func _ready ( ) :

#save_meshes("Mesh", "msh")

#fixedmat_gen("Textures", "Mats", "png", "mtl")

#scene_save("Mesh", "test", "scn")

#save_anims("Anims", "anm")

# Called every time the node is added to the scene.

# Initialization here