2012-01-11 115 views
1

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

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轴旋转]绘制圆柱体,我在哪里出现严重错误?我如何纠正这一点。非常感谢你的帮助。

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

+2

你有错误? – 2012-01-11 12:40:45

+1

请注明Blender版本,因为在2.49搅拌器使用Python 3与不同的API – jsbueno 2012-01-11 12:53:12

+0

添加了要求的详细信息。 – chaitu 2012-01-11 14:59:09

回答

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

+0

旋转矩阵怎么样?与原始位置有偏移旋转。 – chaitu 2012-01-13 06:46:12

+0

在这种情况下,请查看包含各种操作的'mathutils'库:http://www.blender.org/documentation/blender_python_api_2_56_0/mathutils.html – 2012-01-13 13:37:08