Tuesday, May 10, 2011

Leg! Leg! Leg!

Today's my first trying is to make vertex group for end faces of extruded spines. If I could this, I'll make flowers to each spine top! :-)

And second, to make 'legs' on 'leg' vertex group. This is same as 'spine', not so difficult. This is the code,

<CODE>

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


# spineStep ---------------------------------------->
def spineStep(dr, sr, rr):
distRandom = random.random()*dr
bpy.ops.transform.shrink_fatten(value=-0.2+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)


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


# 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()


# 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(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')


# Legs! -------------------------------------------->
def legs(dr, sr, rr):


# 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()


# 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(5):
     
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(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')


# select faces from Groups -------------------------------->
def selPart(selGroup):
bpy.ops.object.vertex_group_set_active(group=selGroup)
bpy.ops.object.vertex_group_select()

# 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'


# Select the vertex group 'back' ------------------------------->
selPart('back')


# 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')


# spine(randomness dist, scale, rot) ----------------------------------------->
spine(0.1, 0.3, 0.5)


# Select the vertex group 'leg' ------------------------------->
selPart('leg')


# 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')


# legs(randomness dist, scale, rot) ----------------------------------------->
legs(0.1, 0.3, 0.5)

</CODE>

The result is this. Tips of spines are grouped to 'spineTop' group. And ten legs are generated!


One thing, I'd like to try to delete un-necessary, generated 'mygroup.~' vertex groups.

No comments:

Post a Comment