Advertisement
Guest User

g3d_import.py

a guest
Sep 17th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.72 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. ###########################################################################
  4. # Glest Model / Texture / UV / Animation Importer and Exporter
  5. # for the Game Glest that u can find http://www.glest.org
  6. # 2011/05/25: v0.1 alpha1
  7. # created by William Zheng for Blender 2.57(loveheaven_zhengwei@hotmail.com)
  8. #"""
  9. #Here an explanation of the V4 Format found at www.glest.org
  10. #================================
  11. #1. DATA TYPES
  12. #================================
  13. #G3D files use the following data types:
  14. #uint8: 8 bit unsigned integer
  15. #uint16: 16 bit unsigned integer
  16. #uint32: 32 bit unsigned integer
  17. #float32: 32 bit floating point
  18. #================================
  19. #2. OVERALL STRUCTURE
  20. #================================
  21. #- File header
  22. #- Model header
  23. #- Mesh header
  24. #- Texture names
  25. #- Mesh data
  26. #================================
  27. #2. FILE HEADER
  28. #================================
  29. #Code:
  30. #struct FileHeader{
  31. #   uint8 id[3];
  32. #   uint8 version;
  33. #};
  34. #This header is shared among all the versions of G3D, it identifies this file as a G3D model and provides information of the version.
  35. #id: must be "G3D"
  36. #version: must be 4, in binary (not '4')
  37. #================================
  38. #3. MODEL HEADER
  39. #================================
  40. #Code:
  41. #struct ModelHeader{
  42. #   uint16 meshCount;
  43. #   uint8 type;
  44. #};        
  45. #meshCount: number of meshes in this model
  46. #type: must be 0
  47. #================================
  48. #4. MESH HEADER
  49. #================================
  50. #There is a mesh header for each mesh, there must be "meshCount" headers in a file but they are not consecutive, texture names and mesh data are stored in between.
  51. #Code:
  52. #struct MeshHeader{
  53. #   uint8 name[64];
  54. #   uint32 frameCount;
  55. #   uint32 vertexCount;
  56. #   uint32 indexCount;
  57. #   float32 diffuseColor[3];
  58. #   float32 specularColor[3];
  59. #   float32 specularPower;
  60. #   float32 opacity;
  61. #   uint32 properties;
  62. #   uint32 textures;
  63. #};
  64. #name: name of the mesh
  65. #frameCount: number of keyframes in this mesh
  66. #vertexCount: number of vertices in each frame
  67. #indexCount: number of indices in this mesh (the number of triangles is indexCount/3)
  68. #diffuseColor: RGB diffuse color
  69. #specularColor: RGB specular color (currently unused)
  70. #specularPower: specular power (currently unused)
  71. ##properties: property flags
  72. #Code:
  73. #enum MeshPropertyFlag{
  74. #   mpfTwoSided= 1,
  75. #       mpfCustomColor= 2,
  76. #};
  77. #mpfTwoSided: meshes in this mesh are rendered by both sides, if this flag is not present only "counter clockwise" faces are rendered
  78. #mpfCustomColor: alpha in this model is replaced by a custom color, usually the player color
  79. #textures: texture flags, only 0x1 is currently used, indicating that there is a diffuse texture in this mesh.
  80. #================================
  81. #4. TEXTURE NAMES
  82. #================================
  83. #A list of uint8[64] texture name values. One for each texture in the mesh. If there are no textures in the mesh no texture names are present. In practice since Glest only uses 1 texture for each mesh the number of texture names should be 0 or 1.
  84. #================================
  85. #5. MESH DATA
  86. #================================
  87. #After each mesh header and texture names the mesh data is placed.
  88. #vertices: frameCount * vertexCount * 3, float32 values representing the x, y, z vertex coords for all frames
  89. #normals: frameCount * vertexCount * 3, float32 values representing the x, y, z normal coords for all frames
  90. #texture coords: vertexCount * 2, float32 values representing the s, t tex coords for all frames (only present if the mesh has 1 texture at least)
  91. #indices: indexCount, uint32 values representing the indices
  92. ###########################################################################
  93.  
  94. bl_info = {
  95.     "name": "G3D Mesh Importer",
  96.     "author": "William Zheng (corrected by MrPostiga)",
  97.     "version": (0, 1, 1),
  98.     "blender": (2, 5, 9),
  99.     "api": 36079,
  100.     "location": "File > Import > Glest 3D File (.g3d)",
  101.     "description": "Import .g3d file and create a mesh",
  102.     "warning": "",
  103.     "wiki_url": "",
  104.     "tracker_url": "",
  105.     "category": "Import-Export"}
  106. ###########################################################################
  107. # Importing Structures needed (must later verify if i need them really all)
  108. ###########################################################################
  109. import bpy
  110. from bpy.props import StringProperty
  111. from bpy_extras.image_utils import load_image
  112. from bpy_extras.io_utils import ImportHelper, ExportHelper
  113.  
  114. import sys, struct, string, types
  115. from types import *
  116. import os
  117. from os import path
  118. from os.path import dirname
  119. ###########################################################################
  120. # Variables that are better Global to handle
  121. ###########################################################################
  122. imported = []   #List of all imported Objects
  123. toexport = []   #List of Objects to export (actually only meshes)
  124. sceneID  = None #Points to the active Blender Scene
  125. def unpack_list(list_of_tuples):
  126.     l = []
  127.     for t in list_of_tuples:
  128.         l.extend(t)
  129.     return l
  130. ###########################################################################
  131. # Declaring Structures of G3D Format
  132. ###########################################################################
  133. class G3DHeader:                            #Read first 4 Bytes of file should be G3D + Versionnumber
  134.     binary_format = "<3cB"
  135.     def __init__(self, fileID):
  136.         temp = fileID.read(struct.calcsize(self.binary_format))
  137.         data = struct.unpack(self.binary_format,temp)
  138.         self.id = str(data[0]+data[1]+data[2], "utf-8")
  139.         self.version = data[3]
  140.  
  141. class G3DModelHeaderv3:                  #Read Modelheader in V3 there is only the number of Meshes in file
  142.     binary_format = "<I"
  143.     def __init__(self, fileID):
  144.         temp = fileID.read(struct.calcsize(self.binary_format))
  145.         data = struct.unpack(self.binary_format,temp)
  146.         self.meshcount = data[0]
  147.  
  148. class G3DModelHeaderv4:                  #Read Modelheader: Number of Meshes and Meshtype (must be 0)
  149.     binary_format = "<HB"
  150.     def __init__(self, fileID):
  151.         temp = fileID.read(struct.calcsize(self.binary_format))
  152.         data = struct.unpack(self.binary_format,temp)
  153.         self.meshcount = data[0]
  154.         self.mtype = data[1]
  155.        
  156. class G3DMeshHeaderv3:                                       #Read Meshheader
  157.     binary_format = "<7I64c"
  158.     def __init__(self,fileID):
  159.         temp = fileID.read(struct.calcsize(self.binary_format))
  160.         data = struct.unpack(self.binary_format,temp)
  161.         self.framecount = data[0]                #Framecount = Number of Animationsteps
  162.         self.normalframecount= data[1]       #Number of Normal Frames actualli equal to Framecount
  163.         self.texturecoordframecount= data[2]#Number of Frames of Texturecoordinates seems everytime to be 1
  164.         self.colorframecount= data[3]         #Number of Frames of Colors seems everytime to be 1
  165.         self.vertexcount= data[4]                 #Number of Vertices in each Frame
  166.         self.indexcount= data[5]                       #Number of Indices in Mesh (Triangles = Indexcount/3)
  167.         self.properties= data[6]                       #Property flags
  168.         if self.properties & 1:                   #PropertyBit is Mesh Textured ?
  169.             self.hastexture = False
  170.             self.texturefilename = None
  171.         else:
  172.             self.texturefilename = "".join([str(x, "ascii") for x in data[7:-1] if x[0]< 127 ])
  173.             self.hastexture = True
  174.         if self.properties & 2:                   #PropertyBit is Mesh TwoSided ?
  175.             self.istwosided = True
  176.         else:
  177.             self.istwosided = False
  178.         if self.properties & 4:                   #PropertyBit is Mesh Alpha Channel custom Color in Game ?
  179.             self.customalpha = True
  180.         else:
  181.             self.customalpha = False
  182.        
  183. class G3DMeshHeaderv4:                                       #Read Meshheader
  184.     binary_format = "<64c3I8f2I"
  185.     texname_format = "<64c"
  186.     def __init__(self,fileID):
  187.         temp = fileID.read(struct.calcsize(self.binary_format))
  188.         data = struct.unpack(self.binary_format,temp)
  189.         self.meshname = "".join([str(x, "ascii") for x in data[0:64] if x[0] < 127 ]) #Name of Mesh every Char is a  String on his own
  190.         self.framecount = data[64]              #Framecount = Number of Animationsteps
  191.         self.vertexcount = data[65]            #Number of Vertices in each Frame
  192.         self.indexcount = data[66]              #Number of Indices in Mesh (Triangles = Indexcount/3)
  193.         self.diffusecolor = data[67:70]    #RGB diffuse color
  194.         self.specularcolor = data[70:73]      #RGB specular color (unused)
  195.         self.specularpower = data[73]        #Specular power (unused)
  196.         self.opacity = data[74]                    #Opacity
  197.         self.properties= data[75]                 #Property flags
  198.         self.textures = data[76]                      #Texture flag
  199.         if self.properties & 1:                   #PropertyBit is Mesh TwoSided ?
  200.             self.istwosided = True
  201.         else:
  202.             self.istwosided = False
  203.         if self.properties & 2:                   #PropertyBit is Mesh Alpha Channel custom Color in Game ?
  204.             self.customalpha = True
  205.         else:
  206.             self.customalpha = False
  207.         if self.textures & 1:                       #PropertyBit is Mesh Textured ?
  208.             temp = fileID.read(struct.calcsize(self.texname_format))
  209.             data = struct.unpack(self.texname_format,temp)
  210.             self.texturefilename = "".join([str(x, "ascii") for x in data[0:-1] if x[0] < 127 ])
  211.             self.hastexture = True
  212.         else:
  213.             self.hastexture = False
  214.             self.texturefilename = None
  215.  
  216. class G3DMeshdataV3:                                               #Calculate and read the Mesh Datapack
  217.     def __init__(self,fileID,header):
  218.         #Calculation of the Meshdatasize to load because its variable
  219.         #Animationframes * Vertices per Animation * 3 (Each Point are 3 Float X Y Z Coordinates)
  220.         vertex_format = "<%if" % int(header.framecount * header.vertexcount * 3)
  221.         #The same for Normals
  222.         normals_format = "<%if" % int(header.normalframecount * header.vertexcount * 3)
  223.         #Same here but Textures are 2D so only 2 Floats needed for Position inside Texture Bitmap
  224.         texturecoords_format = "<%if" % int(header.texturecoordframecount * header.vertexcount * 2)
  225.         #Colors in format RGBA
  226.         colors_format = "<%if" % int(header.colorframecount * 4)
  227.         #Indices
  228.         indices_format = "<%iI" % int(header.indexcount)
  229.         #Load the Meshdata as calculated above
  230.         self.vertices = struct.unpack(vertex_format,fileID.read(struct.calcsize(vertex_format)))
  231.         self.normals = struct.unpack(normals_format,fileID.read(struct.calcsize(normals_format)))
  232.         self.texturecoords = struct.unpack(texturecoords_format,fileID.read(struct.calcsize(texturecoords_format)))
  233.         self.colors = struct.unpack(colors_format,fileID.read(struct.calcsize(colors_format)))
  234.         self.indices = struct.unpack(indices_format ,fileID.read(struct.calcsize(indices_format)))
  235.  
  236. class G3DMeshdataV4:                                               #Calculate and read the Mesh Datapack
  237.     def __init__(self,fileID,header):
  238.         #Calculation of the Meshdatasize to load because its variable
  239.         #Animationframes * Points (Vertex) per Animation * 3 (Each Point are 3 Float X Y Z Coordinates)
  240.         vertex_format = "<%if" % int(header.framecount * header.vertexcount * 3)
  241.         #The same for Normals
  242.         normals_format = "<%if" % int(header.framecount * header.vertexcount * 3)
  243.         #Same here but Textures are 2D so only 2 Floats needed for Position inside Texture Bitmap
  244.         texturecoords_format = "<%if" % int(header.vertexcount * 2)
  245.         #Indices
  246.         indices_format = "<%iI" % int(header.indexcount)
  247.         #Load the Meshdata as calculated above
  248.         self.vertices = struct.unpack(vertex_format,fileID.read(struct.calcsize(vertex_format)))
  249.         self.normals = struct.unpack(normals_format,fileID.read(struct.calcsize(normals_format)))
  250.         if header.hastexture:
  251.             self.texturecoords = struct.unpack(texturecoords_format,fileID.read(struct.calcsize(texturecoords_format)))
  252.         self.indices = struct.unpack(indices_format ,fileID.read(struct.calcsize(indices_format)))
  253.  
  254. def createMesh(filename,header,data):                         #Create a Mesh inside Blender
  255.     mesh = bpy.data.meshes.new(header.meshname)     #New Mesh
  256.     meshobj = bpy.data.objects.new(header.meshname+'Object', mesh)   #New Object for the new Mesh
  257.     scene = bpy.context.scene
  258.     scene.objects.link(meshobj)
  259.     scene.update()
  260.     uvcoords = []
  261.     image = None
  262.     if header.hastexture:                                                 #Load Texture when assigned
  263.         texturefile = dirname(filename)+os.sep+header.texturefilename
  264.         image = bpy.data.images.load(texturefile) #load_image(texturefile, dirname(filename))
  265.         for x in range(0,len(data.texturecoords),2): #Prepare the UV
  266.             uvcoords.append([data.texturecoords[x],data.texturecoords[x+1]])
  267.    
  268.     vertsCO = []
  269.     vertsNormal = []
  270.     for x in range(0,header.vertexcount*3,3):      #Get the Vertices and Normals into empty Mesh
  271.         vertsCO.extend([(data.vertices[x],data.vertices[x+1],data.vertices[x+2])])
  272.         vertsNormal.extend([(data.normals[x],data.normals[x+1],data.normals[x+2])])
  273.         #vertsCO.extend([(data.vertices[x+(header.framecount-1)*header.vertexcount*3],data.vertices[x+(header.framecount-1)*header.vertexcount*3+1],data.vertices[x+(header.framecount-1)*header.vertexcount*3+2])])
  274.         #vertsNormal.extend([(data.normals[x+(header.framecount-1)*header.vertexcount*3],data.normals[x+(header.framecount-1)*header.vertexcount*3+1],data.normals[x+(header.framecount-1)*header.vertexcount*3+2])])
  275.     mesh.vertices.add(len(vertsCO))
  276.     mesh.vertices.foreach_set("co", unpack_list(vertsCO))
  277.     mesh.vertices.foreach_set("normal", unpack_list(vertsNormal))
  278.    
  279.     faces = []
  280.     faceuv = []
  281.     for i in range(0,len(data.indices),3):            #Build Faces into Mesh
  282.         faces.extend([data.indices[i], data.indices[i+1], data.indices[i+2], 0])
  283.         if header.hastexture:      
  284.             uv = []
  285.             u0 = uvcoords[data.indices[i]][0]
  286.             v0 = uvcoords[data.indices[i]][1]
  287.             uv.append([u0,v0])
  288.             u1 = uvcoords[data.indices[i+1]][0]
  289.             v1 = uvcoords[data.indices[i+1]][1]
  290.             uv.append([u1,v1])
  291.             u2 = uvcoords[data.indices[i+2]][0]
  292.             v2 = uvcoords[data.indices[i+2]][1]
  293.             uv.append([u2,v2])
  294.             faceuv.append([uv,0,0,0])
  295.         else:
  296.             uv.append([0,0])
  297.             uv.append([0,0])
  298.             uv.append([0,0])
  299.             faceuv.append([uv,0,0,0])
  300.     mesh.faces.add(len(faces)//4)
  301.     mesh.faces.foreach_set("vertices_raw", faces)
  302.     mesh.faces.foreach_set("use_smooth", [True] * len(mesh.faces))
  303.     mesh.update_tag()
  304.     mesh.update()                                                                  #Update changes to Mesh
  305.     #===================================================================================================
  306. #Material Setup
  307. #===================================================================================================
  308.     if header.hastexture:      
  309.         materialname = "pskmat"
  310.         materials = []
  311.         texture = bpy.data.textures.new(name=header.texturefilename,type='IMAGE')
  312.         texture.image = image
  313.         matdata = bpy.data.materials.new(materialname + '1')
  314.         slot = matdata.texture_slots.add()
  315.         slot.texture = texture
  316.         slot.texture_coords = 'UV'
  317.         if header.isv4:
  318.             matdata.diffuse_color = (header.diffusecolor[0], header.diffusecolor[1],header.diffusecolor[2])
  319.             matdata.alpha = header.opacity
  320.             matdata.specular_color = (header.specularcolor[0], header.specularcolor[1],header.specularcolor[2])
  321.         materials.append(matdata)
  322.  
  323.         for material in materials:
  324.             #add material to the mesh list of materials
  325.             mesh.materials.append(material)
  326.  
  327.         countm = 0
  328.         psktexname="psk" + str(countm)
  329.         mesh.uv_textures.new(name=psktexname)
  330.         for countm in range(len(mesh.uv_textures)):
  331.             mesh.update()
  332.             uvtex = mesh.uv_textures[countm] #add one uv texture
  333.             mesh.update()
  334.             if (len(faceuv) > 0):
  335.                 counttex = 0
  336.                 countm = 0
  337.                 for countm in range(len(mesh.uv_textures)):
  338.                     mesh.update()
  339.                     psktexname="psk" + str(countm)
  340.                     uvtex = mesh.uv_textures[countm] #add one uv texture
  341.                     mesh.update()
  342.                     for i, face in enumerate(mesh.faces):
  343.                         blender_tface = uvtex.data[i] #face
  344.                         mfaceuv = faceuv[i]
  345.                         #face.material_index = faceuv[i][1]
  346.                         if countm == faceuv[i][1]:
  347.                             face.material_index = faceuv[i][1]
  348.                             blender_tface.uv1 = mfaceuv[0][0] #uv = (0,0)
  349.                             blender_tface.uv2 = mfaceuv[0][1] #uv = (0,0)
  350.                             blender_tface.uv3 = mfaceuv[0][2] #uv = (0,0)
  351.                             blender_tface.image = image
  352.                             blender_tface.use_image = True
  353.                             blender_tface.blend_type = 'ALPHA'
  354.                             blender_tface.use_twoside = True
  355.                         else:
  356.                             blender_tface.uv1 = [0,0]
  357.                             blender_tface.uv2 = [0,0]
  358.                             blender_tface.uv3 = [0,0]
  359.     mesh.update()
  360.     imported.append(meshobj)                        #Add to Imported Objects
  361.     for x in range(header.framecount):                    #Put in Vertex Positions for Keyanimation
  362.         print("Frame"+str(x))
  363.         for i in range(0,header.vertexcount*3,3):
  364.             print(str(i//3)+':\t'+str(data.vertices[x*header.vertexcount*3 + i])+'\t'+
  365.                 str(data.vertices[x*header.vertexcount*3 + i +1])+'\t'+
  366.                 str(data.vertices[x*header.vertexcount*3 + i +2]))
  367.             mesh.vertices[i//3].co[0]= data.vertices[x*header.vertexcount*3 + i]
  368.             mesh.vertices[i//3].co[1]= data.vertices[x*header.vertexcount*3 + i +1]
  369.             mesh.vertices[i//3].co[2]= data.vertices[x*header.vertexcount*3 + i +2]
  370.         meshobj.keyframe_insert("location",-1, frame=(x+1))
  371.     mesh.update()
  372.     return
  373. ###########################################################################
  374. # Import
  375. ###########################################################################
  376. def G3DLoader(filepath):            #Main Import Routine
  377.     global imported, sceneID
  378.     print ("\nNow Importing File: " + filepath)
  379.     fileID = open(filepath,"rb")
  380.     header = G3DHeader(fileID)
  381.     print ("\nHeader ID         : " + header.id)
  382.     print ("Version           : " + str(header.version))
  383.     if header.id != "G3D":
  384.         print ("This is Not a G3D Model File")
  385.         fileID.close
  386.         return
  387.     if header.version not in (3, 4):
  388.         print ("The Version of this G3D File is not Supported")
  389.         fileID.close
  390.         return
  391.     #in_editmode = Blender.Window.EditMode()             #Must leave Editmode when active
  392.     #if in_editmode: Blender.Window.EditMode(0)
  393.     sceneID = bpy.context.scene                       #Get active Scene
  394.     #scenecontext=sceneID.getRenderingContext()       #To Access the Start/Endframe its so hidden i searched till i got angry :-)
  395.     basename=os.path.basename(filepath).split('.')[0]   #Generate the Base Filename without Path + extension
  396.     imported = []
  397.     maxframe=0
  398.     if header.version == 3:
  399.         modelheader = G3DModelHeaderv3(fileID)
  400.         print ("Number of Meshes  : " + str(modelheader.meshcount))
  401.         for x in range(modelheader.meshcount):
  402.             meshheader = G3DMeshHeaderv3(fileID)
  403.             meshheader.isv4 = False
  404.             print ("\nMesh Number         : " + str(x+1))
  405.             print ("framecount            : " + str(meshheader.framecount))
  406.             print ("normalframecount      : " + str(meshheader.normalframecount))
  407.             print ("texturecoordframecount: " + str(meshheader.texturecoordframecount))
  408.             print ("colorframecount       : " + str(meshheader.colorframecount))
  409.             print ("pointcount            : " + str(meshheader.vertexcount))
  410.             print ("indexcount            : " + str(meshheader.indexcount))
  411.             print ("texturename           : " + str(meshheader.texturefilename))
  412.             print ("hastexture            : " + str(meshheader.hastexture))
  413.             print ("istwosided            : " + str(meshheader.istwosided))
  414.             print ("customalpha           : " + str(meshheader.customalpha))
  415.             meshheader.meshname = basename+str(x+1)  #Generate Meshname because V3 has none
  416.             if meshheader.framecount > maxframe: maxframe = meshheader.framecount #Evaluate the maximal animationsteps
  417.             meshdata = G3DMeshdataV3(fileID,meshheader)
  418.             createMesh(filepath,meshheader,meshdata)
  419.         fileID.close
  420.         bpy.context.scene.frame_start=1
  421.         bpy.context.scene.frame_end=maxframe
  422.         bpy.context.scene.frame_current=1
  423.         anchor = bpy.data.objects.new('Empty', None)
  424.         anchor.select = True
  425.         bpy.context.scene.objects.link(anchor)
  426.         for ob in imported:
  427.                 ob.parent = anchor
  428.         bpy.context.scene.update()
  429.         return
  430.     if header.version == 4:
  431.         modelheader = G3DModelHeaderv4(fileID)
  432.         print ("Number of Meshes  : " + str(modelheader.meshcount))
  433.         for x in range(modelheader.meshcount):
  434.             meshheader = G3DMeshHeaderv4(fileID)
  435.             meshheader.isv4 = True   
  436.             print ("\nMesh Number   : " + str(x+1))
  437.             print ("meshname        : " + str(meshheader.meshname))
  438.             print ("framecount      : " + str(meshheader.framecount))
  439.             print ("vertexcount     : " + str(meshheader.vertexcount))
  440.             print ("indexcount      : " + str(meshheader.indexcount))
  441.             print ("diffusecolor    : %1.6f %1.6f %1.6f" %meshheader.diffusecolor)
  442.             print ("specularcolor   : %1.6f %1.6f %1.6f" %meshheader.specularcolor)
  443.             print ("specularpower   : %1.6f" %meshheader.specularpower)
  444.             print ("opacity         : %1.6f" %meshheader.opacity)
  445.             print ("properties      : " + str(meshheader.properties))
  446.             print ("textures        : " + str(meshheader.textures))
  447.             print ("texturename     : " + str(meshheader.texturefilename))
  448.             if len(meshheader.meshname) ==0:    #When no Meshname in File Generate one
  449.                     meshheader.meshname = basename+str(x+1)
  450.             if meshheader.framecount > maxframe: maxframe = meshheader.framecount #Evaluate the maximal animationsteps
  451.             meshdata = G3DMeshdataV4(fileID,meshheader)
  452.             createMesh(filepath,meshheader,meshdata)
  453.         fileID.close
  454.        
  455.         bpy.context.scene.frame_start=1
  456.         bpy.context.scene.frame_end=maxframe
  457.         bpy.context.scene.frame_current=1
  458.         anchor = bpy.data.objects.new('Empty', None)
  459.         anchor.select = True
  460.         bpy.context.scene.objects.link(anchor)
  461.         for ob in imported:
  462.                 ob.parent = anchor
  463.         bpy.context.scene.update()
  464.         print ("Created a empty Object as 'Grip' where all imported Objects are parented to")
  465.         print ("To move the complete Meshes only select this empty Object and move it")
  466.         print ("All Done, have a good Day :-)\n\n")
  467.         return
  468.  
  469.  
  470. #---=== Register ===
  471. class ImportG3D(bpy.types.Operator, ImportHelper):
  472.     '''Load a G3D file'''
  473.     bl_idname = "importg3d.g3d"
  474.     bl_label = "Import G3D"
  475.  
  476.     filename_ext = ".g3d"
  477.     filter_glob = StringProperty(default="*.g3d", options={'HIDDEN'})
  478.  
  479.     def execute(self, context):
  480.         try:
  481.             G3DLoader(**self.as_keywords(ignore=("filter_glob",)))
  482.         except:
  483.             import traceback
  484.             traceback.print_exc()
  485.  
  486.             return {'CANCELLED'}
  487.  
  488.         return {'FINISHED'}
  489.  
  490.  
  491. def menu_func_import(self, context):
  492.     self.layout.operator(ImportG3D.bl_idname, text="Glest 3D File (.g3d)")
  493.  
  494. def register():
  495.     bpy.utils.register_module(__name__)
  496.  
  497.     bpy.types.INFO_MT_file_import.append(menu_func_import)
  498.  
  499.  
  500. def unregister():
  501.     bpy.utils.unregister_module(__name__)
  502.  
  503.     bpy.types.INFO_MT_file_import.remove(menu_func_import)
  504.  
  505. if __name__ == '__main__':
  506.     register()
  507. #   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement