[Blender] 编程基础

发布于 2023-04-26  73 次阅读


1. 根据输入坐标绘制曲线

思路:先绘制曲线,切分为需要点数,注意改变点坐标 RenderCurveInCycles.blend

from bpy import context, data, ops
from mathutils import geometry, Euler, Matrix, Quaternion, Vector
import numpy as np

ops.object.mode_set(mode='OBJECT')
ops.object.select_all(action='SELECT')
ops.object.delete(use_global=False)

# Load centroid data and assigned to the bezier points
filepath = "D:\\04_coding\\Python\\06_Visualization\Mesh\\00_data_acquisition\\3D_2_4_5layers\\Long_0\\centerline.csv"
centerline = np.loadtxt(filepath, delimiter = ',', skiprows = 1)
count = centerline.shape[0] - 2

num_point = count + 2
ops_mesh = ops.mesh

# Create curve and randomize its points.
ops.curve.primitive_bezier_curve_add(enter_editmode=True)
ops.curve.subdivide(number_cuts=count) # 18 lines for every 1/4 circle.

# Acquire a reference to the bezier points.
bez_curve = context.active_object
bez_curve.name = 'Centerline'
bez_points = bez_curve.data.splines[0].bezier_points
print(bez_points)

# Set handles to desired handle type.
for bez_point in bez_points:
    bez_point.handle_left_type = 'AUTO'  # 'AUTO', 'FREE', 'VECTOR'
    bez_point.handle_right_type = 'AUTO'

slice = 0
while slice < num_point:
    bez_points[slice].co = Vector(centerline[slice,:])
    print(Vector(centerline[slice,:]))
    slice += 1


context.object.data.bevel_depth = 1.5
context.object.data.bevel_resolution = 10

Render NumPy voxels in Blender

import bpy
import numpy as np

objs = bpy.data.objects
scn = bpy.context.scene

scene_objs = [
        objs['Cube_transparent'],
        objs['Cube_body']
#        objs['sand'],
#        objs['grass'],
#        objs['water'],
        ]

voxel_data = np.load('/home/u/p/cat_1st_demo/render_bendich/_cartesian_model.npy').astype('int')

for x in range(voxel_data.shape[0]):
    for y in range(voxel_data.shape[1]):
        for z in range(voxel_data.shape[2]):
            obj = scene_objs[voxel_data[x,y,z]].copy()
            scn.objects.link(obj)
            obj.location = (x*2.1, y*2.1, z*2.1)

本当の声を響かせてよ