blender-python-读取物体信息

目标

新建立方体,然后将立方体的信息读取出来,将立方体的方位、旋转角度、缩放信息放入json类型文档中;
再新建一个blender2文件,通过读取文档,新建出之前的立方体;

前期工作

文件名改成一致的
image.png
直接下载blender的插件解决无法解析导入“bpy”的报错,警告还是在的,但是运行代码没问题的
image.png

如何新建物体

image.png

import bpy

view_layer = bpy.context.view_layer

# Create new light datablock.
light_data = bpy.data.lights.new(name="New Light", type='POINT')

# Create new object with our light datablock.
light_object = bpy.data.objects.new(name="New Light", object_data=light_data)

# Link light object to the active collection of current view layer,
# so that it'll appear in the current scene.
view_layer.active_layer_collection.collection.objects.link(light_object)

# Place light to a specified location.
light_object.location = (5.0, 5.0, 5.0)

# And finally select it and make it active.
light_object.select_set(True)
view_layer.objects.active = light_object

新建立方体和json文件,并写入物体信息在文件上

#/写入
 my_objects = []#列表
 scene = bpy.context.scene
 for obj in scene.objects:
   if obj.type == "MESH":
     #存储立方体的信息
     obj_info = {}#字典
     obj_info["name"] = obj.name
     obj_info["position"] = [obj.location.x, obj.location.y, obj.location.z]#位置
     obj_info["direction"] = [obj.rotation_euler.x, obj.rotation_euler.y, obj.rotation_euler.z]#旋转
     obj_info["scale"] = [obj.scale.x, obj.scale.y, obj.scale.z]#缩放
     print(obj_info)
     my_objects.append(obj_info)
#打印出来立方体信息
 print(my_objects)


# #存到文件里面
# filename = "D:/graduate students/blender-python/python/my_info_file.json"
# f = open(filename, "w")#写入文件w
# json.dump(obj_info, f)
#/

用json文件存储读取的立方体信息

可以用先print立方体的信息在将这些信息放入自己建立的json文件中
json都是双引号,ctrl+f,找替换,全部替换
image.png
image.png
输出的三个立方体再blender中的位置,旋转,缩放

[
  {
    "name": "Cube",
    "position": [
      0.0,
      5.0,
      0.0
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  },
  {
    "name": "NewCube",
    "position": [
      -1.3625563383102417,
      5.549374580383301,
      3.793199062347412
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  },
  {
    "name": "NewCube.001",
    "position": [
      0.0,
      0.0,
      0.0
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  }
]

完整流程

image.png

import bpy
import json

#/读取+创建立方体
filename = "D:/graduate students/blender-python/python/my_objects.json"
f = open(filename,"r")
my_objects = json.load(f)
cube_old_obj=None
scene = bpy.context.scene
for obj in scene.objects:
	if obj.type=="MESH":
		cube_old_obj=obj
view_layer = bpy.context.view_layer
for obj_info in my_objects:
	mesh_data = bpy.data.meshes.new_from_object(cube_old_obj)
	cube_object = bpy.data.objects.new(name=obj_info["name"], object_data=mesh_data)
	cube_object.location=(obj_info["position"][0],obj_info["position"][1],obj_info["position"][2])
	cube_object.rotation_euler=(obj_info["direction"][0],obj_info["direction"][1],obj_info["direction"][2])
	cube_object.scale=(obj_info["scale"][0],obj_info["scale"][1],obj_info["scale"][2])
	view_layer.active_layer_collection.collection.objects.link(cube_object)
#/

json文件名对应代码中的my_objects.json

[
  {
    "name": "Cube",
    "position": [
      0.0,
      5.0,
      0.0
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  },
  {
    "name": "NewCube",
    "position": [
      -1.3625563383102417,
      5.549374580383301,
      3.793199062347412
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  },
  {
    "name": "NewCube.001",
    "position": [
      0.0,
      0.0,
      0.0
    ],
    "direction": [
      0.0,
      0.0,
      0.0
    ],
    "scale": [
      1.0,
      1.0,
      1.0
    ]
  }
]

代码思路:
image.png
运行结果:
1运行、2立方体必须存在,才能运行代码,出现其他三个立方体
image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值