Advertisement
Guest User

Untitled

a guest
Jan 13th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # Make a file. This file should contain the name of your models and operations you wish to perform. Put it into ARGV[1]
  2. # f1+f2    - merge two files, new filename f1f2
  3. # f1*r     - Make a series of x degree rotations, default advtrains-compatible, again and again until passed 359 degrees. Define using ARGV[2]
  4. # f1*x     - Rotate by x degrees
  5. # =f3      - Set the resulting filename to f3
  6. rotationAmount = 22.5 # In degrees
  7.  
  8. import sys
  9.  
  10. if 4 <= len(sys.argv):
  11.     rotationAmount = sys.argv[2]
  12.  
  13. # AST generation
  14.  
  15. operators = {
  16.     "merge":"+",
  17.     "rotateDegrees":"*",
  18.     "rotateFull":"*r",
  19.     }
  20.  
  21. with open(sys.argv[1]) as op:
  22.     operations = op.read()
  23.  
  24. operations = operations.split("\n")
  25.  
  26. for i in range(len(operations)):
  27.     for o in operators:
  28.         operations[i] = operations[i].replace(operators[o], ".splithere." + o + ".splithere.")
  29.     operations[i] = operations[i].split(".splithere.")
  30.  
  31. # AST Parsing
  32. def rotate(degree, filename):
  33.     import bpy
  34.     import shutil
  35.  
  36.     blender_bin = shutil.which("blender")
  37.     if blender_bin:
  38.        print("Found:", blender_bin)
  39.        bpy.app.binary_path = blender_bin
  40.     else:
  41.        print("Unable to find blender!")
  42.  
  43.     expandedFilename = filename + ".obj"
  44.  
  45.     bpy.ops.wm.read_factory_settings(use_empty=True)
  46.     bpy.ops.import_scene.obj(filename = expandedFilename)
  47.     bpy.ops.transform.rotate(value = degree)
  48.     bpy.ops.export_scene.obj(filename = expandedFilename + str(degree))
  49.  
  50. def recurseRotate(filename):
  51.     for i in range(int(360/rotateAmount)):
  52.         rotate(rotateAmount*i, filename)
  53.  
  54. def merge(file1, file2):
  55.     with open(file1 + ".obj", "r") as f1:
  56.         file1Contents = f1.read()
  57.     with open(file2 + ".obj", "r") as f2:
  58.         file2Contents = f2.read()
  59.     with open(file1+file2 + ".obj", "w") as f3:
  60.         file3Contents = file1Contents + "\n" + file2Contents
  61.         f3.write(file3Contents)
  62.  
  63. for i in range(len(operations)):
  64.     ops = operations[i]
  65.     try:
  66.         if ops[1] == "merge":
  67.             merge(ops[0], ops[2])
  68.         elif ops[1] == "rotateDegrees":
  69.             rotate(ops[2], ops[1])
  70.         elif ops[1] == "rotateFull":
  71.             recurseRotate(ops[0])
  72.     except:
  73.         pass
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement