diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2014-04-09 15:33:37 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2014-04-09 15:33:37 +0400 |
commit | ee2cd7105d505a4f6b4613aba26598f532f97f30 (patch) | |
tree | ed50f3a8e7285101a4a3baf4b0d8a19164b19cc7 | |
parent | 6c32300be8cc8d48a3e02055dc43bea2ab98a525 (diff) |
Backport revisions for the 2.70a releasev2.70a
fa5cbcd, 95a9c96, 31bccec, c6e1af6.
-rw-r--r-- | add_mesh_extra_objects/add_mesh_teapot.py | 307 | ||||
-rw-r--r-- | io_scene_obj/export_obj.py | 4 |
2 files changed, 171 insertions, 140 deletions
diff --git a/add_mesh_extra_objects/add_mesh_teapot.py b/add_mesh_extra_objects/add_mesh_teapot.py index 2771dd73..dc4d575d 100644 --- a/add_mesh_extra_objects/add_mesh_teapot.py +++ b/add_mesh_extra_objects/add_mesh_teapot.py @@ -16,7 +16,7 @@ # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License @@ -34,44 +34,74 @@ bl_info = { "url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/Scripts/Add_Teapot", "category": "Add Mesh"} ''' -import bpy, mathutils, io, operator, functools + +import bpy +from bpy.props import IntProperty + +import mathutils + +import io +import operator +import functools + class AddTeapot(bpy.types.Operator): - """Add a teapot mesh""" - bl_idname = "mesh.primitive_teapot_add" - bl_label = "Add Teapot" - bl_options = {"REGISTER", "UNDO"} - - resolution = bpy.props.IntProperty(name="Resolution", - description="Resolution of the Teapot", - default=5, min=2, max=15) - - objecttype = bpy.props.IntProperty(name="Object Type", - description="Type of Bezier Object", - default=1, min=1, max=2) - - def execute(self, context): - verts, faces = make_teapot(self.objecttype, - self.resolution) - # Actually create the mesh object from this geometry data. - obj = create_mesh_object(context, verts, [], faces, "Teapot") - return {'FINISHED'} + """Add a teapot mesh""" + bl_idname = "mesh.primitive_teapot_add" + bl_label = "Add Teapot" + bl_options = {"REGISTER", "UNDO"} + + resolution = IntProperty( + name="Resolution", + description="Resolution of the Teapot", + default=5, min=2, max=15, + ) + objecttype = IntProperty( + name="Object Type", + description="Type of Bezier Object", + default=1, min=1, max=2) + + def execute(self, context): + verts, faces = make_teapot(self.objecttype, + self.resolution) + # Actually create the mesh object from this geometry data. + obj = create_mesh_object(context, verts, [], faces, "Teapot") + return {'FINISHED'} + def menu_func(self, context): - self.layout.operator(AddTeapot.bl_idname, text="Teapot+", icon="MESH_CUBE") + self.layout.operator(AddTeapot.bl_idname, text="Teapot+", icon="MESH_CUBE") + def register(): - bpy.utils.register_module(__name__) - bpy.types.INFO_MT_mesh_add.append(menu_func) + bpy.utils.register_module(__name__) + bpy.types.INFO_MT_mesh_add.append(menu_func) + def unregister(): - bpy.utils.unregister_module(__name__) - bpy.types.INFO_MT_mesh_add.remove(menu_func) + bpy.utils.unregister_module(__name__) + bpy.types.INFO_MT_mesh_add.remove(menu_func) if __name__ == "__main__": - register() + register() + + +def create_mesh_face_hack(faces): + # FIXME, faces with duplicate vertices shouldn't be created in the first place. + faces_copy = [] + for f in faces: + f_copy = [] + for i in f: + if i not in f_copy: + f_copy.append(i) + faces_copy.append(f_copy) + faces[:] = faces_copy + def create_mesh_object(context, verts, edges, faces, name): + + create_mesh_face_hack(faces) + # Create new mesh mesh = bpy.data.meshes.new(name) # Make a mesh from a list of verts/edges/faces. @@ -81,139 +111,140 @@ def create_mesh_object(context, verts, edges, faces, name): from bpy_extras import object_utils return object_utils.object_data_add(context, mesh, operator=None) + # ========================== # === Bezier patch Block === # ========================== def read_indexed_patch_file(filename): - file = io.StringIO(filename) - rawpatches = [] - patches = [] - numpatches = int(file.readline()) - for i in range(numpatches): - line = file.readline() - a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p = map(int, line.split(",")) - patches.append([[a,b,c,d], [e,f,g,h], [i,j,k,l], [m,n,o,p]]) - rawpatches.append([[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]]) - verts = [] - numverts = int(file.readline()) - for i in range(numverts): - line = file.readline() - v1,v2,v3 = map(float, line.split(",")) - verts.append((v1,v2,v3)) - for i in range(len(patches)): - for j in range(4): #len(patches[i])): - for k in range(4): #len(patches[i][j])): - index = patches[i][j][k] - 1 - rawpatches[i][j][k] = verts[index] - return rawpatches + file = io.StringIO(filename) + rawpatches = [] + patches = [] + numpatches = int(file.readline()) + for i in range(numpatches): + line = file.readline() + (a, b, c, d, + e, f, g, h, + i, j, k, l, + m, n, o, p, + ) = map(int, line.split(",")) + patches.append([[a, b, c, d], [e, f, g, h], [i, j, k, l], [m, n, o, p]]) + rawpatches.append([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) + verts = [] + numverts = int(file.readline()) + for i in range(numverts): + line = file.readline() + v1, v2, v3 = map(float, line.split(",")) + verts.append((v1, v2, v3)) + for i in range(len(patches)): + for j in range(4): # len(patches[i])): + for k in range(4): # len(patches[i][j])): + index = patches[i][j][k] - 1 + rawpatches[i][j][k] = verts[index] + return rawpatches + def patches_to_raw(patches, resolution): - raw = [] - for patch in patches: - verts = make_verts(patch, resolution) - faces = make_faces(resolution) - rawquads = indexed_to_rawquads(verts, faces) - raw.append(rawquads) - raw = functools.reduce(operator.add, raw) # flatten the list - return raw + raw = [] + for patch in patches: + verts = make_verts(patch, resolution) + faces = make_faces(resolution) + rawquads = indexed_to_rawquads(verts, faces) + raw.append(rawquads) + raw = functools.reduce(operator.add, raw) # flatten the list + return raw -def make_bezier(ctrlpnts, resolution): - b1 = lambda t: t*t*t - b2 = lambda t: 3*t * t * (1-t) - b3 = lambda t: 3*t * (1-t) * (1-t) - b4 = lambda t: (1-t) * (1-t) * (1-t) - makevec = lambda v: mathutils.Vector(v) - p1,p2,p3,p4 = map(makevec, ctrlpnts) - curveverts = [] - for i in range(resolution+1): - t = i/resolution - x,y,z = b1(t)*p1 + b2(t)*p2 + b3(t)*p3 + b4(t)*p4 - curveverts.append((x,y,z)) - return curveverts def make_bezier(ctrlpnts, resolution): - b1 = lambda t: t*t*t - b2 = lambda t: 3*t * t * (1-t) - b3 = lambda t: 3*t * (1-t) * (1-t) - b4 = lambda t: (1-t) * (1-t) * (1-t) - p1,p2,p3,p4 = map(mathutils.Vector, ctrlpnts) - def makevert(t): - x,y,z = b1(t)*p1 + b2(t)*p2 + b3(t)*p3 + b4(t)*p4 - return (x,y,z) - curveverts = [makevert(i/resolution) for i in range(resolution+1)] - return curveverts + b1 = lambda t: t * t * t + b2 = lambda t: 3.0 * t * t * (1.0 - t) + b3 = lambda t: 3.0 * t * (1.0 - t) * (1.0 - t) + b4 = lambda t: (1.0 - t) * (1.0 - t) * (1.0 - t) + p1, p2, p3, p4 = map(mathutils.Vector, ctrlpnts) + + def makevert(t): + x, y, z = b1(t) * p1 + b2(t) * p2 + b3(t) * p3 + b4(t) * p4 + return (x, y, z) + curveverts = [makevert(i/resolution) for i in range(resolution+1)] + return curveverts + def make_verts(a, resolution): - s = [] - for i in a: - c = make_bezier(i, resolution) - s.append(c) - b = transpose(s) - s = [] - for i in b: - c = make_bezier(i, resolution) - s.append(c) - verts = s - verts = functools.reduce(operator.add, verts) # flatten the list - return verts + s = [] + for i in a: + c = make_bezier(i, resolution) + s.append(c) + b = transpose(s) + s = [] + for i in b: + c = make_bezier(i, resolution) + s.append(c) + verts = s + verts = functools.reduce(operator.add, verts) # flatten the list + return verts + def make_faces(resolution): - n = resolution+1 - faces = [] - for i in range(n-1): - for j in range(n-1): - v1 = (i+1)*n+j - v2 = (i+1)*n+j+1 - v3 = i*n+j+1 - v4 = i*n+j - faces.append([v1,v2,v3,v4]) - return faces + n = resolution + 1 + faces = [] + for i in range(resolution): + for j in range(resolution): + v1 = (i + 1) * n + j + v2 = (i + 1) * n + j + 1 + v3 = i * n + j + 1 + v4 = i * n + j + faces.append([v1, v2, v3, v4]) + return faces + def indexed_to_rawquads(verts, faces): - rows = len(faces) - cols = len(faces[0]) # or 4 - rawquads = [[None]*cols for i in range(rows)] - for i in range(rows): - for j in range(cols): - index = faces[i][j] - rawquads[i][j] = verts[index] - return rawquads - -def raw_to_indexed(rawfaces): # Generate verts and faces lists, without dups - verts = [] - coords = {} - index = 0 - for i in range(len(rawfaces)): - for j in range(len(rawfaces[i])): - vertex = rawfaces[i][j] - if vertex not in coords: - coords[vertex] = index - index += 1 - verts.append(vertex) - rawfaces[i][j] = coords[vertex] - return verts, rawfaces + rows = len(faces) + cols = len(faces[0]) # or 4 + rawquads = [[None] * cols for i in range(rows)] + for i in range(rows): + for j in range(cols): + index = faces[i][j] + rawquads[i][j] = verts[index] + return rawquads + + +def raw_to_indexed(rawfaces): + # Generate verts and faces lists, without dups + verts = [] + coords = {} + index = 0 + for i in range(len(rawfaces)): + for j in range(len(rawfaces[i])): + vertex = rawfaces[i][j] + if vertex not in coords: + coords[vertex] = index + index += 1 + verts.append(vertex) + rawfaces[i][j] = coords[vertex] + return verts, rawfaces + def transpose(rowsbycols): - rows = len(rowsbycols) - cols = len(rowsbycols[0]) - colsbyrows = [[None]*rows for i in range(cols)] - for i in range(cols): - for j in range(rows): - colsbyrows[i][j] = rowsbycols[j][i] - return colsbyrows + rows = len(rowsbycols) + cols = len(rowsbycols[0]) + colsbyrows = [[None] * rows for i in range(cols)] + for i in range(cols): + for j in range(rows): + colsbyrows[i][j] = rowsbycols[j][i] + return colsbyrows + def make_teapot(filename, resolution): - filenames = [None, teapot, teaspoon] - filename = filenames[filename] - patches = read_indexed_patch_file(filename) - raw = patches_to_raw(patches, resolution) - verts, faces = raw_to_indexed(raw) - return (verts, faces) + filenames = [None, teapot, teaspoon] + filename = filenames[filename] + patches = read_indexed_patch_file(filename) + raw = patches_to_raw(patches, resolution) + verts, faces = raw_to_indexed(raw) + return (verts, faces) # ================================= # === Indexed Bezier Data Block === # ================================= -teapot="""32 +teapot = """32 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 4,17,18,19,8,20,21,22,12,23,24,25,16,26,27,28 19,29,30,31,22,32,33,34,25,35,36,37,28,38,39,40 @@ -555,7 +586,7 @@ teapot="""32 1.425,-0.798,0.0 """ -teaspoon="""16 +teaspoon = """16 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32 33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48 diff --git a/io_scene_obj/export_obj.py b/io_scene_obj/export_obj.py index 9d0f03c5..b4c779d2 100644 --- a/io_scene_obj/export_obj.py +++ b/io_scene_obj/export_obj.py @@ -84,8 +84,8 @@ def write_mtl(scene, filepath, path_mode, copy_set, mtl_dict): fw('Ka %.6f %.6f %.6f\n' % (mat.ambient * world_amb)[:]) # Ambient, uses mirror color, fw('Kd %.6f %.6f %.6f\n' % (mat.diffuse_intensity * mat.diffuse_color)[:]) # Diffuse fw('Ks %.6f %.6f %.6f\n' % (mat.specular_intensity * mat.specular_color)[:]) # Specular - if hasattr(mat, "ior"): - fw('Ni %.6f\n' % mat.ior) # Refraction index + if hasattr(mat, "raytrace_transparency") and hasattr(mat.raytrace_transparency, "ior"): + fw('Ni %.6f\n' % mat.raytrace_transparency.ior) # Refraction index else: fw('Ni %.6f\n' % 1.0) fw('d %.6f\n' % mat.alpha) # Alpha (obj uses 'd' for dissolve) |