# Inits Soya and sets the data directory. soya.init() soya.path.append(os.path.join(os.path.dirname(sys.argv[0]), "data")) # Sets the file format soya uses for saving file. # # The default configuration is to use cPickle for saving files, and to support loading # files saved either by cPickle or Cerealizer (if available). # # The complete syntax is set_file_format(saving_format, loading_format), where loading_format # is optional and can be either a single format, or a list of format. # There are currently 2 supported formats: cPickle and Cerealizer. # See set_file_format's __doc__ for more information. soya.set_file_format(cPickle) # Creates the scene. scene = soya.World() # Loads the sword model. sword_model = soya.Model.get("sword") # Creates a rotating body in the scene, using the sword model. sword = RotatingBody(scene, sword_model) # Creates a light.
CODE_OBJ = "R" CODE_OWN_CONTROL = "+" CODE_REMOVE_MOBILE = "-" CODE_ACTION = "A" CODE_STATE = "S" CODE_ACK_STATE = "!" CODE_MESSAGE = "M" CODE_NOOP = " " PORT = 6902 HOST = "" LOGIN = "******" PASSWORD = "******" OPT_DATA = "" soya.set_file_format(cerealizer, cerealizer) class NetworkError(StandardError): pass class PacketSocket(object): def __init__(self, sock): self.sock = sock self.current_packet = "" self.current_packet_size = -1 self.current_packet_size_part = "" self.closed = 0 self.current_writing = "" def fileno(self): try: return self.sock.fileno() except: self.closed = 1
def export(self): Blender.Redraw() # Needed for GetRawFromObject print "setting file format to %s..." % self.file_format if self.file_format == "pickle": import cPickle as pickle soya.set_file_format(pickle.dumps) elif self.file_format == "cerealizer": import cerealizer, soya.cerealizer4soya as cerealizer4soya soya.set_file_format(cerealizer.dumps) else: raise ValueError("Unsupported file format %s" % self.file_format) objs = Blender.Object.Get() if self.animation: for obj in objs: data = obj.getData() if (type(data) is Blender.Types.ArmatureType): scene = Blender.Scene.getCurrent() #armature = Blender.Armature.Get()[0] armature = obj animation = Blender.Armature.NLA.GetActions()[self.animation] animation.setActive(armature) scene.getRenderingContext().currentFrame(int(self.animation_time)) scene.makeCurrent() materials = [] already_warned = [] nb_points_and_lines = 0 soya.path.insert(0, self.path) # insert at index 0 => save in this path. root_world = soya.World() root_world.filename = self.filename for obj in objs: data = obj.getData() if (type(data) is Blender.Types.NMeshType) and data.faces: nmesh = Blender.NMesh.GetRawFromObject(obj.getName()) matrix = [[obj.mat[0][0], obj.mat[0][1], obj.mat[0][2], obj.mat[0][3]], [obj.mat[1][0], obj.mat[1][1], obj.mat[1][2], obj.mat[1][3]], [obj.mat[2][0], obj.mat[2][1], obj.mat[2][2], obj.mat[2][3]], [obj.mat[3][0], obj.mat[3][1], obj.mat[3][2], obj.mat[3][3]]] for face in nmesh.faces: if (not self.keep_points_and_lines) and (len(face.v) <= 2): nb_points_and_lines += 1 continue f = soya.Face(root_world) # face option if(face.smooth != 0): f.smooth_lit = 1 if(face.mode & Blender.NMesh.FaceModes.TWOSIDE): f.double_sided = 1 # vertices index = 0 for vertex in face.v: # vertex coordinates co = pointbymatrix(vertex.co, matrix) v = soya.Vertex(root_world, co[0], co[1], co[2]) # vertex color if (face.col != None) and (len(face.col) > 0): color = face.col[index] v.color = (color.r / 255.0, color.g / 255.0, color.b / 255.0, color.a / 255.0) # vertex texture coordinates if len(face.uv) > 0: uv = face.uv[index] v.tex_x = uv[0] v.tex_y = 1.0 - uv[1] f.append(v) index = index + 1 # material # check for a material with the same name that the image UV texture file # (without extension). if(face.image != None): image_filename = face.image.name if "." in image_filename: material_filename = image_filename[:image_filename.rfind(".")] material_filename = self.materials_map.get(material_filename, material_filename) f.material = get_material(material_filename) if nb_points_and_lines: print "blender2soya.py: removing %s points and lines..." % nb_points_and_lines # Soya has different axis conventions root_world.rotate_x(-90.0) # Ensure quad's vertices are coplanar, and split any bugous quads nb_non_coplanar_quads = facecutter.check_quads(root_world) if nb_non_coplanar_quads: print "blender2soya.py: splitting %s non coplanar quads..." % nb_non_coplanar_quads if self.scale != 1.0: root_world.scale(self.scale, self.scale, self.scale) if self.cellshading: root_world.model_builder = soya.CellShadingModelBuilder( shader = (self.cellshading_shader and get_material(self.cellshading_shader)) or None, outline_color = self.cellshading_outline_color, outline_width = self.cellshading_outline_width, outline_attenuation = self.cellshading_outline_attenuation, ) if self.shadow: if not root_world.model_builder: root_world.model_builder = soya.SimpleModelBuilder() root_world.model_builder.shadow = 1 if self.max_face_angle != 80.0: if not root_world.model_builder: root_world.model_builder = soya.SimpleModelBuilder() root_world.model_builder.max_face_angle = self.max_face_angle if self.launch_editor: self.edit(root_world, *materials) else: for material in materials: print "blender2soya.py: exporting material %s..." % material.filename material.save() print "blender2soya.py: exporting world %s..." % root_world.filename root_world.save()