Wednesday, May 11, 2011

Clean Up, and Next.

I did the clean up the code a little bit, and succeeded to remove the used temporary 'mygroup'.


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

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

# rotDir = 1.0 --- use in a future
bpy.ops.transform.rotate(value=(rr,), axis=(0, 0, 1), 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, 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(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 = 5
legDist = [0.1, 0.4, 0.02, 0.02, 0.3]
legScale = [0.5, 2.0, 0.8, 1.2, 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]
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) --------------------------------------------------<

# 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, randomness dist, scale, rot) 
spine('back', 0.1, 0.3, 0.5)
# legs(area) 
legs('leg')
legs('spineTop')

</CODE>

The result of this code is this.


And now, I'd like to bend the legs. In a leg making sequence, get the center pos of a selected face, and decide the rot direction [+] or [-] by X pos. So, how to get the center pos of a selected face?

P.S. With Dolf's help, the problem was solved!

<CODE>

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

</CODE>

And, set the leg shape parameter to this.

<CODE>

# Leg Shape
step = 5
legDist = [0.1, 0.4, 0.02, 0.02, 0.3]
legScale = [0.5, 2.0, 0.8, 1.2, 0.2]
legRot = [-0.3, 0.5, 0.0, 0.0, 0.0]

</CODE>

The result is,


It's very very good!  \(^o^)/

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.

Monday, May 9, 2011

Def functions to clear the code, and Add randomness.

Make some functions for clearing the code, and add randomness for spine operation.

<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(2):
     
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)


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

</CODE>

This is the result. The spines are generated with some randomness.


And with Subsurf, like this. It seems organic, a little bit.


The next plan is to make other module, legs, eyes, tail, wing,,, maybe.

Sunday, May 8, 2011

Selected from vertex groups

I tried another way to select faces with using vertex groups.

I made two vertex groups on the object, 'back' and 'leg'. And this code select vertices assigned 'back' group.


<CODE>


# First import bpy to get access to well... everything in blender
import bpy


# 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'
bpy.ops.object.vertex_group_set_active(group='back')
bpy.ops.object.vertex_group_select()


</CODE>


But I didn't success to extrude and resize the selected faces. If I could connect to the Dolf's script on May 5th well, I'll advance more one step.

P.S.
This part of Dolf's script, 'if ~', could be change to 'a face is already selected', I think 'mygroup' could be made from the selected faces. How about it?

<CODE>


# Now loop through all the faces
for i, f in enumerate(ob.data.faces):
    
    # The following is the same as i % 5 == 0, but faster.
    if not i % 5:
        
        # 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')
         
</CODE>

And I re-write this part to this,

<CODE>


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


</CODE>

Yes! I got a good result! It's like this.


How about this? It's a very big step for me. The all code is this.

<CODE>

# First import bpy to get access to well... everything in blender
import bpy

# 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'
bpy.ops.object.vertex_group_set_active(group='back')
bpy.ops.object.vertex_group_select()

# 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()
    
    # 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')
        
        bpy.ops.transform.shrink_fatten(value=-0.1, 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=(0.8, 0.8, 0.8), 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)


</CODE>

Friday, May 6, 2011

One answer from Dolf.

Thanks Dolf, Your code worked.
I have to think more and more. But now We have short holidays in Japan, (We called 'Golden Week' but as you know 'Husbands' has no free time in Holidays!) So, I'll get some time to think code next week. (T_T)

<CODE>

#Let's make a completely new script where we create a series of vertex groups, that we assign faces to.


#So the idea is... for every "extrusion" we want to do... we create a vertex group.


# Just like before we loop through all the faces
import bpy
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')


# 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):
    
    # The following is the same as i % 5 == 0, but faster.
    if not i % 5:
        
        # 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()
    
    # 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')
        
        bpy.ops.transform.shrink_fatten(value=-0.1, 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=(0.8, 0.8, 0.8), 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)
 
</CODE>

Monday, May 2, 2011

Question: Bug? or Something wrong?

I tried to write simple description for 'gene' system. I made some function for replacing long operation code with 'def'.

At first, I use many 'Spine' shape for my work, so I define 'gene_Spine', this is the code.

<CODE>


# Here is how we do things separately for every face!
# In a very very simple way.
# Get access to blender through bpy and get the active object


import bpy
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')

# Transform selected faces. tValue = move
def fMove(tValue):
bpy.ops.transform.shrink_fatten(value=tValue, 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)

# Resize
def fScale(sValue):
bpy.ops.transform.resize(value=(sValue, sValue, sValue), 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)

# Rotate
def fRot(rValue):
bpy.ops.transform.rotate(value=(rValue,), axis=(-0.929842, -0.0471948, -0.364919), 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)
# gene_Spine
def gene_Spine():
# Extrude the current Face (We use 'REGION' because 'FACES' only works with multiple faces selected)
bpy.ops.mesh.extrude(type='REGION')

# Here's the really hacky part... to make sure the shrink works correct.. we go in and out of object mode
# That way the mesh "updates" nicely after the extrude... I do not want to do this... but it works
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

fMove(-0.05)
fScale(0.5)
fRot(-0.0)

# Extrude the current Face (We use 'REGION' because 'FACES' only works with multiple faces selected)
bpy.ops.mesh.extrude(type='REGION')

# Here's the really hacky part... to make sure the shrink works correct.. we go in and out of object mode
# That way the mesh "updates" nicely after the extrude... I do not want to do this... but it works
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.mode_set(mode='EDIT')

fMove(-0.1)
fScale(0.3)
fRot(-0.0)

# Now just like before we loop through all the faces, but we don't just select/deselect, we add them to a list...

# 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 fresh new empty list to put your faces in
faceList = []

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

# The index of every 5th face will be added to the list
# if i % 5 == 0:
faceList.append(f.index)
# NOW WE LOOP THROUGH THE LIST
for index in faceList:
# Make sure we are in object mode so we can select the current face in the list
bpy.ops.object.mode_set(mode='OBJECT')
# Select this face
print('selecting',index)
ob.data.faces[index].select = True
# NOW WE CAN DO WHATEVER WE WANT TO THIS FACE
# Go into edit mode for editing!
bpy.ops.object.mode_set(mode='EDIT')
# Spine test
gene_Spine()
# Now if we are done with this face... we deselect it, and anything else that might be selected (outside the 5 long loop)
bpy.ops.mesh.select_all(action='DESELECT')



</CODE>

Now, I got this result.


Yes, good spines. But why the spines were generated alternately? 

<CODE>

# if i % 5 == 0:
faceList.append(f.index)

</CODE>

This code means, 'Select All Faces'. In the console, I could see the selected numbers. And, in the next 'for index in faceList:', I think the  'gene_Spine' must generate for All faces. (Because 'if' line are commented out.)

<CODE>

for index in faceList:
# Make sure we are in object mode so we can select the current face in the list
bpy.ops.object.mode_set(mode='OBJECT')
# Select this face
print('selecting',index)
ob.data.faces[index].select = True
# NOW WE CAN DO WHATEVER WE WANT TO THIS FACE
# Go into edit mode for editing!
bpy.ops.object.mode_set(mode='EDIT')
# Spine test
gene_Spine()
# Now if we are done with this face... we deselect it, and anything else that might be selected (outside the 5 long loop)
bpy.ops.mesh.select_all(action='DESELECT')

</CODE>

And more, I didn't find a couse of this.


A part of this, 'gene_Spine' was applied for some generated faces. I'm stuck here. :-(