Advertisement
flwkjlwekjfs

Untitled

Jul 2nd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. #!/usr/local/bin/python3.6
  2.  
  3. import json
  4. import urllib.request
  5. import subprocess
  6.  
  7. # settings
  8. jsonUrl = "https://raw.githubusercontent.com/magnusmichaelson/rack3d/master/rack3d_json/example.json"
  9. objFile = "/tmp/dc.obj"
  10. mtlFile = "/tmp/dc.mtl"
  11. importFile = "/tmp/dc.py"
  12. blender = "/Applications/Blender/blender.app/Contents/MacOS/blender"
  13.  
  14. # download the json and import into a list of dicts
  15. with urllib.request.urlopen(jsonUrl) as response:
  16. data = json.loads(response.read())
  17.  
  18. # process the list of dicts
  19. for thing in data:
  20. # generate rgb values by object type
  21. thing['rgb_blue'] = 0.5
  22. thing['rgb_green'] = 0.5
  23. thing['rgb_red'] = 0.5
  24. if ( thing['objtype_id'] == '4' ):
  25. thing['rgb_blue']=0.75
  26. thing['rgb_green']=0.75
  27. thing['rgb_red']=1.0
  28. if ( thing['objtype_id'] == '8' ):
  29. thing['rgb_blue']=0.75
  30. thing['rgb_green']=1.0
  31. thing['rgb_red']=0.75
  32. if ( thing['objtype_id'] == '9' ):
  33. thing['rgb_blue']=1.0
  34. thing['rgb_green']=0.75
  35. thing['rgb_red']=0.75
  36. # scale down from mm to meters
  37. thing['xMin'] = thing['xMin'] * 0.001
  38. thing['xMax'] = thing['xMax'] * 0.001
  39. thing['yMin'] = thing['yMin'] * 0.001
  40. thing['yMax'] = thing['yMax'] * 0.001
  41. thing['zMin'] = thing['zMin'] * 0.001
  42. thing['zMax'] = thing['zMax'] * 0.001
  43. # rotate -90 on x axis to compensate for blender import script
  44. temp = thing['yMin']
  45. thing['yMin'] = thing['zMin']
  46. thing['zMin'] = thing['yMax'] * -1
  47. thing['yMax'] = thing['zMax']
  48. thing['zMax'] = temp * -1
  49.  
  50. # export obj file
  51. vert=0
  52. face=0
  53. with open(objFile, 'w') as obj:
  54. for thing in data:
  55. obj.write("o {}\n".format(thing['name']))
  56. obj.write("v {} {} {}\n".format(thing['xMin'],thing['yMin'],thing['zMin']))
  57. obj.write("v {} {} {}\n".format(thing['xMin'],thing['yMax'],thing['zMin']))
  58. obj.write("v {} {} {}\n".format(thing['xMax'],thing['yMax'],thing['zMin']))
  59. obj.write("v {} {} {}\n".format(thing['xMax'],thing['yMin'],thing['zMin']))
  60. obj.write("v {} {} {}\n".format(thing['xMin'],thing['yMin'],thing['zMax']))
  61. obj.write("v {} {} {}\n".format(thing['xMin'],thing['yMax'],thing['zMax']))
  62. obj.write("v {} {} {}\n".format(thing['xMax'],thing['yMax'],thing['zMax']))
  63. obj.write("v {} {} {}\n".format(thing['xMax'],thing['yMin'],thing['zMax']))
  64. obj.write("vn 0.0 0.0 -1.0\n")
  65. obj.write("vn 0.0 0.0 1.0\n")
  66. obj.write("vn 0.0 1.0 0.0\n")
  67. obj.write("vn 1.0 0.0 0.0\n")
  68. obj.write("vn 0.0 -1.0 0.0\n")
  69. obj.write("vn -1.0 0.0 0.0\n")
  70. obj.write("usemtl {}\n".format(thing['name']))
  71. obj.write("s off\n")
  72. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+1), (face+1), (vert+2), (face+1), (vert+3), (face+1), (vert+4), (face+1)))
  73. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+5), (face+2), (vert+8), (face+2), (vert+7), (face+2), (vert+6), (face+2)))
  74. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+2), (face+3), (vert+6), (face+3), (vert+7), (face+3), (vert+3), (face+3)))
  75. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+3), (face+4), (vert+7), (face+4), (vert+8), (face+4), (vert+4), (face+4)))
  76. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+4), (face+5), (vert+8), (face+5), (vert+5), (face+5), (vert+1), (face+5)))
  77. obj.write("f {}//{} {}//{} {}//{} {}//{}\n".format( (vert+1), (face+6), (vert+5), (face+6), (vert+6), (face+6), (vert+2), (face+6)))
  78. obj.write("\n")
  79. vert = vert + 8
  80. face = face + 6
  81.  
  82. # export mtl file
  83. with open(mtlFile, 'w') as mtl:
  84. for thing in data:
  85. mtl.write("newmtl {}\n".format(thing['name']))
  86. mtl.write("Ns 96.078431\n")
  87. mtl.write("Ka 1.000000 1.000000 1.000000\n");
  88. mtl.write("Kd {} {} {}\n".format(thing['rgb_red'],thing['rgb_green'],thing['rgb_blue']))
  89. mtl.write("Ks 0.500000 0.500000 0.500000\n");
  90. mtl.write("Ke 0.000000 0.000000 0.000000\n");
  91. mtl.write("Ni 1.000000\n");
  92. mtl.write("d 1.000000\n");
  93. mtl.write("illum 2\n");
  94. mtl.write("\n");
  95.  
  96. # build the import python script
  97. with open(importFile, 'w') as load:
  98. load.write("import bpy\n");
  99. load.write("full_path_to_file = \"{}\"\n".format(objFile))
  100. load.write("bpy.ops.import_scene.obj(filepath=full_path_to_file)\n")
  101.  
  102. # run blender
  103. #print("{} -P {}".format(blender, importFile))
  104. process=subprocess.run([blender,"-P",importFile], env={}, close_fds=True)
  105. print(process.returncode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement