Game Development Community

dev|Pro Game Development Curriculum

How to fix Collada files exported from a DIF

by Thomas -elfprince13- Dickerson · 01/31/2013 (5:22 pm) · 5 comments

The first step is to open up your Collada file in a text editor, and repair all of <init_from> values in each <image> in the <images_library>. They will probably all look something like "file://PATH/TO/FILE" (and may even be lacking in file extensions. Replace each of those URIs with a normal (Unix-style: /) file reference, probably relative to the current location of your .dae file, and respecting the case sensitivities of your file-system (this step is actually optional if you're only using it in Torque, but most people want to get their model into an editor).

Then run it through this Python script (written in Python 2.7, depends on ElementTree)

#!/usr/bin/env python
import xml.etree.ElementTree as ET
import sys
from re import compile

whitespace = compile(r"s+")

ET.register_namespace("","http://www.collada.org/2005/11/COLLADASchema")

tree = ET.parse(sys.argv[1])#"bridge_bdbd9598.dae")

root = tree.getroot()
fx = root.find("{http://www.collada.org/2005/11/COLLADASchema}library_effects")
for child in fx:
	name = child.attrib['id'][:-3]
	prof = child.find("{http://www.collada.org/2005/11/COLLADASchema}profile_COMMON")
	surparam = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}newparam",attrib={'sid':"%s-Surface"%(name)})
	surface = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}surface",attrib={'type':"2D"})
	sur_init = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}init_from")
	sur_init.text = "%s-Diffuse" % (name)
	surface.append(sur_init)
	surparam.append(surface)
	prof.insert(0,surparam)
	samplerparam = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}newparam",attrib={'sid':"%s-Sampler"%(name)})
	sampler = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}sampler2D")
	source = ET.Element("{http://www.collada.org/2005/11/COLLADASchema}source")
	source.text = "%s-Surface" % (name)
	sampler.append(source)
	samplerparam.append(sampler)
	prof.insert(1,samplerparam)
	tex = prof.find("{http://www.collada.org/2005/11/COLLADASchema}technique").find("{http://www.collada.org/2005/11/COLLADASchema}phong").find("{http://www.collada.org/2005/11/COLLADASchema}diffuse").find("{http://www.collada.org/2005/11/COLLADASchema}texture")
	tex.set("texture","%s-Sampler" %(name))
geo = root.find("{http://www.collada.org/2005/11/COLLADASchema}library_geometries")
for child in geo:
	name = child.attrib['id'][:-4]
	mesh = child.find("{http://www.collada.org/2005/11/COLLADASchema}mesh")
	uvs = mesh.find("{http://www.collada.org/2005/11/COLLADASchema}source[@id='%s-UV0']" % (name))
	uv_txt = uvs.find("{http://www.collada.org/2005/11/COLLADASchema}float_array")
	fmt = "                        %.4f %.4frn"
	nums = [float(entry) for entry in whitespace.split(uv_txt.text) if entry != ""]
	nums[1::2] = [(1-f) for f in nums[1::2]]
	fmt *= len(nums)/2
	uv_txt.text = fmt % tuple(nums)
	

	
tree.write(sys.argv[2],encoding="utf-8",xml_declaration=True)

and enjoy. The textures will be oriented correctly and everything. Note that this only patches UV0 coordinates, so if you use other sets of UV coordinates in your DIF (I don't think this is possible? but just in case), you'll need to add a couple more lines.

the usage for the script looks like:
python fix_torque_collada.py in_from_torque_randomletters.dae fixeredup.dae

About the author

C.S. PhD student at Brown University. Project lead for FreeBuild. Administrator, Cemetech tech community. Webmaster for the Village2Village Projects and the Vermont Sustainable Heating Initiative.


#1
01/31/2013 (7:25 pm)
Quote:f <init_from> values in each <image> in the <images_library>"

may be you are talking about this warnings:


Warning: Fixup invalid URI in Map__2-image: "file://C:UsersShoikoDesktopGame%20ArtAnimeRPGCharterPackVillagerVillager02VILLAGER02_Eye.png"



Error: Could not read file:/E:/3d/3D-Objekte/Chinaguy/Jaguar%2004%2012/Jaguar_H/Leopardpeople_L.dae into memory
Error: Failed to load external reference: file:/E:/3d/3D-Objekte/Chinaguy/Jaguar%2004%2012/Jaguar_H/Leopardpeople_L.dae


none of those are pointing to any valid path(of my pc).

do not know anything about python.so i did not try your code.
but i will try later.

question is:
do i have to cleanup one by one manually?
is there anyway to run a batch processing to auto cleanup/fix those lines from all dae files?
#2
01/31/2013 (8:29 pm)
Hi Ahsan. Right now the part that still must be done manually is fixing the bad URIs (because I don't know where the textures *should* be stored in the general case). It sounds like you need to open up the dae file in a text editor and change them to point where they should (for example "./VILLAGER02_Eye.png" instead of "file://C:/Users/..../VILLAGER02_Eye.png" if your texture is in the same directory).


Keep in mind that I only wrote this to patch up Collada files exported from a DIF in Torque 3D, so if you got your errors on character models rather than interior buildings, this probably isn't what you're looking for.

That said, on a Unix operating system, I would probably use the find command with the -exec option to do this as a batch process. You can do that on Windows as well using the gnuwin32 prebuilt programs and libraries or with Cygwin or MinGW. You may also be able to do it in a more Windowsy fashion, but my familiarity with batch files is less, so you'll have to ask someone else for help constructing the appropriate commands.
#3
02/01/2013 (11:34 pm)
I love using Python for tasks like this. Especially lxml which can use etree syntax.
#4
02/04/2013 (10:42 am)
Thomas @ I just use a grep to remove all of those file: statements and a few others ... to rid all of those warning errors from the console.log

ie... removing the line seems to have no negative impact, at least when imported into T3D.

www.garagegames.com/community/forums/viewthread/132930
#5
02/04/2013 (11:22 am)
Changing the file: statements is important for other editors, not T3D specifically :)