当前位置: 首页 > news >正文

Blender下使用python设置骨骼旋转

Blender下的Python脚本代码

空对象移动
import bpy# 删除所有对象,清空场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)# 创建一个新的骨骼对象
bpy.ops.object.armature_add(enter_editmode=True, align='WORLD', location=(0, 0, 0))
armature = bpy.context.object
# Armature 骨架
armature.name = 'Armature'# 获取骨架数据
armature_data = armature.data# 添加骨头
bone1 = armature_data.edit_bones.new('Bone1')
bone1.head = (0, 0, 0)
bone1.tail = (0, 1, 0)bone2 = armature_data.edit_bones.new('Bone2')
bone2.head = (0, 1, 0)
bone2.tail = (0, 2, 0)
bone2.parent = bone1# 退出编辑模式
bpy.ops.object.mode_set(mode='OBJECT')# 创建一个空的平面坐标对象 target 
# 实际上是控制的这个空对象的位置,然后其他的mesh或者obj跟随着这个空对象的位置变化
# 所以不存在,骨架的位置变形,mesh或者对象变形之类的问题
# 控制的是骨骼绑定的空对象 target
bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 2, 0))
target = bpy.context.object
target.name = 'Target'# 为 Bone2 添加 IK 约束 并绑定上面的那个空坐标对象
pose_bone2 = armature.pose.bones['Bone2']
ik_constraint = pose_bone2.constraints.new(type='IK')
ik_constraint.target = target
ik_constraint.chain_count = 2# 设置关键帧动画 从1开始到50结束
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 50# 在第1帧设置目标位置并插入关键帧
bpy.context.scene.frame_set(1)
# 0,2,0 y=2
target.location = (0, 2, 0)
target.keyframe_insert(data_path="location", index=-1)# 在第25帧设置目标位置并插入关键帧
bpy.context.scene.frame_set(25)
# 2,2,0 x=2,y=2
target.location = (2, 2, 0)
target.keyframe_insert(data_path="location", index=-1)# 在第50帧设置目标位置并插入关键帧
bpy.context.scene.frame_set(50)
target.location = (0, -2, 0)
target.keyframe_insert(data_path="location", index=-1)
空对象旋转
import bpy
import math# 删除所有对象,清空场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)# 创建一个新的骨骼对象
bpy.ops.object.armature_add(enter_editmode=True, align='WORLD', location=(0, 0, 0))
armature = bpy.context.object
# Armature 骨架
armature.name = 'Armature'# 获取骨架数据
armature_data = armature.data# 添加骨头
bone1 = armature_data.edit_bones.new('Bone1')
bone1.head = (0, 0, 0)
bone1.tail = (0, 1, 0)bone2 = armature_data.edit_bones.new('Bone2')
bone2.head = (0, 1, 0)
bone2.tail = (0, 2, 0)
bone2.parent = bone1# 退出编辑模式
bpy.ops.object.mode_set(mode='OBJECT')# 创建一个空的平面坐标对象 target
# 实际上是控制的这个空对象的位置,然后其他的mesh或者obj跟随着这个空对象的位置变化
# 所以不存在,骨架的位置变形,mesh或者对象变形之类的问题
# 控制的是骨骼绑定的空对象 target
bpy.ops.object.empty_add(type='PLAIN_AXES', location=(0, 2, 0))
target = bpy.context.object
target.name = 'Target'# 为 Bone2 添加 IK 约束 并绑定上面的那个空坐标对象
pose_bone2 = armature.pose.bones['Bone2']
ik_constraint = pose_bone2.constraints.new(type='IK')
ik_constraint.target = target
ik_constraint.chain_count = 2# 设置关键帧动画 从1开始到50结束
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 50# 计算旋转动画关键帧
# 计算出每个帧的角度,并算出这个角度下的xyz的具体值,然后在哪个位置插入关键帧
num_frames = 50
radius = 2.0  # 旋转半径
for frame in range(num_frames):angle = -2 * math.pi * (frame / num_frames)  # 计算当前帧的角度 负角度实现顺时针旋转# angle = 2 * math.pi * (frame / num_frames)  # 计算当前帧的角度 正角度实现逆时针旋转x = radius * math.cos(angle)y = radius * math.sin(angle)bpy.context.scene.frame_set(frame + 1)target.location = (x, y, 0)target.keyframe_insert(data_path="location", index=-1)# 恢复到第一帧
bpy.context.scene.frame_set(1)# # 在第1帧设置目标位置并插入关键帧
# bpy.context.scene.frame_set(1)
# # 0,2,0 y=2
# target.location = (0, 2, 0)
# target.keyframe_insert(data_path="location", index=-1)
#
# # 在第25帧设置目标位置并插入关键帧
# bpy.context.scene.frame_set(25)
# # 2,2,0 x=2,y=2
# target.location = (2, 2, 0)
# target.keyframe_insert(data_path="location", index=-1)
#
# # 在第50帧设置目标位置并插入关键帧
# bpy.context.scene.frame_set(50)
# target.location = (0, -2, 0)
# target.keyframe_insert(data_path="location", index=-1)
手指旋转
import bpy
import math# 删除所有对象,清空场景
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)# 创建一个新的骨骼对象
bpy.ops.object.armature_add(enter_editmode=True, align='WORLD', location=(0, 0, 0))
armature = bpy.context.object
armature.name = 'HandRig'# 获取骨架数据
armature_data = armature.data# 创建手指骨骼
bones = []
bone_names = ['Bone_Base', 'Bone_Mid', 'Bone_Tip']
positions = [(0, 0, 0), (0, 0, 1), (0, 0, 2)]
for i in range(len(bone_names)):bone = armature_data.edit_bones.new(bone_names[i])bone.head = positions[i]bone.tail = (positions[i][0], positions[i][1], positions[i][2] + 1)if i > 0:bone.parent = bones[i - 1]bones.append(bone)# 退出编辑模式
bpy.ops.object.mode_set(mode='OBJECT')# 设置动画帧范围
bpy.context.scene.frame_start = 1
bpy.context.scene.frame_end = 50# 获取姿势模式下的骨骼对象
pose_bones = bpy.context.object.pose.bones# 关键帧动画设置函数
def set_keyframes(pose_bone, start_frame, end_frame, start_angle, end_angle):bpy.context.scene.frame_set(start_frame)pose_bone.rotation_mode = 'XYZ'pose_bone.rotation_euler = (start_angle, 0, 0)pose_bone.keyframe_insert(data_path="rotation_euler", index=0)bpy.context.scene.frame_set(end_frame)pose_bone.rotation_euler = (end_angle, 0, 0)pose_bone.keyframe_insert(data_path="rotation_euler", index=0)# 为每个关节设置动画
set_keyframes(pose_bones['Bone_Base'], 1, 25, 0, math.radians(-45))
set_keyframes(pose_bones['Bone_Base'], 25, 50, math.radians(-45), 0)set_keyframes(pose_bones['Bone_Mid'], 1, 25, 0, math.radians(-90))
set_keyframes(pose_bones['Bone_Mid'], 25, 50, math.radians(-90), 0)set_keyframes(pose_bones['Bone_Tip'], 1, 25, 0, math.radians(-90))
set_keyframes(pose_bones['Bone_Tip'], 25, 50, math.radians(-90), 0)# 恢复到第一帧
bpy.context.scene.frame_set(1)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Java进阶示例
  • 【100个C++面试题和解答】
  • 电脑怎么录音?分享2种音频录制方法
  • iOS 18 Siri 升级之后都有哪些改变?
  • Idea连接GitLab的过程以及创建在gitlab中创建用户和群组
  • 【无标题】蓝桥杯软件赛Java研究生组/A组)第二章基础算法-第四节:构造
  • WDF驱动开发-硬件资源(一)
  • ElasticSearch学习篇13_《检索技术核心20讲》进阶篇之LSM树
  • 【如何在Python中使用turtle库】
  • 小程序分页新写法
  • STM32开发过程中碰到的问题总结 - 3
  • 使用Unsloth微调Llama3-Chinese-8B-Instruct中文开源大模型
  • excel的vba操作
  • 基于STM32和人工智能的智能四轴飞行器系统
  • 分数限制下,选好专业还是选好学校
  • $translatePartialLoader加载失败及解决方式
  • C++类中的特殊成员函数
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • Redis在Web项目中的应用与实践
  • seaborn 安装成功 + ImportError: DLL load failed: 找不到指定的模块 问题解决
  • SpringBoot 实战 (三) | 配置文件详解
  • Stream流与Lambda表达式(三) 静态工厂类Collectors
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 技术发展面试
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 试着探索高并发下的系统架构面貌
  • 自定义函数
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • #etcd#安装时出错
  • (¥1011)-(一千零一拾一元整)输出
  • (0)Nginx 功能特性
  • (10)ATF MMU转换表
  • (11)MSP430F5529 定时器B
  • (2015)JS ES6 必知的十个 特性
  • (4)Elastix图像配准:3D图像
  • (function(){})()的分步解析
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (原)Matlab的svmtrain和svmclassify
  • (轉貼) 2008 Altera 亞洲創新大賽 台灣學生成果傲視全球 [照片花絮] (SOC) (News)
  • . NET自动找可写目录
  • .gitattributes 文件
  • .java 9 找不到符号_java找不到符号
  • .net Application的目录
  • .NET Core WebAPI中封装Swagger配置
  • .net core 依赖注入的基本用发
  • .NET/C# 编译期间能确定的相同字符串,在运行期间是相同的实例
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .NetCore+vue3上传图片 Multipart body length limit 16384 exceeded.
  • .net开源工作流引擎ccflow表单数据返回值Pop分组模式和表格模式对比
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .net图片验证码生成、点击刷新及验证输入是否正确
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • [ 数据结构 - C++] AVL树原理及实现
  • [ 隧道技术 ] 反弹shell的集中常见方式(二)bash反弹shell