3DXML Importer

I quickly hacked together a 3DXML importer for v2.49. The format is based on XML and python’s new etree module makes parsing very easy.

It’s meant for use with 3DVIA PrintScreen, which is a cool utility that allows you to capture any model (with textures) from almost any opengl app or game.

http://home.comcast.net/~gamma-ray/tmp/import_3dxml.py

Requires a full python 2.6 installation.

I tried importing this file


and I got this error
Unzipping material_3_8_Rendering.3DRep
Traceback (most recent call last):
File “C:\Program Files\Blender Foundation\Blender249.blender\scripts\import_3
dxml.py”, line 161, in read
load_meshes(filename)
File “C:\Program Files\Blender Foundation\Blender249.blender\scripts\import_3
dxml.py”, line 19, in load_meshes
numimages = len(images)
TypeError: object of type ‘NoneType’ has no len()

I tried to import this model in 3dxml format.

And I got this error

Unzipping material_0_8_Rendering.3DRep
Traceback (most recent call last):
File “C:\Program Files\Blender Foundation\Blender249.blender\scripts\import_3
dxml.py”, line 161, in read
load_meshes(filename)
File “C:\Program Files\Blender Foundation\Blender249.blender\scripts\import_3
dxml.py”, line 19, in load_meshes
numimages = len(images)
TypeError: object of type ‘NoneType’ has no len()

The importer does not read models that do not have textures or uv coords.

Thanks Scopius

scorpius,

any chance you can make a script that imports without UVs or testures. 3dxml and grannyviewer don’t work well

hello, I have tried to install the script into my blender(2.49a) and have failed.

the error was at line 126 of the code

3DXML_import.py:126: Warning: 'with ’ will become a reserved keyword in python 2.6
File “3DXML_import.py” , line 126
with open (pathname, “wb”) as file:
^
syntax error: invalid error

is that a wrong version of python or did i do something wrong during installation. I dont know what.

i am not used to the blender api so please please please help me!

thank you

i am also on a mac if that helps

pm me with your email and I’ll send you the working file I have. for some reason the forum won’t let me post the code

I don’t have plans to update the script any time soon because I only used it once or twice myself.

That’s fine I have a working script that kept UV mapping. Unfortunately the forum won’t let me post it

3DXML_import.py:126: Warning: 'with ’ will become a reserved keyword in python 2.6
File “3DXML_import.py” , line 126
with open (pathname, “wb”) as file:
^
syntax error: invalid error

Please help!!!

riftmaster

the error i posted above is on my mac. when i tried it on windows, it creates the texture files then crashes

Help

Just copy below coe and paste in notepad txt file then save it and change the extension to py. Script follows:



