diff options
author | 2020-05-31 15:58:18 +0800 | |
---|---|---|
committer | 2020-05-31 15:58:18 +0800 | |
commit | 705438a239622899a259f69266539a24fb9fb1cb (patch) | |
tree | 85b07021e8b523dbbd3b78de79d06d53bca7e9b1 /Virtools | |
parent | ed47c1557915bb2472f6959e723cd76155312a98 (diff) | |
download | oddities-705438a239622899a259f69266539a24fb9fb1cb.tar.xz |
Load normals from the 3DXML file, added an option for using auto smooth.
Fixed random reference errors.
Also fixed several brain farts.
3DXML files generated by Virtools seems to have normals randomly flipped
to the other side. There might be some unspecified ordering issues with
the vertices in each face. I'm currently using auto smoothing as a
workaround.
Diffstat (limited to 'Virtools')
-rw-r--r-- | Virtools/3dxml.py | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/Virtools/3dxml.py b/Virtools/3dxml.py index c95cbf8..a72cf54 100644 --- a/Virtools/3dxml.py +++ b/Virtools/3dxml.py @@ -5,7 +5,7 @@ bl_info={ "description":"Import 3D XML 3.0", "author":"Chris Xiong", "version":(0,1), - "blender":(2,80,0), + "blender":(2,82,0), "category":"Import-Export", "support":"TESTING" } @@ -36,7 +36,7 @@ bl_info={ ################################################################ import bpy,bmesh,bpy_extras,mathutils import xml.etree.ElementTree as etree -import pathlib,zipfile,time,os,tempfile +import pathlib,zipfile,time,os,tempfile,math NS="{http://www.3ds.com/xsd/3DXML}" @@ -90,6 +90,7 @@ def load_meshes(tree): for rep in tree.findall(f".//{NS}Representation"): rid=rep.attrib["id"] verts=unflatten(rep.find(f".//{NS}Positions").text,float,3,unitfactor) + normals=unflatten(rep.find(f".//{NS}Normals").text,float,3) uvs=unflatten(rep.find(f".//{NS}TextureCoordinates").text,float,2) faces=[] facemat=[] @@ -116,15 +117,20 @@ def load_meshes(tree): faces.extend(unflatten(face.attrib["fans"],int,4)) facemat.extend([fmat]*len(unflatten(face.attrib["fans"],int,4))) meshmat[rid]=matslots - create_mesh(verts,faces,facemat,uvs,rid) + create_mesh(verts,faces,facemat,normals,uvs,rid) def load_objects(tree): rr=tree.findall(f".//{NS}ReferenceRep[@format='TESSELLATED']") - i3d=tree.findall(f".//{NS}Instance3D") + sr=set() + for ref in rr: + n=ref.attrib["name"] + sr.add(n[:n.rfind('_')]) + ti3d=tree.findall(f".//{NS}Instance3D") + i3d=[i3 for i3 in ti3d if i3.attrib["name"][:i3.attrib["name"].rfind('_')] in sr] for ref,i3 in zip(rr,i3d): meshid=ref.attrib["associatedFile"].split(":")[-1] objname=ref.attrib["name"] - objname=objname[0:objname.rfind('_')] + objname=objname[:objname.rfind('_')] mat=list(map(float,i3.find(f"./{NS}RelativeMatrix").text.split(' '))) obj=bpy.data.objects.new(objname,meshes[meshid]) _wmat=mathutils.Matrix() @@ -135,15 +141,18 @@ def load_objects(tree): obj.matrix_world=_wmat scn=bpy.context.scene scn.collection.objects.link(obj) - bpy.ops.object.shade_smooth() + obj.select_set(True) -def create_mesh(verts,faces,facemat,uvs,meshidx): - if len(uvs)>len(verts): + bpy.ops.object.shade_smooth() + +def create_mesh(verts,faces,facemat,norms,uvs,meshidx): + if len(uvs)<len(verts): uvs.append([uvs[0]]*(len(verts)-len(uvs))) meshname=f"Mesh_{meshidx}" mesh=bmesh.new() - for i in verts: - mesh.verts.new(i) + for vert,norm in zip(verts,norms): + v=mesh.verts.new(vert) + v.normal=norm mesh.verts.ensure_lookup_table() mesh.verts.index_update() for i,m in zip(faces,facemat): @@ -158,8 +167,10 @@ def create_mesh(verts,faces,facemat,uvs,meshidx): for lp in face.loops: lp[uv].uv=mathutils.Vector(uvs[lp.vert.index]) msh=bpy.data.meshes.new(meshname) + if usesmooth: + msh.use_auto_smooth=True + msh.auto_smooth_angle=smoothangle mesh.to_mesh(msh) - msh.use_auto_smooth=True meshes[meshidx]=msh mesh.free() @@ -206,12 +217,16 @@ class ImportDialog(bpy.types.Operator,bpy_extras.io_utils.ImportHelper): filter_glob=bpy.props.StringProperty(default='*.3dxml',options={'HIDDEN'}) pt=bpy.props.StringProperty(name="Texture path",default=texdir) - uf=bpy.props.FloatProperty(name="Unit factor",default=unitfactor) + uf=bpy.props.FloatProperty(name="Unit factor",default=unitfactor,min=0) + us=bpy.props.BoolProperty(name="Use auto smooth instead of the normal data in the 3DXML file",default=False) + aa=bpy.props.FloatProperty(name="Auto smooth angle",min=0,max=180,default=90) def execute(self,context): - global texdir,unitfactor + global texdir,unitfactor,usesmooth,smoothangle texdir=self.pt unitfactor=self.uf + usesmooth=self.us + smoothangle=self.aa/180*math.pi read(self.filepath) return {'FINISHED'} |