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 ExportAsLight(self, obj, node=None): """ Export object as a light node. If the light has different color values for Ambient, Diffuse and Specular, create a mesh object and give its material colors the desired values then parent it to the light. The objects name has to be in the format <light_name>_"color" e.g. if the light's name is Lamp01 the Meshes name should be "Lamp01_color" """ if(obj.getType()=="Lamp"): obj_name = obj.getName() lamp = obj.getData() scene_node = n2.lookup(n2.psel()) scene_node.setfloat("LightRange", lamp.getDist()) #scene_node.settype("Directional") mode = lamp.getMode() if(mode & lamp.Modes["RayShadow"]): scene_node.setcastshadows(True) else: scene_node.setcastshadows(False) color_info_obj = GetChildByName(obj, obj_name + "_color") if(color_info_obj and color_info_obj.getType() == "Mesh" and color_info_obj.getData(mesh=True).materials[0]): material = color_info_obj.getData(mesh=True).materials[0] # Col = Diffuse, Spe = Specular, Mir = Ambient diffuse = material.getRGBCol() specular = material.getSpecCol() ambient = material.getMirCol() scene_node.setvector("LightDiffuse", diffuse[0], diffuse[1], diffuse[2], 1.0) scene_node.setvector("LightSpecular", specular[0], specular[1], specular[2], 1.0) scene_node.setvector("LightAmbient", ambient[0], ambient[1], ambient[2], 1.0) else: n2.nprint("WARNING: Color information object for Lamp '%s' not found or not valid" % obj_name) col = lamp.col scene_node.setvector("LightDiffuse", col[0], col[1], col[2], 1.0) scene_node.setvector("LightSpecular", col[0], col[1], col[2], 1.0) scene_node.setvector("LightAmbient", col[0], col[1], col[2], 1.0) else: n2.nprint("WARNING: %s is Not a Lamp and was not exported\n" % obj.getName())