#!BPY
"""
Name: '3dXML...'
Blender: 249
Group: 'Import'
Tooltip: 'Import 3dxml'
"""
__author__ = "Captain Binky"
__url__ = ("blender","elysiun","Author's homepage, ")
__version__ = "2.0"
__bpydoc__ = ""
import Blender, zipfile, base64, struct, os, sys, time, string
import xml.etree.ElementTree as etree
def load_meshes(filename):
tree = etree.parse(filename)
images = tree.find("//{http://www.3ds.com/xsd/3DXML}ImageSet")
numimages = len(images)
print "ImageSet:", images
imgs = []
imgidxs = []
for i in tree.findall("//{http://www.3ds.com/xsd/3DXML}Material"):
imgidx = i.attrib["texture"]
imgidx = int(imgidx.split(":")[-1])
imgidxs.append(imgidx)
print "Image Idx:", imgidx
allverts = []
for i in tree.findall("//{http://www.3ds.com/xsd/3DXML}Positions"):
verts = unflatten(i.text, float, 3)
allverts.append(verts)
print "NumVerts:", len(verts)
alltxuvs = []
for i in tree.findall("//{http://www.3ds.com/xsd/3DXML}TextureCoordinates"):
uvcos = unflatten(i.text, float, 2)
alltxuvs.append(uvcos)
print "NumUVs:", len(uvcos)
allfaces = []
for i in tree.findall("//{http://www.3ds.com/xsd/3DXML}Faces"):
fdict = i[1].attrib
if "triangles" in fdict:
faces = fdict["triangles"]
faces = string.replace(faces, ",", "")
faces = unflatten(faces, int, 3)
print "NumFaces:", len(faces)
elif "fans" in fdict:
faces = fdict["fans"]
faces = string.replace(faces, ",", "")
faces = unflatten(faces, int, 4)
print "NumFans:", len(faces)
else:
print "err", fdict
allfaces.append(faces)
objs = zip(allverts, alltxuvs, allfaces)
if len(objs) != len(imgidxs):
print "len(objs) != len(imgidxs)", len(objs), len(imgidxs)
print "Number of objects:", len(objs)
for (objidx,obj) in enumerate(objs):
verts, txuvs, faces = obj
create_mesh(verts, faces, objidx, txuvs, imgs, imgidxs)
def create_mesh(verts, faces, objidx, uvs, imgs, imgidxs):
if len(verts) != len(uvs):
print objidx,
print "len(verts) != len(uvs)", len(verts), len(uvs),
print "resetting the uv list"
uvs = [uvs[0]]*len(verts)
numobjs = len(imgidxs)
size = len(`numobjs`)
objname = str("Mesh-%0"+`size`+"d") % (objidx+1)
mesh = Blender.Mesh.New(objname)
mesh.vertexUV = 1
mesh.verts.extend(verts)
mesh.faces.extend(faces)
for i in range(len(verts)):
mesh.verts[i].uvco = Blender.Mathutils.Vector(uvs[i])
for face in mesh.faces:
face.smooth = 0
#face.image = imgs[imgidxs[objidx]-1]
pervertuv_to_perfaceuv(mesh)
scn = Blender.Scene.GetCurrent()
obj = scn.objects.new(mesh)
obj.sel = True
mesh.remDoubles(0.0)
Blender.Redraw()
def pervertuv_to_perfaceuv(mesh):
for face in mesh.faces:
face.uv = [vert.uvco for vert in face.verts]
#face.mode |= Blender.Mesh.FaceModes.TWOSIDE
mesh.vertexUV = 0
def unflatten(seq, func, step):
if step == 2: seq = seq.replace(",", "")
seq = seq.split()
seq = list(map(func, seq))
nseq = len(seq)
if step == 2:
return [(seq[i],seq[i+1]) for i in range(0, nseq, step)]
elif step == 3:
return [(seq[i],seq[i+1],seq[i+2]) for i in range(0, nseq, step)]
elif step == 4:
return [(seq[i],seq[i+1],seq[i+2],seq[i+3]) for i in range(0, nseq, step)]
else:
return None
def write_tga(image, numimages):
h = int(image.attrib["height"])
w = int(image.attrib["width"])
fmt = image.attrib["format"]
pixels = image[0].text
size = len(`numimages`)
imgidx = image.attrib["id"]
imgidx = str("Map-%0"+`size`+"d") % int(imgidx)
filename = imgidx + ".tga"
bits = 32 if "A" in fmt else 24
alpha = 8 if "A" in fmt else 0
dirname = "textures"
if not os.path.exists(dirname): os.mkdir(dirname)
pathname = os.path.join(dirname, filename)
if not os.path.exists(pathname):
print "writing %s (%dx%d) %s" % (pathname, w, h, fmt.lower())
with open(pathname, "wb") as file:
file.write(struct.pack("12B", 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0))
file.write(struct.pack("HH", w, h))
file.write(struct.pack("BB", bits, alpha))
pixels = base64.b64decode(pixels) #$base64.b64decode(bytes(pixels, "utf-8"))
pixels = rgb_to_bgr(pixels, fmt)
file.write(pixels)
image = Blender.Image.Load(pathname)
image.makeCurrent()
Blender.Window.RedrawAll()
return(image)
def rgb_to_bgr(pixels, fmt):
pixels = list(pixels)
npixels = len(pixels)
step = 4 if "A" in fmt else 3
for i in range(0, npixels, step):
pixels[i],pixels[i+2] = pixels[i+2],pixels[i]
#$pixels = map(chr, pixels)
pixels = "".join(pixels)
#$pixels = bytes(pixels, "latin-1")
return pixels
 
def read(filename):
time1 = time.time()
path = os.path.dirname(filename)
os.chdir(path)
os.system("cls") if "nt" in os.name else os.system("clear")
print path
if zipfile.is_zipfile(filename):
zf = zipfile.ZipFile(filename, "r")
member = zf.namelist()[1]
filename = zf.open(member)
print "Unzipping", member
load_meshes(filename)
time2 = time.time()
print "Total import time is: %.2f seconds." % (time2 - time1)
if __name__=="__main__":
Blender.Window.FileSelector(read, "Import 3dXML", "*.3dxml")
 



Hi ekmek
[noparse]
Use code tags to enclose code to keep formatting


paste code here

[/noparse]

I have been trying to install this on my blender. The add-on won’t install and I keep getting an error on the terminal.
What version of blender does this work on? I am using v2.64
Any special instructions?

This is the error message:
fake_module: addon missing ‘bl_info’ gives bad performance!: ‘C:\Users\…\Blender\2.64\scripts\addons\import_3dxml.py’

Any help is greatly appreciated

I am trying to get this installed into blender. I have tried with v2.64 and v2.49
It threw up errors when I tried to install on 2.64 and it just didn’t work. I suspect compatibility issues.
It installed on 2.49 but when i try to import something it says “ImportError: No module named zipfile”

Please help.

It requires a full python 2.6 installation, as stated in the 1st post.

I tried to install this script on blender v2.49b by putting it in the “.blender/scripts” folder where it is supposed to go, but in blender, it doesn’t give me the option to import .3dxml files. I have python 2.6 installed, what am I doing wrong?

Blender is not seeing where you put it. There should be – somewhere in the options page – a place you can select your scripts directory.

im i supposted to go in the blender foundation folder?