Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import bpy
  2. import mathutils
  3.  
  4. obj = bpy.context.active_object
  5. me = obj.data
  6.  
  7. me.calc_tangents()
  8.  
  9. vertices = []
  10.  
  11. for t in me.polygons:
  12. assert(t.loop_total == 3)
  13. for l in t.loop_indices:
  14. loop = me.loops[l]
  15. pos = me.vertices[loop.vertex_index].co
  16. nrm = loop.normal
  17. uv = me.uv_layers.active.data[l].uv
  18. vertices.append([[pos.x, pos.y, pos.z], [uv.x, uv.y], [nrm.x, nrm.y, nrm.z]])
  19.  
  20. assert(len(vertices)%3 == 0)
  21.  
  22. me2 = bpy.data.meshes.new("meshCopy")
  23.  
  24. me2.vertices.add(len(vertices))
  25. me2.polygons.add(len(vertices)/3)
  26. me2.loops.add(len(vertices))
  27.  
  28. me2.uv_textures.new()
  29. uv_layer = me2.uv_layers[0]
  30. custom_normals = [None] * len(vertices)
  31.  
  32. for i in range(len(vertices)//3):
  33. me2.loops[i*3].vertex_index = i*3
  34. me2.loops[i*3+1].vertex_index = i*3+1
  35. me2.loops[i*3+2].vertex_index = i*3+2
  36.  
  37. uv_layer.data[i*3].uv = mathutils.Vector(vertices[i*3][1])
  38. uv_layer.data[i*3+1].uv = mathutils.Vector(vertices[i*3+1][1])
  39. uv_layer.data[i*3+2].uv = mathutils.Vector(vertices[i*3+2][1])
  40.  
  41. me2.vertices[i*3].co = mathutils.Vector(vertices[i*3][0])
  42. me2.vertices[i*3+1].co = mathutils.Vector(vertices[i*3+1][0])
  43. me2.vertices[i*3+2].co = mathutils.Vector(vertices[i*3+2][0])
  44.  
  45. custom_normals[i*3] = vertices[i*3][2]
  46. custom_normals[i*3+1] = vertices[i*3+1][2]
  47. custom_normals[i*3+2] = vertices[i*3+2][2]
  48.  
  49. me2.polygons[i].loop_start = i*3
  50. me2.polygons[i].loop_total = 3
  51. me2.polygons[i].material_index = 0
  52.  
  53. me2.update(calc_edges=True)
  54. me2.normals_split_custom_set(custom_normals)
  55.  
  56. me2.materials.append(bpy.data.materials['bronze_medal'])
  57.  
  58. me2.use_auto_smooth = True
  59.  
  60. obj2 = bpy.data.objects.new("copy object", me2)
  61. bpy.context.scene.objects.link(obj2)
  62.  
  63. obj2.location = mathutils.Vector([0,-1,0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement