Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.85 KB | None | 0 0
  1. import os
  2. import time
  3. import struct
  4. import bpy
  5. import bmesh
  6. import mathutils
  7. from bpy_extras.io_utils import (ImportHelper,
  8.                                  axis_conversion,)
  9.  
  10. materialList = []
  11. #===============================================================================
  12. # create_CubeMesh
  13. #===============================================================================
  14. def create_CubeMesh(name, objColor, loc, rot, sca):
  15.     #Define vertices, faces, edges
  16. #     vertsC = [ [1, 1, 0], [1, 0, 0], [0, 0, 0], [0, 1, 0], [1, 1, 1], [0, 1, 1], [0, 0, 1], [1, 0, 1] ]
  17. #     facesC = [ [0, 1, 2], [0, 2, 3], [4, 5, 6], [4, 6, 7], [0, 4, 7], [0, 7, 1], [1, 7, 6], [1, 6, 2], [2, 6, 5], [2, 5, 3], [4, 0, 3], [4, 3, 5] ]
  18.      
  19.     #Define mesh and object
  20.     mesh = bpy.data.meshes['cubePrimary']
  21.     obj = bpy.data.objects.new(name, mesh)
  22.        
  23.     obj.color = objColor
  24.    
  25.     #Set location and scene of object
  26.     obj.location = loc
  27.     obj.dimensions = sca
  28.     obj.rotation_euler = rot
  29.     print(obj)
  30.        
  31.     #Create mesh
  32. #     mesh.from_pydata(vertsC,[],facesC)
  33. #     mesh.update(calc_edges=True)
  34.    
  35.     # Link object to scene and make active
  36.     #bpy.context.scene.objects.link(obj)
  37.     scn = bpy.context.scene
  38.     scn.objects.link(obj)
  39.     return obj
  40.  
  41.  
  42. #===============================================================================
  43. # create_sphere_mesh
  44. #===============================================================================
  45. def create_SphereMesh(name, radius, locat, objColor):
  46.  
  47.     # Create mesh and object
  48.     mesh = bpy.data.curves['Primary']
  49.     obj = bpy.data.objects.new(name, mesh)
  50.     #obj.show_name = True
  51.     obj.scale = (radius, radius, radius)
  52.     obj.location = locat
  53.     obj.color = objColor
  54.          
  55.     # Link object to scene and make active
  56.     scn = bpy.context.scene
  57.     scn.objects.link(obj)
  58.  
  59.    
  60.     return obj
  61. #===============================================================================
  62. # createCubePrimitive
  63. #===============================================================================
  64.  
  65. def createCubePrimitive(name):
  66.     bpy.ops.mesh.primitive_cube_add()
  67.     # Add a material slot to each object
  68.     bpy.ops.object.material_slot_add()
  69.     ob = bpy.context.object
  70.     ob.name = name
  71.     #ob.show_name = True
  72.     me = ob.data
  73.     me.name = name
  74.     return ob
  75.  
  76. #===============================================================================
  77. # createSpherePrimitive
  78. #===============================================================================
  79. def createSpherePrimitive(name):
  80.     bpy.ops.surface.primitive_nurbs_surface_sphere_add()
  81.     # Add a material slot to each object
  82.     bpy.ops.object.material_slot_add()
  83.     ob = bpy.context.object
  84.     ob.name = name
  85.     #ob.show_name = True
  86.     me = ob.data
  87.     me.name = name
  88.     return ob
  89.  
  90. #===============================================================================
  91. # makeMatte
  92. #===============================================================================
  93. def makeMatte(name ):
  94.     mat = bpy.data.materials.new(name)
  95.     #mat.diffuse_color = diffuse
  96.     mat.diffuse_shader = 'LAMBERT'
  97.     mat.diffuse_intensity = 0.8
  98.     #mat.specular_color = specular
  99.     mat.specular_shader = 'COOKTORR'
  100.     mat.specular_intensity = 0.5
  101.     #mat.alpha = alpha
  102.     mat.ambient = 1
  103.     mat.use_object_color = True
  104.     return mat
  105.  
  106. def setMatte(ob, mat):
  107.     me = ob.data
  108.     me.materials.append(mat)
  109.    
  110. # Function to convert RGB to hex for groupong
  111.  
  112. #===============================================================================
  113. # rgb_to_hex
  114. #===============================================================================
  115. def rgb_to_hex(r,g,b):
  116.      
  117.     flo = [ x * 255 for x in (r,g,b)]
  118.          
  119.     R,G,B = flo[0], flo[1], flo[2]
  120.     hex = '#%02X%02X%02X' % (R,G,B)
  121.     print(hex)
  122.  
  123. #===============================================================================
  124. # load_ssynth_mesh
  125. #===============================================================================
  126. def load_ssynth_mesh(file, self):
  127.  
  128. # Load the .ssynth file
  129.     objCount = 0
  130.    
  131.     createSpherePrimitive('Primary')
  132.     createCubePrimitive('cubePrimary')
  133.    
  134.     makeMatte('SSmat')
  135.     #read each line of the .ssynth file
  136.     for line in file:
  137.         if  objCount % 2500 == 0:
  138.             print ("Import progress report: " + str(objCount) + " objects")
  139.             args = line.split()
  140.             argsIndex = 0
  141.             colR = 1
  142.             colB = 1
  143.             colG = 1
  144.        
  145.         #begin sphere shape. here we import any spheres
  146.         if args[argsIndex] == "s":
  147.             argsIndex += 1
  148.             cx = float(args[argsIndex])
  149.             argsIndex += 1
  150.             cy = float(args[argsIndex])
  151.             argsIndex += 1
  152.             cz = float(args[argsIndex])
  153.             argsIndex += 1
  154.             radius = float(args[argsIndex])
  155.             argsIndex += 1
  156.            
  157.             colR = float(args[argsIndex])
  158.             argsIndex += 1
  159.             colG = float(args[argsIndex])
  160.             argsIndex += 1
  161.             colB = float(args[argsIndex])
  162.             argsIndex += 1
  163.            
  164.            
  165.             locat = (cx, cy, cz,)
  166.             # Set the Alpha - maybe we can use this later
  167.             alpha = 1.0
  168.             bpy.context.object.color = (colR, colG, colB, alpha)
  169.             objColor = bpy.context.object.color
  170.             create_SphereMesh("Sphere", radius, locat, objColor)            
  171.             # Find the active material
  172.             bpy.context.object.active_material_index = 0
  173.             # Add that material to the slot of each object
  174.             bpy.context.object.active_material = bpy.data.materials["SSmat"]
  175.        
  176.         #begin box shape. here we import any boxes
  177.         elif args[argsIndex] == "b":
  178.             argsIndex += 1
  179.             transMatrix = ((float(args[argsIndex + 0]),  float(args[argsIndex + 1]),  float(args[argsIndex + 2]),  float(args[argsIndex + 12])),
  180.                            (float(args[argsIndex + 4]),  float(args[argsIndex + 5]),  float(args[argsIndex + 6]),  float(args[argsIndex + 13])),
  181.                            (float(args[argsIndex + 8]),  float(args[argsIndex + 9]),  float(args[argsIndex + 10]), float(args[argsIndex + 14])),
  182.                            (float(args[argsIndex + 3]),  float(args[argsIndex + 7]),  float(args[argsIndex + 11]), float(args[argsIndex + 15])))
  183.  
  184.            
  185.             print (transMatrix)
  186.            
  187.             mat = mathutils.Matrix(transMatrix)
  188.             print(mat)
  189.            
  190.             loc, rot, sca = mat.decompose()
  191.              
  192.             print(loc, rot, sca)
  193.             loc = tuple(loc)
  194.             sca = tuple(sca)
  195.             rot = tuple(mat.to_euler())
  196.             print (loc, sca, rot)
  197.                        
  198.             argsIndex += 16
  199.  
  200.             colR = eval(args[argsIndex])
  201.             argsIndex += 1
  202.             colG = eval(args[argsIndex])
  203.             argsIndex += 1
  204.             colB = eval(args[argsIndex])
  205.             argsIndex += 1
  206.             alpha = 1.0
  207.            
  208.            
  209.             bpy.context.object.color = (colR, colG, colB, alpha)
  210.             objColor = bpy.context.object.color
  211.             create_CubeMesh('cubePrimary', objColor, loc, rot, sca)
  212.             objColor_group = (colR, colG, colB)
  213.             print(objColor_group)
  214.             color_Hex = rgb_to_hex(colR, colG, colB)
  215.             print(color_Hex)
  216.             # Find the active material
  217.             bpy.context.object.active_material_index = 0
  218.             # Add that material to the slot of each object
  219.             bpy.context.object.active_material = bpy.data.materials["SSmat"]
  220.             materialList.append(color_Hex)
  221.    
  222. #     color_group = list(set(materialList))
  223.     #print(materialList)  
  224.            
  225.     return {'FINISHED'}        
  226.            
  227.     file.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement