def __init__(self): """ Initialise scene root /scene and meshes NOH /meshes and delete if they exists to avoid conflicts """ self.CleanUp() self.mesh_uv = True self.mesh_col = False self.mesh_format = "n3d2" self.group = "examples" n2.new("nroot", "/export") self.model = n2.new("ntransformnode", "/export/model") n2.sel(self.model) self.tmp_mesh = n2.new("nscriptablemeshbuilder", "/export/meshes/tmp") self.mesh = n2.new("nscriptablemeshbuilder", "/export/meshes/model")
def ExportAsMesh(self, obj, node=None): """ Export "obj" as Mesh by extracting and saving mesh data. """ node = n2.lookup(n2.psel()) me = obj.getData(mesh=1) editmode = Window.EditMode() if(editmode): Window.EditMode(0) obj_name = obj.getName() # Check if more than one material if(len(me.materials)>1): n2class = self.GetTypeFromProperty(obj) if(not n2class): n2class = "nshapenode" # Create dictionary with materila index as key and kist of face indices as value mat_dict = {} faces = me.faces for f in faces: if(mat_dict.has_key(f.mat)): mat_dict[f.mat].append(f) else: mat_dict[f.mat] = [] mat_dict[f.mat].append(f) # Create nshapenode for each material i = 0 # hack in case the same materila is assigned to diffrenet vertex groups for index in mat_dict: mat = me.materials[index] node = n2.new(n2class, "%s_%s_%i" % (obj_name, mat.getName(), i)) self.CreateMesh(mat_dict[index], obj) group_idx = self.mesh.append(self.tmp_mesh) try: node.setgroupindex(group_idx) node.setmesh("meshes:%s/%s.%s" % (self.group, self.filename, self.mesh_format)) self.SetShader(node, mat) except AttributeError: n2.nprint("%s 'class' property must be an nshapenode or derived\n" % obj_name) i = i+1 else: if(node.isa("nshapenode")): mesh = obj.getData(mesh=1) self.CreateMesh(me.faces, obj) group_idx = self.mesh.append(self.tmp_mesh) node.setmesh("meshes:%s/%s.%s" % (self.group, self.filename, self.mesh_format)) node.setgroupindex(group_idx) if(len(mesh.materials)>0): mat = mesh.materials[0] self.SetShader(node, mat) else: self.SetShader(node) else: n2.nprint("WARNING: Trying to export Mesh '%s' as ntransformnode, Mesh data not exported\n" % obj.getName()) Window.EditMode(editmode)
def ExportScene(self, scene=None): """ Export all objects in "scene" """ scn = Blender.Scene.GetCurrent() if(scene != None): scn = scene objs = scn.getChildren() if(len(objs)): root = n2.new("ntransformnode", self.filename) n2.sel(root) model = n2.new("ntransformnode", "model") n2.sel(model) for obj in objs: if(not obj.parent): #parented objects will be exported by parent self.ExportObj(obj) return True else: return False
def ExportObj(self, obj): """ Call for each object to be added to the export hierachy. "obj's" children will also be exported by this function. Usage. Only call directly when you need to export a single or selected objects, otherwise use ExportScene(blender_scene). Determine Nebula scenenode class by first checking for a property "class" in obj if not found determine through obj.getData() and if this fails export as ntransformnode. If "obj" has more than one material, create a transform node and export each material group as a different nshapenode """ bl_type = obj.getType() obj_name = obj.getName() n2class = None #self.GetTypeFromProperty(obj) if(bl_type == "Mesh" and len(obj.getData(mesh=1).materials)>1): # Multimaterial mesh with 'class' property n2class = "ntransformnode" elif(bl_type == "Mesh" and len(obj.getData(mesh=1).materials)<=1): n2class = self.GetTypeFromProperty(obj) if(not n2class): n2class = "nshapenode" if(not n2class): n2class = self.GetTypeFromData(obj) node = n2.new(n2class, obj_name) n2.sel(node) # TODO: Set transform properties location = obj.getLocation() pos = [location[0], location[1], location[2]] rot = Mathutils.Euler(obj.getEuler()) rot[0] = math.degrees(rot[0]) rot[1] = math.degrees(rot[1]) rot[2] = math.degrees(rot[2]) size = obj.getSize() scale = [size[0], size[1], size[2]] if(obj.parent): parent = obj.parent parent_world_mat = parent.getMatrix("worldspace") # Copy wrapped data and Invert parent_world_mat = Mathutils.Matrix(parent_world_mat) parent_world_mat.invert() local_mat = obj.getMatrix("localspace") world_mat = obj.getMatrix("worldspace") local_mat = world_mat * parent_world_mat # Rotation rot = local_mat.rotationPart().toEuler() # Position pos[0] = local_mat[3][0] pos[1] = local_mat[3][1] pos[2] = local_mat[3][2] # Scale quat = local_mat.toQuat() inv_rot_mat = quat.toMatrix() inv_rot_mat.resize4x4() inv_rot_mat.invert() scale_mat = local_mat * inv_rot_mat scale[0] = scale_mat[0][0] scale[1] = scale_mat[1][1] scale[2] = scale_mat[2][2] node.setposition(-pos[0], pos[2], pos[1]) node.seteuler(-rot[0], rot[2], rot[1]) node.setscale(scale[0], scale[2], scale[1]) if(bl_type == "Mesh"): self.ExportAsMesh(obj) elif(bl_type == "Lamp"): self.ExportAsLight(obj) # TODO: Export Animators # Get objects children and export each one while still in parent object's working directory children = GetChildren(obj) for child in children: # HACK: Dont export objetcs whose parent is a light and name is <parent>_color if(obj.getType()=="Lamp" and child.getName()==(obj.getName()+"_color")): # child provides color information pass else: self.ExportObj(child) # Select initial working dirctory n2.sel("..")