Support for Blender
简介
近期DBD正努力增加对Blinder的技术支持
教程列表
001 | 通过python代码,在Blender中生成4个小球,并且让他们动起来 |
002 | 遍历每一帧画面,获取关键数据,并导出CSV文件 |
003 | 将movement.csv文件转换成控制器可执行的文件 |
001-通过python代码,在Blender中生成4个小球,并且让他们动起来
step01,清除已有物体和关键帧
1.打开blender,切换到scripting界面,新建python文件.2.输入如下代码,保存并运行.这样就清除了应经插入的关键帧和已有的物体.
import bpy
bpy.context.scene.frame_preview_start = 0
bpy.context.scene.frame_current = 0
# Deselect all objects
bpy.ops.mesh.select_all(action='DESELECT')
# Select all objects and delete them
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.delete()
bpy.ops.anim.keyframe_clear_v3d()
step02,创建4个小球,直径30mm 间距150mm
import bpy
# Create 4 balls
for i in range(4):
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.03, location=(i*0.15, 0, 0))
step03,插入关键帧, 让小球动起来
import bpy
bpy.context.scene.frame_preview_start = 0
bpy.context.scene.frame_current = 0
#bpy.context.scene.frame_preview_end = 100
bpy.ops.object.select_all(action='SELECT')
bpy.ops.anim.keyframe_clear_v3d()
def setPosition(pos):
bpy.data.objects["Sphere"].location[2] = pos[0]
bpy.data.objects["Sphere.001"].location[2] = pos[1]
bpy.data.objects["Sphere.002"].location[2] = pos[2]
bpy.data.objects["Sphere.003"].location[2] = pos[3]
def insertKeyFrame(timeline, pos):
bpy.context.scene.frame_current = timeline
bpy.ops.object.select_all(action='SELECT')
setPosition(pos)
bpy.ops.anim.keyframe_insert_menu(type='Location')
insertKeyFrame(0, [0,0,0,0])
insertKeyFrame(1, [0,0,0,0])
insertKeyFrame(25, [0.75,0.5,0.25,0.1])
insertKeyFrame(99, [0,0,0,0])
insertKeyFrame(100, [0,0,0,0])
bpy.context.scene.frame_preview_end = 100
bpy.context.scene.frame_end = bpy.context.scene.frame_preview_end
step04,切换到animation界面,点击播放按钮就可以看到小球动起来啦
002-遍历每一帧画面,获取关键数据,并导出CSV文件
import bpy
import numpy as np
framesize = bpy.context.scene.frame_end
i = 0
tempCounter=0
darray = np.zeros((framesize+1, 4))
bpy.context.scene.frame_current = 0
def run():
global i
global darray
global framesize
bpy.context.scene.frame_current = i
# motor-0
darray[i][0] = bpy.data.objects["Sphere"].location[2]
darray[i][1] = bpy.data.objects["Sphere.001"].location[2]
darray[i][2] = bpy.data.objects["Sphere.002"].location[2]
darray[i][3] = bpy.data.objects["Sphere.003"].location[2]
i = i+1
if(i>framesize):
np.savetxt("/home/db/DBD_Linux/Blender/2.8/tutorial001/movement.csv", darray, delimiter=",")
return None
return 0.1
bpy.app.timers.register(run)
003-将movement.csv文件转换成控制器可执行的文件
导出的csv文件有4列,分别代表4个小球的高度信息.有101行,分别代表画面帧0-100对应的小球高度信息.这里单位是m,我们需要转换成电机的脉冲单位. 计算方法如下:小球的高度H,轮子的直径D,电机转一圈的脉冲数M,所以转换过来就是P=H/(Pi*D)*M. 我们还需要将25帧每秒的数据插补到100帧每秒.最后将数据以二进制的形式存储在文件中,