目标
新建立方体,然后将立方体的信息读取出来,将立方体的方位、旋转角度、缩放信息放入json类型文档中;
再新建一个blender2文件,通过读取文档,新建出之前的立方体;
前期工作
文件名改成一致的
直接下载blender的插件解决无法解析导入“bpy”的报错,警告还是在的,但是运行代码没问题的
如何新建物体
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,找替换,全部替换
输出的三个立方体再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
]
}
]
完整流程
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
]
}
]
代码思路:
运行结果:
1运行、2立方体必须存在,才能运行代码,出现其他三个立方体