Thursday, May 12, 2011

Eyes, Arms, and next?

It's like an evolution, the object has legs, arms, spine, eyes(Ohm type and Chameleon type!). This model isn't a work. It's just a technical verification model, but it's cute, isn't it? (^o^)


Add 'subSurf' modifier, how about this?


<CODE>

# First import bpy to get access to well... everything in blender
import bpy
import math, mathutils, random

# spineStep ---------------------------------------->
def spineStep(s_facter, dr, sr, rr):
distRandom = random.random()*dr
bpy.ops.transform.shrink_fatten(value=(-0.2*s_facter)+distRandom, 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)
sizeRandom = random.random()*sr
bpy.ops.transform.resize(value=(0.4+sizeRandom, 0.4+sizeRandom, 0.4+sizeRandom), 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)
rotRandom = random.uniform(-rr,rr)
bpy.ops.transform.rotate(value=(rotRandom,), axis=(random.uniform(-0.05,0.05), random.uniform(-0.05,0.05), random.uniform(-0.05,0.05)), 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)
# legStep ---------------------------------------->
def legStep(dr, sr, rr):
bpy.ops.transform.shrink_fatten(value=-dr, 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=(sr, sr, sr), 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=(rr*rotDir,), axis=(-0, 1, -3.42285e-08), 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)

# armStep ---------------------------------------->
def armStep(dr, sr, rr):
distRandom = random.random()*dr
bpy.ops.transform.shrink_fatten(value=-dr, 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)
sizeRandom = random.random()*sr
bpy.ops.transform.resize(value=(sr, sr, sr), 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)
rotRandom = random.uniform(-rr,rr)
bpy.ops.transform.rotate(value=(rotRandom,), axis=(random.uniform(-0.05,0.05), random.uniform(-0.05,0.05), random.uniform(-0.05,0.05)), 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)
# eyeCamStep ---------------------------------------->
def eyeCamStep(dr, sr, rr):
bpy.ops.transform.shrink_fatten(value=-dr, 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=(sr, sr, sr), 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]*center[2] > 0.0:
rotDir = 1.0
else:
rotDir = -1.0
bpy.ops.transform.rotate(value=(rr*rotDir,), axis=(-0, 1, -3.42285e-08), 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)

# Spine! ------------------------------------------------>
def spine(vGroup, s_facter, dr, sr, rr):

# 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

# Lets extrude/shrink/scale 5 times (hacky non commented code)
for i in range(3):
     
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')

spineStep(s_facter, dr, sr, rr)
bpy.ops.object.vertex_group_set_active(group='spineTop')
bpy.ops.object.vertex_group_assign(new=False)
bpy.ops.mesh.select_all(action='DESELECT')

# Spine!(END) -------------------------------------------------->

# Legs! ------------------------------------------------------->
def legs(vGroup):

# 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

# Leg Shape
step = 6
legDist = [0.1, 0.4, 0.02, 0.02, 0.4, 0.1]
legScale = [0.3, 2.0, 0.8, 1.2, 0.4, 0.1]
legRot = [-0.3, -0.8, 0.0, 0.0, 1.0, 0.0]
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')

dr = legDist[i]
sr = legScale[i]
rr = legRot[i]
legStep(dr, sr, rr)

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

# Arms! ------------------------------------------------------->
def arms(vGroup, s_facter):

# 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

# Arm Shape
step = 5
legDist = [0.1, 0.3, 0.3, 0.3, 0.3]
legScale = [0.5, 2.0, 1.0, 0.5, 0.1]
legRot = [0.3, 0.3, 0.3, 0.3, 0.3]
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')

dr = legDist[i]
if i==0:
sr = s_facter
else:
sr = legScale[i]
rr = legRot[i]
armStep(dr, sr, rr)

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

# Eye_Ohm! ('Ohm' type eye-------------------------------------->
def eyeOhm(vGroup, s_facter):

# 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

# eyeOhm Shape
step = 5
legDist = [0.06, 0.03, 0.0, 0.1, 0.02]
legScale = [1.0, 0.8, 0.95, 0.7, 0.2]
legRot = [0.0, 0.0, 0.0, 0.0, 0.0]
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')

dr = legDist[i]*s_facter
sr = legScale[i]
rr = legRot[i]
legStep(dr, sr, rr)

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

# Eye_Cam! ('Camereon' type eye-------------------------------------->
def eyeCam(vGroup, s_facter):

# 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

# eyeCam Shape
step = 7
legDist = [0.06, 0.03, 0.0, 0.3, 0.1, 0.0, -0.1]
legScale = [1.0, 0.8, 0.95, 0.6, 0.3, 0.8, 1.0]
legRot = [0.0, 0.0, -0.2, 0.0, 0.0, 0.0, 0.0, 0.0]
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')

dr = legDist[i]*s_facter
sr = legScale[i]
rr = legRot[i]
eyeCamStep(dr, sr, rr)

# bpy.ops.object.vertex_group_set_active(group='legTop')
# bpy.ops.object.vertex_group_assign(new=False)
bpy.ops.mesh.select_all(action='DESELECT')
# eyeCam!(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!!! ----------------------------------------------------------------------------->

# spine(area, s_facter, randomness dist, scale, rot) 
spine('back', 0.5, 0.0, 0.0, 0.1)
# legs(area) 
legs('leg')
# arm(area, scale)
arms('arm', 0.3)
# eyeOhm(area, scale)
eyeOhm('eye_1', 0.5)
# eyeCam(area, scale)
eyeCam('eye_2', 0.5)

</CODE>


What is next parts? Maybe 'mouth', 'wing', 'tail', 'nose', 'fin',,,. Any other?

No comments:

Post a Comment