Untitled

 avatar
unknown
python
3 years ago
3.3 kB
17
Indexable
# Считываем бибилиотеки относящиеся к маетматическим функциям и процедурам
import itertools
import math

import bpy 
from mathutils import * 

# Очищаем пространственную сцену от объектов
for mesh in bpy.data.meshes:
    bpy.data.meshes.remove(mesh)

# Очищаем пространственную сцену от камер
for cam in bpy.data.cameras:
    bpy.data.cameras.remove(cam)

# Sourcing
data_file_path = "C:\\Users\\le0\\Desktop\\c20.txt" # Путь до файла с данными
data_array = []
with open(data_file_path) as data_file: # Считываем данные из файла
    for line in data_file:
        data_array.append([float(number) for number in line.split()])

# Filtering
filtered_data_array = data_array # Filtering пуст

# Mapping
sphere_radius = 0.1 # Задаем радиус сфер
minimal_distance = 1.5 # Задаем минимальное расстояние для цилиндров
sphere_material = bpy.data.materials.new(name="SphereMaterial") 
sphere_material.diffuse_color = [0.5, 0.9, 0.3, 1] # Задаем цвет сфер
for point in filtered_data_array:
    # Создаем сферу
    bpy.ops.mesh.primitive_uv_sphere_add(radius=sphere_radius, location=point)
    sphere = bpy.context.object
    # Добавляем материал для сферы
    sphere.data.materials.append(sphere_material)

cylinder_radius = sphere_radius / 2 # Задаем радиус цилиндра
cylinder_material = bpy.data.materials.new(name="SphereMaterial")
cylinder_material.diffuse_color = [1, 1, 1, 1] # Задаем цвет цилиндров
for start, end in itertools.combinations(filtered_data_array, 2):
    # Считаем расстояние между точками
    distance = math.dist(start, end)
    if distance < minimal_distance:
        diff = [start[i] - end[i] for i in range(3)]
        middle = [(start[i] + end[i]) / 2 for i in range(3)]
        # Создаем цилиндр
        bpy.ops.mesh.primitive_cylinder_add(
            radius=cylinder_radius, depth=distance, location=middle
        )
        cylinder = bpy.context.object
        # Поварачиваем цилиндр
        cylinder.rotation_euler[1] = math.acos(diff[2] / distance)
        cylinder.rotation_euler[2] = math.atan2(diff[1], diff[0])
        cylinder.data.materials.append(cylinder_material)

# Rendering
camera_location = [5, 5, 5]
camera_target = [0, 0, 0]
# Задаем ветор для камеры
camera_view_vector = [camera_target[i] - camera_location[i] for i in range(3)]
# Создаем камеру
camera = bpy.data.objects.new("Camera", bpy.data.cameras.new("Camera"))
# Помещаем и поварачиваем камеру
camera.location = camera_location
camera.rotation_euler = Vector(camera_view_vector).to_track_quat('-Z', 'Y').to_euler()
# Помещаем камеру на сцену и выбираем её в качестве активной камеры
bpy.context.scene.collection.objects.link(camera)
bpy.context.scene.camera = camera
for area in bpy.context.screen.areas:
    if area.type == "VIEW_3D":
        area.spaces.active.region_3d.view_perspective = "CAMERA"
        break
Editor is loading...