[英]Blender python script for a primitive blender exporter
I am trying to write a very basic export to blender (from primitive shapes) script. 我正在尝试编写一个非常基本的导出到Blender(从原始形状)的脚本。 I have to draw cylinders, at various angles and positions.
我必须绘制各种角度和位置的圆柱体。 I have information of offset position, and dimensions.
我有偏移位置和尺寸的信息。
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))
This does not draw the cylinder at (9,2,7) [nor rotated along y axis] where am i terribly wrong? 这不会在(9,2,7)[不沿y轴旋转]处拉伸圆柱体,这在哪里我非常错误? How can i correct this.
我该如何纠正。 Much Appreciate your help.
非常感谢您的帮助。
EDIT: Using Blender version 2.60 (python interactive console 3.2.2) Output shows the cylinder, at (-1,0,0). 编辑:使用Blender 2.60版(python交互式控制台3.2.2),输出显示圆柱体,位于(-1,0,0)。 I expect/need it to be at (9,2,7) (location+offsetPosition)
我希望/需要它在(9,2,7)(location + offsetPosition)
In the function draw_cylinder
, you need to add the two vectors: 在函数
draw_cylinder
,您需要添加两个向量:
pos = (
location[0]+offsetPosition[0],
location[1]+offsetPosition[2],
location[1]+offsetPosition[2],
)
and then 接着
bpy.ops.mesh.primitive_cylinder_add(radius=radius, depth=depth, location=pos, rotation=rotation)
[EDIT] If you need more complex operations, have a look at the mathutils library . [编辑]如果您需要更复杂的操作,请查看mathutils库 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.