Reputation: 719
I am trying to write a very basic export to blender (from primitive shapes) script. 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? 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). I expect/need it to be at (9,2,7) (location+offsetPosition)
Upvotes: 1
Views: 1900
Reputation: 328724
In the function draw_cylinder
, you need to add the two vectors:
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.
Upvotes: 1