In this tutorial I will recreate the same box but with a more complex source code.
In the previous tutorial I used the same source code several times...
obj = bpy.ops.mesh.primitive_plane_add(size=2,
calc_uvs=True,
enter_editmode=False,
align='CURSOR',
location=location,
rotation=(0, 0, 0),
scale=(0,0,0)
)
# rename the object
bpy.context.object.name = obj_name
# return the object reference
return bpy.context.object
It is easier to understand the steps taken and then move on to optimizing them in complex forms.
Obviously a presentation of the source code in this tutorial will show you the differences.
I will add that the source code does not include scalar transformations and is limited to a box with size 1.
import bpy
def create_plane_XYZ(loc, obj_name):
#define the pi
pi = 3.1415926
# this is a definition like a tuple
rot = (0,0,0)
# need to convert it to a list in order to add new values
rot_list = list(rot)
# this ang variable will rotate obj to the 90-degree angle
ang = -90*(pi/180)
ang_inc = 45*(pi/180)
if loc[0] < 0:
rot_list[1]=ang
if loc[0] > 0:
rot_list[1]=ang
if loc[1] < 0:
rot_list[0]=ang
if loc[1] > 0:
rot_list[0]=ang
# this check if the two value from position of plane is not zero
if loc[1] != loc[2] != 0:
rot_list[0]=ang_inc
# this convert a list back to tuple
rot=tuple(rot_list)
# this create the plane
obj = bpy.ops.mesh.primitive_plane_add(size=2,
calc_uvs=True,
enter_editmode=False,
align='CURSOR',
location=loc,
rotation= rot,
scale=(0,0,0)
)
# rename the object
bpy.context.object.name = obj_name
# return the object reference
return bpy.context.object
# this will create a plane on X and translate with -1 on Y
planeX001 = create_plane_XYZ((0,-1,0), "Plane-X")
# this will create a plane on X and translate with 1 on Y
planeX002 = create_plane_XYZ((0,1,0), "Plane+X")
# this will create a plane on Y and translate with -1 on X
planeY001 = create_plane_XYZ((-1,0,0), "Plane-Y")
# this will create a plane on X and translate with 1 on Y
planeY002 = create_plane_XYZ((1,0,0), "Plane+Y")
# this will create a plane with 45 degree because two value on tuple is not zero
planeZ001 = create_plane_XYZ((0,-0.25,1.66), "Plane-Y+Z")