2012-01-11 203 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