Blender 脚本不起作用

问题描述 投票:0回答:2

我想用 python 在 Blender 中制作一个 Side Scroller。有人可以向我解释一下为什么这个脚本不起作用吗?

import bge

def main():

    cont = bge.logic.getCurrentController()
    player = cont.owner

    scene = bge.logic.getCurrentScene()

    keyboard = bge.logic.keyboard

    if bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.DKEY]:
        player.localPosition.x += 0.1

    elif bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.AKEY]:
        player.localPosition.x += -0.1

    elif bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]:
        player.localPosition.z += 0.5

main()
python blender
2个回答
0
投票
import bpy
import math

# Clear existing objects
bpy.ops.object.select_all(action='DESELECT')
bpy.ops.object.select_by_type(type='MESH')
bpy.ops.object.delete()

# Create base mesh
bpy.ops.mesh.primitive_cylinder(radius=1, depth=2, location=(0, 0, 0))

# Access the newly created cylinder object
obj = bpy.context.active_object

# Scale the cylinder to create the body proportions
obj.scale = (0.5, 0.5, 1.5)

# Apply scale to avoid issues with further modifications
bpy.ops.object.transform_apply(scale=True)

# Subdivide the mesh for smoother deformation
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.subdivide(number_cuts=6)
bpy.ops.object.mode_set(mode='OBJECT')

# Apply smooth shading
bpy.ops.object.shade_smooth()

# Rename the object
obj.name = "Draculaura_Body_Base"

# Create shape keys for further modifications
bpy.ops.object.shape_key_add(from_mix=False)

# Rename the first shape key to "Basis"
obj.data.shape_keys.key_blocks[0].name = "Basis"

# Add shape key for adjusting proportions
bpy.ops.object.shape_key_add(from_mix=False)

# Rename the second shape key to "Proportions"
obj.data.shape_keys.key_blocks[1].name = "Proportions"

# Set up Draculaura-like proportions (you may need to adjust these values)
proportions_key = obj.data.shape_keys.key_blocks["Proportions"]
for v in obj.data.vertices:
    if v.co.z > 0.5:
        v.co *= 1.1  # Adjust upper body
    else:
        v.co *= 0.9  # Adjust lower body

# Example: Add wings, you can customize further based on your requirements
bpy.ops.mesh.primitive_cube_add(size=1, location=(0, 0, 2.5))
wings_obj = bpy.context.active_object
wings_obj.scale = (0.2, 0.2, 0.2)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.subdivide(number_cuts=2)
bpy.ops.object.mode_set(mode='OBJECT')
wings_obj.name = "Wings"
wings_obj.parent = obj

# Select the main object
bpy.context.view_layer.objects.active = obj
obj.select_set(True)

-1
投票

根据为我工作的脚本和您所说的错误,看来您使用脚本的方式是错误的。 bge 模块仅在游戏引擎运行时可用,听起来您正在尝试通过单击搅拌机文本编辑器中的运行脚本按钮将其作为普通搅拌机脚本运行。

要在搅拌机的游戏引擎中使用 python 脚本,您需要向玩家对象添加逻辑块,并将脚本分配给连接有传感器的 python 控制器,因为您的脚本直接从键盘读取,您可以添加一个始终启用脉冲的传感器。

© www.soinside.com 2019 - 2024. All rights reserved.