Saturday, May 14, 2011

Tidy up more. And make mouth.

Today's work, tidy up the some overlapping code. Therefore, the code are very clean and easy to see it. There are two main function, 'transformFaces', and 'transformRegion'. These could make many kind of shape by parameters. 
This is the code,

<CODE>

# First import bpy to get access to well... everything in blender
import bpy
import math, mathutils, random
# transStep ---------------------------------------->
def transStep(dA, sA, rA, dR, sR, rR, axis_x, axis_y, axis_z):
bpy.ops.transform.shrink_fatten(value=-dA+(dR*random.uniform(-0.5, 0.5)), mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)
bpy.ops.transform.resize(value=(sA+(sR*random.uniform(-0.5, 0.5)), sA+(sR*random.uniform(-0.5, 0.5)), sA+(sR*random.uniform(-0.5, 0.5))), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.0762767, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), texture_space=False, release_confirm=False)\
# Get the mesh
me = ob.data
# Loop through the faces to find the center
for f in me.faces:
if f.select == True:
center = f.center
if center[0] > 0.0:
rotDir = 1.0
else:
rotDir = -1.0
bpy.ops.transform.rotate(value=(rA*rotDir+(rR*random.uniform(-0.5, 0.5)),), axis=(axis_x, axis_y, axis_z), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.0762767, snap=False, snap_target='CLOSEST', snap_point=(0, 0, 0), snap_align=False, snap_normal=(0, 0, 0), release_confirm=False)

# transformFaces! ------------------------------------------------------->
def transformFaces(vGroup, step, distArray, distRandom, scaleArray, scaleRandom, rotArray, rotRandom, axis):

# Select the vertex group
selPart(vGroup)

# Go into object mode so that we are sure we have the current list of faces 
bpy.ops.object.mode_set(mode='OBJECT')

# Make a nice list to keep the vertex groups in
groupList = []

# Now loop through all the faces
for i, f in enumerate(ob.data.faces):

# If this face is selected~
if ob.data.faces[i].select == True:

# Create a new vertex group!
newGroup = ob.vertex_groups.new('mygroup')
groupList.append(newGroup)

# Get all the vertices in the current face and add them to the new group
for v in f.vertices:
newGroup.add([v], 1.0, 'REPLACE')
# Now we loop through the groups and do what we want.
for g in groupList:

# Make sure nothing is selected
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')

# Make the group in our list the active one
ob.vertex_groups.active_index = g.index

# Select all the verts in the active group
bpy.ops.object.vertex_group_select()

bpy.ops.object.vertex_group_remove(all=False)

# AND NOW WE CAN DO OUR STUFF... little test example follows

# Shape
for i in range(step):
     
bpy.ops.mesh.extrude(type='REGION')

# An added trick... remove the extruded face from all vertex groups after the first extrusion (i == 0)
# This way it can't get extruded again
# Because the edge of the first face can be part of multiple groups
if not i:
bpy.ops.object.vertex_group_remove_from(all=True)
         
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

dA = distArray[i]
sA = scaleArray[i]
rA = rotArray[i]
dR = distRandom
sR = scaleRandom
rR = rotRandom
axis_x = axis[0]
axis_y = axis[1]
axis_z = axis[2]
transStep(dA, sA, rA, dR, sR, rR, axis_x, axis_y, axis_z)

bpy.ops.object.vertex_group_set_active(group='legTop')
bpy.ops.object.vertex_group_assign(new=False)
bpy.ops.mesh.select_all(action='DESELECT')
# transformFaces!(END) --------------------------------------------------<

# transformRegion! ---------------------------------------------------->
def transformRegion(vGroup, step, distArray, distRandom, scaleArray, scaleRandom, rotArray, rotRandom, axis):
selPart(vGroup)
# Loop through all the areas
for area in areas:

# See if the current area is a 3D view
if area.type == 'VIEW_3D':

