【问题标题】:Place objects randomly within a range in Blender Python在 Blender Python 的范围内随机放置对象
【发布时间】:2020-07-08 03:40:32
【问题描述】:

我有 10 个对象,我想将这些对象随机放置在 x、y、z 范围内(具有最小值和最大值)。类似的东西:

import bpy
import bpy_extras
import random
import math

bpy.data.objects["Sasso_1"].select_set(True)
bpy.ops.rigidbody.objects_add(type='ACTIVE')
bpy.context.object.rigid_body.mass = 0.25

for obj in bpy.data.collections['Sassi'].all_objects:
    obj.select_set(True)
    bpy.ops.rigidbody.object_settings_copy()

bpy.ops.transform.translate(value=random.uniform(12,27), orient_axis='X')
bpy.ops.transform.translate(value=random.uniform(-15,15), orient_axis='Y')
bpy.ops.transform.translate(value=random.uniform(13,28), orient_axis='Z')

for obj in bpy.data.collections['Sassi'].all_objects:
    bpy.ops.transform.rotate(value=random.uniform(0,360), orient_axis='X')
    bpy.ops.transform.rotate(value=random.uniform(0,360), orient_axis='Y')

在我的脚本中,对象“sassi”以随机方式旋转和平移,但不在我想要的范围内。 结果是对象是不可预测的。 Tnx 引起注意。 :)

【问题讨论】:

  • 请分享一个完全可运行的脚本。进一步指出哪里出了问题
  • 嗨 Vasco,我更改了问题中的脚本。对不起,我是新来的错误。 Blender 随机移动对象,选择 12 和 27(在 x 上)、-15 和 15(在 y 上)等之间的数字。我想在 12 和 27 之间(在 x 上)的范围内移动对象,但我的意思是 x 是位置的范围,而不是翻译值。
  • 对象翻译到什么范围?

标签: python 3d blender


【解决方案1】:

翻译操作符做一个相对运动: https://docs.blender.org/api/blender_python_api_2_67_1/bpy.ops.transform.html

在您的情况下,您想设置一个绝对位置。如何做到这一点在这里解释: https://blender.stackexchange.com/a/89581

bpy.data.objects["Cube"].location.x = 3.0
bpy.data.objects["Cube"].location.y = 2.0
bpy.data.objects["Cube"].location.z = 1.0
bpy.context.scene.update()

【讨论】:

    【解决方案2】:

    我用 randomize trasform 函数解决了这个问题。 这是这样做的代码:

    bpy.ops.object.randomize_transform(random_seed = random.randint(0,100), loc=(6, 6, 6), scale=(1, 1, 1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-16
      • 2021-02-10
      • 2011-12-16
      • 2015-07-09
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多