diff options
author | Andre Susano Pinto <andresusanopinto@gmail.com> | 2008-06-21 01:36:15 +0400 |
---|---|---|
committer | Andre Susano Pinto <andresusanopinto@gmail.com> | 2008-06-21 01:36:15 +0400 |
commit | ffbe42129e0d043214daf9b07125d9d8140d1c74 (patch) | |
tree | 6c99776cfcfadd32347eacb287c69deaa1b35651 /release | |
parent | 824eac5095924ffdc1fea213a925054c88bf05d7 (diff) |
svn merge -r 15202:15292 https://svn.blender.org/svnroot/bf-blender/trunk/blender
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/ms3d_import.py | 132 | ||||
-rw-r--r-- | release/scripts/vrml97_export.py | 80 |
2 files changed, 175 insertions, 37 deletions
diff --git a/release/scripts/ms3d_import.py b/release/scripts/ms3d_import.py index 78ffbb92847..c1438cbfc97 100644 --- a/release/scripts/ms3d_import.py +++ b/release/scripts/ms3d_import.py @@ -50,7 +50,7 @@ def RM(a): cp = cos(a[1]) sr = sin(a[0]) cr = cos(a[0]) - return Matrix([cp*cy, sr*sp*cy+cr*-sy, cr*sp*cy+-sr*-sy],[cp*sy, sr*sp*sy+cr*cy, cr*sp*sy+-sr*cy], [-sp, sr*cp, cr*cp]) + return Matrix([cp*cy, cp*sy, -sp], [sr*sp*cy+cr*-sy, sr*sp*sy+cr*cy, sr*cp],[cr*sp*cy+-sr*-sy, cr*sp*sy+-sr*cy, cr*cp]) # Converts ms3d euler angles to a quaternion @@ -94,7 +94,12 @@ def import_ms3d(path): except IOError: return "Failed to open the file!" - # read id + # get the file size + file.seek(0, os.SEEK_END); + fileSize = file.tell(); + file.seek(0, os.SEEK_SET); + + # read id to check if the file is a MilkShape3D file id = file.read(10) if id!="MS3D000000": return "The file is not a MS3D file!" @@ -123,7 +128,7 @@ def import_ms3d(path): coords.append(struct.unpack("fff", file.read(3*4))) # read bone ids - boneIds.append(struct.unpack("B", file.read(1))[0]) + boneIds.append(struct.unpack("b", file.read(1))[0]) # skip refcount file.read(1) @@ -190,9 +195,10 @@ def import_ms3d(path): triangleIndices = struct.unpack(str(numGroupTriangles) + "H", file.read(2*numGroupTriangles)); # read material - material = struct.unpack("B", file.read(1))[0] - for j in xrange(numGroupTriangles): - mesh.faces[triangleIndices[j]].mat = material + material = struct.unpack("b", file.read(1))[0] + if material>=0: + for j in xrange(numGroupTriangles): + mesh.faces[triangleIndices[j]].mat = material # read the number of materials numMaterials = struct.unpack("H", file.read(2))[0] @@ -224,7 +230,6 @@ def import_ms3d(path): # read shininess shininess = struct.unpack("f", file.read(4))[0] - print "Shininess: " + str(shininess) # read transparency transparency = struct.unpack("f", file.read(4))[0] @@ -272,6 +277,7 @@ def import_ms3d(path): armature.makeEditable() # read joints + joints = [] rotKeys = {} posKeys = {} for i in xrange(numJoints): @@ -280,6 +286,7 @@ def import_ms3d(path): # read name name = uku(file.read(32)) + joints.append(name) # create the bone bone = Blender.Armature.Editbone() @@ -295,11 +302,13 @@ def import_ms3d(path): # read position pos = struct.unpack("fff", file.read(3*4)) - + # set head if bone.hasParent(): - bone.head = bone.parent.matrix * Vector(pos) + bone.parent.head - bone.matrix = bone.parent.matrix * RM(rot) + bone.head = Vector(pos) * bone.parent.matrix + bone.parent.head + tempM = RM(rot) * bone.parent.matrix + tempM.transpose; + bone.matrix = tempM else: bone.head = Vector(pos) bone.matrix = RM(rot) @@ -355,13 +364,111 @@ def import_ms3d(path): # create position keys for key in posKeys[name]: pbone.loc = Vector(key[1]) - pbone.insertKey(armOb, int(key[0]), Blender.Object.Pose.LOC, True) + pbone.insertKey(armOb, int(key[0]+0.5), Blender.Object.Pose.LOC, True) # create rotation keys for key in rotKeys[name]: pbone.quat = RQ(key[1]) - pbone.insertKey(armOb, int(key[0]), Blender.Object.Pose.ROT, True) + pbone.insertKey(armOb, int(key[0]+0.5), Blender.Object.Pose.ROT, True) + + # The old format ends here. If there is more data then the file is newer version + + # check to see if there are any comments + if file.tell()<fileSize: + + # read sub version + subVersion = struct.unpack("i", file.read(4))[0] + + # Is the sub version a supported one + if subVersion==1: + + # Group comments + numComments = struct.unpack("i", file.read(4))[0] + for i in range(numComments): + file.read(4) # index + size = struct.unpack("i", file.read(4))[0] # comment size + if size>0: + print "Group comment: " + file.read(size) + + # Material comments + numComments = struct.unpack("i", file.read(4))[0] + for i in range(numComments): + file.read(4) # index + size = struct.unpack("i", file.read(4))[0] # comment size + if size>0: + print "Material comment: " + file.read(size) + + # Joint comments + numComments = struct.unpack("i", file.read(4))[0] + for i in range(numComments): + file.read(4) # index + size = struct.unpack("i", file.read(4))[0] # comment size + if size>0: + print "Joint comment: " + file.read(size) + + # Model comments + numComments = struct.unpack("i", file.read(4))[0] + for i in range(numComments): + file.read(4) # index + size = struct.unpack("i", file.read(4))[0] # comment size + if size>0: + print "Model comment: " + file.read(size) + + # Unknown version give a warning + else: + print "Warning: Unknown version!" + + + # check to see if there is any extra vertex data + if file.tell()<fileSize: + + # read the subversion + subVersion = struct.unpack("i", file.read(4))[0] + + # is the version supported + if subVersion==2: + # read the extra data for each vertex + for i in xrange(numVertices): + # bone ids + ids = struct.unpack("bbb", file.read(3)) + # weights + weights = struct.unpack("BBB", file.read(3)) + # extra + extra = struct.unpack("I", file.read(4)) + # add extra vertices with weights to deform groups + if ids[0]>=0 or ids[1]>=0 or ids[2]>=0: + mesh.assignVertsToGroup(joints[boneIds[i]], [i], 0.01*weights[0], 1) + if ids[0]>=0: + mesh.assignVertsToGroup(joints[ids[0]], [i], 0.01*weights[1], 1) + if ids[1]>=0: + mesh.assignVertsToGroup(joints[ids[1]], [i], 0.01*weights[2], 1) + if ids[2]>=0: + mesh.assignVertsToGroup(joints[ids[2]], [i], 0.01*(100-(weights[0]+weights[1]+weights[2])), 1) + + elif subVersion==1: + # read extra data for each vertex + for i in xrange(numVertices): + # bone ids + ids = struct.unpack("bbb", file.read(3)) + # weights + weights = struct.unpack("BBB", file.read(3)) + # add extra vertices with weights to deform groups + if ids[0]>=0 or ids[1]>=0 or ids[2]>=0: + mesh.assignVertsToGroup(joints[boneIds[i]], [i], 0.01*weights[0], 1) + if ids[0]>=0: + mesh.assignVertsToGroup(joints[ids[0]], [i], 0.01*weights[1], 1) + if ids[1]>=0: + mesh.assignVertsToGroup(joints[ids[1]], [i], 0.01*weights[2], 1) + if ids[2]>=0: + mesh.assignVertsToGroup(joints[ids[2]], [i], 0.01*(100-(weights[0]+weights[1]+weights[2])), 1) + + # non supported subversion give a warning + else: + print "Warning: Unknown subversion!" + # rest of the extra data in the file is not imported/used + + # refresh the view Blender.Redraw() # close the file @@ -378,4 +485,3 @@ def fileCallback(filename): Blender.Draw.PupMenu("An error occured during import: " + error + "|Not all data might have been imported succesfully.", 2) Blender.Window.FileSelector(fileCallback, 'Import') - diff --git a/release/scripts/vrml97_export.py b/release/scripts/vrml97_export.py index eb3be80c99c..b28c7f5bbdc 100644 --- a/release/scripts/vrml97_export.py +++ b/release/scripts/vrml97_export.py @@ -3,9 +3,6 @@ Name: 'VRML97 (.wrl)...' Blender: 241 Group: 'Export' -Submenu: 'All Objects...' all -Submenu: 'All Objects compressed...' comp -Submenu: 'Selected Objects...' selected Tooltip: 'Export to VRML97 file (.wrl)' """ @@ -55,7 +52,7 @@ want to export only selected or all relevant objects. import Blender from Blender import Object, Mesh, Lamp, Draw, BGL, \ - Image, Text, sys, Mathutils + Image, Text, sys, Mathutils, Registry from Blender.Scene import Render import math @@ -70,8 +67,9 @@ worldmat = Blender.Texture.Get() filename = Blender.Get('filename') _safeOverwrite = True extension = '' -ARG='' +# Matrices below are used only when export_rotate_z_to_y.val: +# # Blender is Z up, VRML is Y up, both are right hand coordinate # systems, so to go from Blender coords to VRML coords we rotate # by 90 degrees around the X axis. In matrix notation, we have a @@ -456,6 +454,8 @@ class VRML2Export: if mat: if (mat.mode & Blender.Material.Modes['VCOL_PAINT']): self.vcolors = 1 + else: + self.vcolors = 0 # check if object is wireframe only if ob.drawType == Blender.Object.DrawTypes.WIRE: @@ -633,8 +633,9 @@ class VRML2Export: meshVertexList = me.verts for vertex in meshVertexList: - blenvert = Mathutils.Vector(vertex.co) - vrmlvert = M_blen2vrml * blenvert + vrmlvert = blenvert = Mathutils.Vector(vertex.co) + if export_rotate_z_to_y.val: + vrmlvert = M_blen2vrml * vrmlvert self.writeUnindented("%s %s %s\n " % \ (vrmlvert[0], \ vrmlvert[1], \ @@ -730,8 +731,8 @@ class VRML2Export: round(uv[1], self.tp)) j=j+1 indexStr += "-1" - texIndexList.append(indexStr) - texCoordList.append(coordStr) + texIndexList.append(indexStr) + texCoordList.append(coordStr) self.writeIndented("texCoord TextureCoordinate {\n", 1) self.writeIndented("point [\n", 1) @@ -1016,7 +1017,10 @@ class VRML2Export: return ob_matrix = Mathutils.Matrix(ob.getMatrix('worldspace')) - matrix = M_blen2vrml * ob_matrix * M_vrml2blen + if export_rotate_z_to_y.val: + matrix = M_blen2vrml * ob_matrix * M_vrml2blen + else: + matrix = ob_matrix e = matrix.rotationPart().toEuler() v = matrix.translationPart() @@ -1089,7 +1093,7 @@ class VRML2Export: self.writeFog() self.proto = 0 allObj = [] - if ARG == 'selected': + if export_selection_only.val: allObj = list(scene.objects.context) else: allObj = list(scene.objects) @@ -1098,7 +1102,7 @@ class VRML2Export: for thisObj in allObj: self.writeObject(thisObj) - if ARG != 'selected': + if not export_selection_only.val: self.writeScript() self.cleanup() @@ -1213,26 +1217,54 @@ def select_file(filename): wrlexport=VRML2Export(filename) wrlexport.export(scene, world, worldmat) +######################################################### +# UI and Registry utilities +######################################################### + +export_selection_only = Draw.Create(0) +export_rotate_z_to_y = Draw.Create(1) +export_compressed = Draw.Create(0) + +def save_to_registry(): + d = {} + d['selection_only'] = export_selection_only.val + d['rotate_z_to_y'] = export_rotate_z_to_y.val + d['compressed'] = export_compressed.val + Registry.SetKey('vrml97_export', d, True) + +def load_from_registry(): + d = Registry.GetKey('vrml97_export', True) + if d: + try: + export_selection_only.val = d['selection_only'] + export_rotate_z_to_y.val = d['rotate_z_to_y'] + export_compressed.val = d['compressed'] + except: save_to_registry() # If data is not valid, rewrite it. + +def show_popup(): + pup_block = [ + ('Selection Only', export_selection_only, 'Only export objects in visible selection. Else export whole scene.'), + ('Rotate +Z to +Y', export_rotate_z_to_y, 'Rotate such that +Z axis (Blender up) becomes +Y (VRML up).'), + ('Compress', export_compressed, 'Generate a .wrz file (normal VRML compressed by gzip).') + ] + return Draw.PupBlock('Export VRML 97...', pup_block) ######################################################### # main routine ######################################################### -try: - ARG = __script__['arg'] # user selected argument -except: - print "older version" - -if Blender.Get('version') < 235: - print "Warning: VRML97 export failed, wrong blender version!" - print " You aren't running blender version 2.35 or greater" - print " download a newer version from http://blender3d.org/" -else: - if ARG == 'comp': +load_from_registry() + +# Note that show_popup must be done before Blender.Window.FileSelector, +# because export_compressed affects the suggested extension of resulting +# file. + +if show_popup(): + save_to_registry() + if export_compressed.val: extension=".wrz" from gzip import * else: extension=".wrl" Blender.Window.FileSelector(select_file, "Export VRML97", \ sys.makename(ext=extension)) - |