From 01ffe6cf89ee0ca32222f6993a2f8e3c872ce0b5 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 9 Oct 2014 19:44:27 -0300 Subject: -Rasterizer supports meshes with both skeletons and blend shapes -Collada exporter supports Blend Shapes (even on actions via set driven keys) --- tools/export/blender25/io_scene_dae/__init__.py | 8 +- tools/export/blender25/io_scene_dae/export_dae.py | 276 +++++++++++++++++----- 2 files changed, 217 insertions(+), 67 deletions(-) (limited to 'tools/export/blender25/io_scene_dae') diff --git a/tools/export/blender25/io_scene_dae/__init__.py b/tools/export/blender25/io_scene_dae/__init__.py index 29fa6ed114..8d6f6b4976 100644 --- a/tools/export/blender25/io_scene_dae/__init__.py +++ b/tools/export/blender25/io_scene_dae/__init__.py @@ -19,14 +19,14 @@ # bl_info = { - "name": "Khronos Collada format", + "name": "Better Collada Exporter", "author": "Juan Linietsky", "blender": (2, 5, 8), "api": 38691, "location": "File > Import-Export", - "description": ("Export DAE Scenes"), + "description": ("Export DAE Scenes, This plugin actually works better! otherwise contact me."), "warning": "", - "wiki_url": ("None"), + "wiki_url": ("http://www.godotengine.org"), "tracker_url": "", "support": 'OFFICIAL', "category": "Import-Export"} @@ -171,7 +171,7 @@ class ExportDAE(bpy.types.Operator, ExportHelper): def menu_func(self, context): - self.layout.operator(ExportDAE.bl_idname, text="Khronos Collada (.dae)") + self.layout.operator(ExportDAE.bl_idname, text="Better Collada (.dae)") def register(): diff --git a/tools/export/blender25/io_scene_dae/export_dae.py b/tools/export/blender25/io_scene_dae/export_dae.py index 801eb7ae4c..9c104901f5 100644 --- a/tools/export/blender25/io_scene_dae/export_dae.py +++ b/tools/export/blender25/io_scene_dae/export_dae.py @@ -123,6 +123,11 @@ class DaeExporter: tup = (self.vertex.x,self.vertex.y,self.vertex.z,self.normal.x,self.normal.y,self.normal.z) for t in self.uv: tup = tup + (t.x,t.y) + #for t in self.bones: + # tup = tup + (t) + #for t in self.weights: + # tup = tup + (t) + return tup def __init__(self): @@ -315,13 +320,127 @@ class DaeExporter: return matid - def export_mesh(self,node,armature=None,shapename=None): + def export_mesh(self,node,armature=None,skeyindex=-1,skel_source=None): mesh = node.data - if (node.data in self.mesh_cache) and shapename==None: + + + if (node.data in self.mesh_cache): return self.mesh_cache[mesh] - if (len(node.modifiers) and self.config["use_mesh_modifiers"]) or shapename!=None: + if (skeyindex==-1 and mesh.shape_keys!=None and len(mesh.shape_keys.key_blocks)): + values=[] + morph_targets=[] + md=None + for k in range(0,len(mesh.shape_keys.key_blocks)): + shape = node.data.shape_keys.key_blocks[k] + values+=[shape.value] #save value + shape.value=0 + + mid = self.new_id("morph") + + for k in range(0,len(mesh.shape_keys.key_blocks)): + + shape = node.data.shape_keys.key_blocks[k] + node.show_only_shape_key=True + node.active_shape_key_index = k + shape.value = 1.0 + mesh.update() + """ + oldval = shape.value + shape.value = 1.0 + + """ + p = node.data + v = node.to_mesh(bpy.context.scene, True, "RENDER") + node.data = v +# self.export_node(node,il,shape.name) + node.data.update() + if (armature and k==0): + md=self.export_mesh(node,armature,k,mid) + else: + md=self.export_mesh(node,None,k) + + node.data = p + node.data.update() + shape.value = 0.0 + morph_targets.append(md) + + """ + shape.value = oldval + """ + node.show_only_shape_key=False + node.active_shape_key_index = 0 + + + self.writel(S_CONT,1,'') + #if ("skin_id" in morph_targets[0]): + # self.writel(S_CONT,2,'') + #else: + self.writel(S_CONT,2,'') + + self.writel(S_CONT,3,'') + self.writel(S_CONT,4,'') + marr="" + warr="" + for i in range(len(morph_targets)): + if (i==0): + continue + elif (i>1): + marr+=" " + + if ("skin_id" in morph_targets[i]): + marr+=morph_targets[i]["skin_id"] + else: + marr+=morph_targets[i]["id"] + + warr+=" 0" + + self.writel(S_CONT,5,marr) + self.writel(S_CONT,4,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,5,'') + self.writel(S_CONT,6,'') + self.writel(S_CONT,5,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,3,'') + + self.writel(S_CONT,3,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,5,warr) + self.writel(S_CONT,4,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,5,'') + self.writel(S_CONT,6,'') + self.writel(S_CONT,5,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,3,'') + + self.writel(S_CONT,3,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,4,'') + self.writel(S_CONT,3,'') + self.writel(S_CONT,2,'') + self.writel(S_CONT,1,'') + if (armature!=None): + + self.armature_for_morph[node]=armature + + meshdata={} + if (armature): + meshdata = morph_targets[0] + meshdata["morph_id"]=mid + else: + meshdata["id"]=morph_targets[0]["id"] + meshdata["morph_id"]=mid + meshdata["material_assign"]=morph_targets[0]["material_assign"] + + + + self.mesh_cache[node.data]=meshdata + return meshdata + + if (len(node.modifiers) and self.config["use_mesh_modifiers"]): mesh=node.to_mesh(self.scene,True,"RENDER") #is this allright? else: mesh=node.data @@ -349,7 +468,6 @@ class DaeExporter: for fi in range(len(mesh.tessfaces)): f=mesh.tessfaces[fi] - if (not (f.material_index in surface_indices)): surface_indices[f.material_index]=[] print("Type: "+str(type(f.material_index))) @@ -419,7 +537,7 @@ class DaeExporter: tup = v.get_tup() idx = 0 - if (tup in vertex_map): + if (skeyindex==-1 and tup in vertex_map): #do not optmize if using shapekeys idx = vertex_map[tup] else: idx = len(vertices) @@ -428,12 +546,9 @@ class DaeExporter: indices.append(idx) - if shapename != None: - meshid = self.new_id("mesh_"+shapename) - self.writel(S_GEOM,1,'') - else: - meshid = self.new_id("mesh") - self.writel(S_GEOM,1,'') + + meshid = self.new_id("mesh") + self.writel(S_GEOM,1,'') self.writel(S_GEOM,2,'') @@ -526,20 +641,26 @@ class DaeExporter: self.writel(S_GEOM,2,'') self.writel(S_GEOM,1,'') + meshdata={} meshdata["id"]=meshid meshdata["material_assign"]=mat_assign - self.mesh_cache[node.data]=meshdata + if (skeyindex==-1): + self.mesh_cache[node.data]=meshdata # Export armature data (if armature exists) - if (armature!=None): + if (armature!=None and (skel_source!=None or skeyindex==-1)): contid = self.new_id("controller") self.writel(S_CONT,1,'') - self.writel(S_CONT,2,'') + if (skel_source!=None): + self.writel(S_CONT,2,'') + else: + self.writel(S_CONT,2,'') + self.writel(S_CONT,3,''+strmtx(node.matrix_world)+'') #Joint Names self.writel(S_CONT,3,'') @@ -614,7 +735,7 @@ class DaeExporter: return meshdata - def export_mesh_node(self,node,il,shapename=None): + def export_mesh_node(self,node,il): if (node.data==None): return @@ -624,14 +745,19 @@ class DaeExporter: if (node.parent.type=="ARMATURE"): armature=node.parent - meshdata = self.export_mesh(node,armature,shapename) + meshdata = self.export_mesh(node,armature) + close_controller=False - if (armature==None): - self.writel(S_NODES,il,'') - else: + if ("skin_id" in meshdata): + close_controller=True self.writel(S_NODES,il,'') for sn in self.skeleton_info[armature]["skeleton_nodes"]: self.writel(S_NODES,il+1,'#'+sn+'') + elif ("morph_id" in meshdata): + self.writel(S_NODES,il,'') + close_controller=True + elif (armature==None): + self.writel(S_NODES,il,'') if (len(meshdata["material_assign"])>0): @@ -644,17 +770,17 @@ class DaeExporter: self.writel(S_NODES,il+2,'') self.writel(S_NODES,il+1,'') - if (armature==None): - self.writel(S_NODES,il,'') - else: + if (close_controller): self.writel(S_NODES,il,'') + else: + self.writel(S_NODES,il,'') def export_armature_bone(self,bone,il,si): boneid = self.new_id("bone") boneidx = si["bone_count"] si["bone_count"]+=1 - bonesid = si["name"]+"-"+str(boneidx) + bonesid = si["id"]+"-"+str(boneidx) si["bone_index"][bone.name]=boneidx si["bone_ids"][bone]=boneid si["bone_names"].append(bonesid) @@ -683,7 +809,7 @@ class DaeExporter: self.skeletons.append(node) armature = node.data - self.skeleton_info[node]={ "bone_count":0, "name":node.name, "bone_index":{},"bone_ids":{},"bone_names":[],"bone_bind_poses":[],"skeleton_nodes":[],"armature_xform":node.matrix_world } + self.skeleton_info[node]={ "bone_count":0, "id":self.new_id("skelbones"),"name":node.name, "bone_index":{},"bone_ids":{},"bone_names":[],"bone_bind_poses":[],"skeleton_nodes":[],"armature_xform":node.matrix_world } @@ -912,21 +1038,18 @@ class DaeExporter: - def export_node(self,node,il,shapename=None): + def export_node(self,node,il): if (not self.is_node_valid(node)): return bpy.context.scene.objects.active = node - if shapename != None: - self.writel(S_NODES,il,'') - else: - self.writel(S_NODES,il,'') + self.writel(S_NODES,il,'') il+=1 self.writel(S_NODES,il,''+strmtx(node.matrix_local)+'') print("NODE TYPE: "+node.type+" NAME: "+node.name) if (node.type=="MESH"): - self.export_mesh_node(node,il,shapename) + self.export_mesh_node(node,il) elif (node.type=="CURVE"): self.export_curve_node(node,il) elif (node.type=="ARMATURE"): @@ -937,22 +1060,9 @@ class DaeExporter: self.export_lamp_node(node,il) self.valid_nodes.append(node) - if shapename==None: - for x in node.children: - self.export_node(x,il) - if node.type=="MESH" and self.config["export_shapekeys"]: - for k in range(0,len(node.data.shape_keys.key_blocks)): - shape = node.data.shape_keys.key_blocks[k] - oldval = shape.value - shape.value = 1.0 - node.active_shape_key_index = k - p = node.data - v = node.to_mesh(bpy.context.scene, True, "RENDER") - node.data = v - self.export_node(node,il,shape.name) - node.data = p - node.data.update() - shape.value = oldval + for x in node.children: + self.export_node(x,il) + il-=1 self.writel(S_NODES,il,'') @@ -1003,18 +1113,22 @@ class DaeExporter: self.writel(S_ASSET,0,'') - def export_animation_transform_channel(self,target,transform_keys): + def export_animation_transform_channel(self,target,keys,matrices=True): - frame_total=len(transform_keys) + frame_total=len(keys) anim_id=self.new_id("anim") self.writel(S_ANIM,1,'') source_frames = "" source_transforms = "" source_interps = "" - for k in transform_keys: + for k in keys: source_frames += " "+str(k[0]) - source_transforms += " "+strmtx(k[1]) + if (matrices): + source_transforms += " "+strmtx(k[1]) + else: + source_transforms += " "+str(k[1]) + source_interps +=" LINEAR" @@ -1028,15 +1142,26 @@ class DaeExporter: self.writel(S_ANIM,3,'') self.writel(S_ANIM,2,'') - # Transform Source - self.writel(S_ANIM,2,'') - self.writel(S_ANIM,3,''+source_transforms+'') - self.writel(S_ANIM,3,'') - self.writel(S_ANIM,4,'') - self.writel(S_ANIM,5,'') - self.writel(S_ANIM,4,'') - self.writel(S_ANIM,3,'') - self.writel(S_ANIM,2,'') + if (matrices): + # Transform Source + self.writel(S_ANIM,2,'') + self.writel(S_ANIM,3,''+source_transforms+'') + self.writel(S_ANIM,3,'') + self.writel(S_ANIM,4,'') + self.writel(S_ANIM,5,'') + self.writel(S_ANIM,4,'') + self.writel(S_ANIM,3,'') + self.writel(S_ANIM,2,'') + else: + # Value Source + self.writel(S_ANIM,2,'') + self.writel(S_ANIM,3,''+source_transforms+'') + self.writel(S_ANIM,3,'') + self.writel(S_ANIM,4,'') + self.writel(S_ANIM,5,'') + self.writel(S_ANIM,4,'') + self.writel(S_ANIM,3,'') + self.writel(S_ANIM,2,'') # Interpolation Source self.writel(S_ANIM,2,'') @@ -1053,7 +1178,10 @@ class DaeExporter: self.writel(S_ANIM,3,'') self.writel(S_ANIM,3,'') self.writel(S_ANIM,2,'') - self.writel(S_ANIM,2,'') + if (matrices): + self.writel(S_ANIM,2,'') + else: + self.writel(S_ANIM,2,'') self.writel(S_ANIM,1,'') return [anim_id] @@ -1075,6 +1203,7 @@ class DaeExporter: tcn = [] xform_cache={} + blend_cache={} # Change frames first, export objects last # This improves performance enormously @@ -1089,9 +1218,26 @@ class DaeExporter: if (not node in self.valid_nodes): continue if (allowed!=None and not (node in allowed)): - continue + if (node.type=="MESH" and node.data!=None and (node in self.armature_for_morph) and (self.armature_for_morph[node] in allowed)): + pass #all good you pass with flying colors for morphs inside of action + else: + continue + if (node.type=="MESH" and node.data!=None and node.data.shape_keys!=None and (node.data in self.mesh_cache) and len(node.data.shape_keys.key_blocks)): + target = self.mesh_cache[node.data]["morph_id"] + for i in range(len(node.data.shape_keys.key_blocks)): + + if (i==0): + continue + + name=target+"-morph-weights("+str(i-1)+")" + if (not (name in blend_cache)): + blend_cache[name]=[] + + blend_cache[name].append( (key,node.data.shape_keys.key_blocks[i].value) ) + if (node.type=="MESH" and node.parent and node.parent.type=="ARMATURE"): + continue #In Collada, nodes that have skin modifier must not export animation, animate the skin instead. if (len(node.constraints)>0 or node.animation_data!=None): @@ -1108,6 +1254,7 @@ class DaeExporter: if (node.type=="ARMATURE"): #All bones exported for now + for bone in node.data.bones: bone_name=self.skeleton_info[node]["bone_ids"][bone] @@ -1138,7 +1285,9 @@ class DaeExporter: #export animation xml for nid in xform_cache: - tcn+=self.export_animation_transform_channel(nid,xform_cache[nid]) + tcn+=self.export_animation_transform_channel(nid,xform_cache[nid],True) + for nid in blend_cache: + tcn+=self.export_animation_transform_channel(nid,blend_cache[nid],False) return tcn @@ -1197,7 +1346,7 @@ class DaeExporter: print("Export anim: "+x.name) self.writel(S_ANIM_CLIPS,1,'') for z in tcn: - self.writel(S_ANIM_CLIPS,2,'') + self.writel(S_ANIM_CLIPS,2,'') self.writel(S_ANIM_CLIPS,1,'') @@ -1276,6 +1425,7 @@ class DaeExporter: self.skeleton_info={} self.config=kwargs self.valid_nodes=[] + self.armature_for_morph={} -- cgit v1.2.3