如何用Blender Python获取Grease Pencil笔触各点的厚度/压力值?
获取Grease Pencil笔触点厚度及遍历判断的实现方法
可行性说明
完全可行。Blender的Python API提供了直接访问Grease Pencil数据结构的接口,能够轻松遍历笔触的所有控制点,并读取、判断每个点的厚度值。
具体实现步骤及代码示例
1. 定位目标Grease Pencil对象
先获取场景中的Grease Pencil对象(示例中假设对象名为"Grease Pencil"):
import bpy # 获取Grease Pencil对象 gp_obj = bpy.data.objects.get("Grease Pencil") if not gp_obj or gp_obj.type != 'GPENCIL': raise Exception("未找到有效的Grease Pencil对象")
2. 遍历层级结构并获取点厚度
Grease Pencil的数据层级为:对象 → 图层(gp_layers) → 帧(frames) → 笔触(strokes) → 控制点(points)。每个控制点的厚度存储在thickness属性中,以下是完整遍历及判断逻辑:
# 遍历所有图层 for gp_layer in gp_obj.data.layers: # 遍历图层内的所有帧 for gp_frame in gp_layer.frames: # 遍历帧内的所有笔触 for gp_stroke in gp_frame.strokes: print(f"当前笔触:{gp_stroke.name}") # 遍历笔触的所有控制点 for idx, point in enumerate(gp_stroke.points): # 获取当前点的厚度值 point_thickness = point.thickness print(f"第{idx}个点的厚度:{point_thickness}") # 判断厚度是否达到设定阈值(示例:阈值为5.0) threshold = 5.0 if point_thickness >= threshold: print(f"第{idx}个点厚度达标(≥{threshold})") else: print(f"第{idx}个点厚度未达标(<{threshold})")
3. 关键注意事项
- 厚度值的单位与Blender全局单位设置一致,默认是米,可按需转换。
- 上述代码在对象模式下即可运行,无需切换到编辑模式。
- 若笔触使用了压力映射厚度,API获取的是控制点的原始厚度值,最终显示厚度会受压力曲线影响,需根据需求判断是否需要结合曲线计算实际显示厚度。
内容的提问来源于stack exchange,提问作者Pow
相关产品推荐
相关产品推荐

