Advertisement
migero

Untitled

Jul 22nd, 2023 (edited)
1,360
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | Science | 0 0
  1. import bpy
  2. import csv
  3. from mathutils import Vector
  4.  
  5. def import_delta_vertices(filepath):
  6.     # Retrieve the vertex positions from the CSV file
  7.     vertices = []
  8.     with open(filepath, newline='') as csvfile:
  9.         reader = csv.reader(csvfile)
  10.         next(reader)  # Skip the header line
  11.         for row in reader:
  12.             vertex_index = int(row[0])
  13.             delta_x = float(row[1])
  14.             delta_y = float(row[2])
  15.             delta_z = float(row[3])
  16.            
  17.             # Convert the delta values to a Vector
  18.             delta_vector = Vector((delta_x, delta_y, delta_z))
  19.            
  20.             # Store the delta vertex position
  21.             vertices.append(delta_vector)
  22.  
  23.     # Create a new mesh object for the point cloud
  24.     mesh = bpy.data.meshes.new("VertexPositions")
  25.     obj = bpy.data.objects.new("VertexPositions", mesh)
  26.  
  27.     # Get the active scene
  28.     scene = bpy.context.scene
  29.  
  30.     # Link the object to the scene
  31.     scene.objects.link(obj)
  32.  
  33.     # Select and make the object active
  34.     obj.select = True
  35.     scene.objects.active = obj
  36.  
  37.     # Enter Edit Mode to add vertices
  38.     bpy.ops.object.mode_set(mode='EDIT')
  39.     bpy.ops.mesh.select_all(action='SELECT')
  40.     bpy.ops.mesh.delete(type='VERT')
  41.  
  42.     # Exit Edit Mode to avoid potential issues
  43.     bpy.ops.object.mode_set(mode='OBJECT')
  44.  
  45.     # Add the vertices to the mesh
  46.     mesh.from_pydata(vertices, [], [])
  47.     mesh.update()
  48.  
  49.     print("Vertex positions imported successfully.")
  50.  
  51. csv_file_path = 'D:/test.csv'
  52. import_delta_vertices(csv_file_path)
  53.  
  54.  
  55.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement