废话少说,直接上代码。
参考链接
https://blender.stackexchange.com/questions/21589/how-can-i-add-a-cube
https://blender.stackexchange.com/questions/157531/blender-2-8-python-add-texture-image
https://blender.stackexchange.com/questions/57446/create-named-object
https://blender.stackexchange.com/questions/134826/how-do-you-specify-the-name-of-a-primitive-created-via-the-python-api
https://blender.stackexchange.com/questions/38618/selecting-an-object-via-scripting-in-2020
https://blender.stackexchange.com/questions/61868/scaling-a-newly-made-cube
code
import bpy
from bpy import context, data, ops
# 创建几何体
bpy.ops.mesh.primitive_cube_add(location=(1,1,1),rotation=(0.1,0.1,0.3))
context.view_layer.objects.active.name = "cube_1"
bpy.ops.mesh.primitive_ico_sphere_add(location=(2,2,2),rotation=(0.1,0.1,0.3))
context.view_layer.objects.active.name = "sphere_1"
bpy.ops.mesh.primitive_cylinder_add(location=(3,3,3),rotation=(0.2,0.1,0.3))
context.view_layer.objects.active.name = "cylinder_1"
mesh = bpy.ops.mesh.primitive_cone_add(location=(4,4,4),rotation=(0.2,0.1,0.3))
context.view_layer.objects.active.name = "cone_1"
bpy.data.objects["cube_1"].select_set(True)
bpy.context.view_layer.objects.active = bpy.data.objects['cube_1']
bpy.context.view_layer.objects.active.scale = (1, 2, 3)
# 读取纹理
mat = bpy.data.materials.new(name="New_Mat")
mat.use_nodes = True
bsdf = mat.node_tree.nodes["Principled BSDF"]
texImage = mat.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.load("C:\\Users\\Kevin\\Desktop\\3333.jpg")
mat.node_tree.links.new(bsdf.inputs['Base Color'], texImage.outputs['Color'])
# 设定纹理
ob = context.view_layer.objects.active
if ob.data.materials:
ob.data.materials[0] = mat
else:
ob.data.materials.append(mat)
结果