【问题标题】:Blender python script for a primitive blender exporter用于原始搅拌机导出器的搅拌机 python 脚本
【发布时间】:2012-02-07 19:29:25
【问题描述】:

我正在尝试编写一个非常基本的导出到搅拌机(从原始形状)脚本。我必须以不同的角度和位置绘制圆柱体。我有偏移位置和尺寸的信息。

import bpy
import bgl
from mathutils import *
from math import *

material = bpy.data.materials.new('red')
material.diffuse_color = (1.0,0.0,0.0)


def draw_cylinder(name,material,radius,depth,location,rotation,offsetPosition,offsetAngle):

    bgl.glRotatef(*offsetAngle[:4]) 
    bgl.glTranslatef(*offsetPosition[:3])

    bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=depth, location=location, rotation=rotation)

    Cylinder = bpy.context.active_object
    Cylinder.name = name
    Cylinder.active_material = material

    bgl.glTranslatef(*[i*-1 for i in offsetPosition[:3]])
    bgl.glRotatef(*[i*-1 for i in offsetAngle[:4]])

    return Cylinder

cmpt = draw_cylinder('first',material,radius=1,depth=2,location=(-1,0,0),rotation=(pi/2,0,0),offsetPosition=(10,2,7),offsetAngle=(pi/2,0,1,0))

这不会在 (9,2,7) 处绘制圆柱体 [也不会沿 y 轴旋转] 我哪里错了?我该如何纠正这个。非常感谢您的帮助。

编辑:使用 Blender 2.60 版(python 交互式控制台 3.2.2) 输出显示圆柱体,位于 (-1,0,0)。我希望/需要它在 (9,2,7) (location+offsetPosition)

【问题讨论】:

  • 请说明 Blender 版本,因为 2.49 之后的 Blender 使用 Python 3 和不同的 API
  • 添加了询问的详细信息。

标签: python blender


【解决方案1】:

在函数draw_cylinder中,需要将两个向量相加:

pos = (
    location[0]+offsetPosition[0],
    location[1]+offsetPosition[2],
    location[1]+offsetPosition[2],
)

然后

bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=depth, location=pos, rotation=rotation)

[编辑]如果您需要更复杂的操作,请查看mathutils library

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-06-15
  • 2020-01-06
  • 2016-05-24
  • 2012-02-02
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多