Blender:创建基本几何体及自动贴图

废话少说,直接上代码。

参考链接

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)

结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值