【Blender开发】网格基础2: 立方体与矩阵 | Matrix函数

https://sinestesia.co/blog/tutorials/python-cube-matrices

系列教程

1:生成2D 网格

2:立方体与矩阵 | Matrix函数

3:正二十面体与细分 | 黄金比例

4:圆角立方体

5:圆与圆柱

 

欢迎来到本系列的第二部分

加了点数学,来控制网格的位置与缩放,来看看如何使用Matrix函数更改网格吧

完整代码

import bpy
import math
from mathutils import Matrix

# -----------------------------------------------------------------------------
# 设置

name = 'Cubert'

# 原始网格点设置
mesh_offset = (0, 0, 0)
origin_offset = (0, 0, 0)

# 矩阵设置
translation = (7, 7, 7)
scale_factor = 3
scale_axis = (0, 0, 0)
rotation_angle = math.radians(0)
rotation_axis = 'X'

# -----------------------------------------------------------------------------
# 通用函数

def vert(x, y, z):
    """ 生成顶点 """
    return (x + origin_offset[0], y + origin_offset[1], z + origin_offset[2])

# -----------------------------------------------------------------------------
# 正方体生成数据

verts = [vert(1.0, 1.0, -1.0),
         vert(1.0, -1.0, -1.0),
         vert(-1.0, -1.0, -1.0),
         vert(-1.0, 1.0, -1.0),
         vert(1.0, 1.0, 1.0),
         vert(1.0, -1.0, 1.0),
         vert(-1.0, -1.0, 1.0),
         vert(-1.0, 1.0, 1.0)]

faces = [(0, 1, 2, 3),
         (4, 7, 6, 5),
         (0, 4, 5, 1),
         (1, 5, 6, 2),
         (2, 6, 7, 3),
         (4, 0, 3, 7)]

# -----------------------------------------------------------------------------
# 添加对象到场景

mesh = bpy.data.meshes.new(name)
mesh.from_pydata(verts, [], faces)

obj = bpy.data.objects.new(name, mesh)
bpy.context.scene.collection.objects.link(obj)

obj.select_set(True)
bpy.context.view_layer.objects.active = obj

# -----------------------------------------------------------------------------
# 将网格移动到起始设置点

obj.location = [(i * -1) + mesh_offset[j] for j, i in enumerate(origin_offset)]

# -----------------------------------------------------------------------------
# Blender的 Matrix(矩阵)函数

translation_matrix = Matrix.Translation(translation)
scale_matrix = Matrix.Scale(scale_factor, 4, scale_axis)
rotation_mat = Matrix.Rotation(rotation_angle, 4, rotation_axis)

obj.matrix_world @= translation_matrix @ rotation_mat @ scale_matrix

# -----------------------------------------------------------------------------
# Matrix 更改网格

# 取消注释可以改变网格
# obj.data.transform(translation_matrix @ scale_matrix)

 

给TA充电
共{{data.count}}人
人已充电
BlenderBlender开发

【Blender开发】Blender文件浏览器全攻略

2022-1-15 2:23:43

BlenderBlender开发

【blender开发】网格基础3:正二十面体与细分 | 黄金比例

2022-1-15 10:01:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索