From 705438a239622899a259f69266539a24fb9fb1cb Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sun, 31 May 2020 15:58:18 +0800 Subject: 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. --- Virtools/3dxml.py | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'Virtools/3dxml.py') 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)