# Set the pivot point to individual origins
area.active_space.pivot_point = 'BOUNDING_BOX_CENTER'
# Shape
for i in range(step):
     
bpy.ops.mesh.extrude(type='REGION')

# An added trick... remove the extruded face from all vertex groups after the first extrusion (i == 0)
# This way it can't get extruded again
# Because the edge of the first face can be part of multiple groups
if not i:
bpy.ops.object.vertex_group_remove_from(all=True)
         
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

dA = distArray[i]
sA = scaleArray[i]
rA = rotArray[i]
dR = distRandom
sR = scaleRandom
rR = rotRandom
axis_x = axis[0]
axis_y = axis[1]
axis_z = axis[2]
transStep(dA, sA, rA, dR, sR, rR, axis_x, axis_y, axis_z)
bpy.ops.mesh.select_all(action='DESELECT')
# transformRegion(END) ----------------------------------------------------<
# select faces from Groups
def selPart(vGroup):
bpy.ops.object.vertex_group_set_active(group=vGroup)
bpy.ops.object.vertex_group_select()

# -- Set Up(START) ------------------------------------------> 
# Now we want to get the active object
# To do this we go through bpy.context... because that is basicly "what you are working on right now"
# bpy.data could work too, but then you'd need to do more work to find your current active object
ob = bpy.context.active_object

# A new trick... lets go into edit mode and deselect everything ('SELECT' works too)
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='DESELECT')

# Get the screen areas we are working in right now
areas = bpy.context.screen.areas

# Loop through all the areas
for area in areas:

# See if the current area is a 3D view
if area.type == 'VIEW_3D':

# Set the pivot point to individual origins
area.active_space.pivot_point = 'INDIVIDUAL_ORIGINS'

# -- Set Up(END) ------------------------------------------< 

# Finally, generate all parts!!! ----------------------------------------------------------------------------->
# transformFaces(vGroup, step, distArray, distRandom, scaleArray, scaleRandom, rotArray, rotRandom, axisArray) 
transformFaces('back', 3, [0.05, 0.1, 0.15], 0.0, [0.5, 0.5, 0.5], 0.0, [0.0, 0.0, 0.0], 0.0, [-0, 1, -3.42285e-08])
transformFaces('arm', 5, [0.1, 0.3, 0.3, 0.3, 0.3], 0.0, [0.3, 2.0, 1.0, 0.5, 0.1], 0.0, [0.3, 0.3, 0.3, 0.3, 0.3], 0.0, [-0, 1, -3.42285e-08])
transformFaces('eye_1', 5, [0.06, 0.015, 0.0, 0.05, 0.01], 0.0, [1.0, 0.8, 0.95, 0.7, 0.2], 0.0, [0.0, 0.0, 0.0, 0.0, 0.0], 0.0, [-0, 1, -3.42285e-08])
transformFaces('eye_2', 7, [0.06, 0.015, 0.0, 0.2, 0.05, 0.0, -0.1], 0.0, [1.0, 0.8, 0.95, 0.5, 0.3, 0.8, 1.0], 0.0, [0.0, 0.0, -0.2, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0, [-0, 1, -3.42285e-08])
transformFaces('leg', 6, [0.1, 0.4, 0.02, 0.02, 0.4, 0.1], 0.0, [0.3, 2.0, 0.8, 1.2, 0.4, 0.1], 0.0, [-0.3, -0.8, 0.0, 0.0, 1.0, 0.0], 0.0, [-0, 1, -3.42285e-08])
# transformRegion(vGroup, step, distArray, distRandom, scaleArray, scaleRandom, rotArray, rotRandom, axisArray) 
transformRegion('mouth', 4, [0.1, 0.2, 0, -0.1], 0.0, [0.9, 0.6, 0.8, 1.0], 0.0, [-0.5, 0.0, 0.0, 0.0], 0.0, [-1, -2.22045e-16, -0])

</CODE>

The result is this.


No comments:

Post a Comment