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. :-(

No comments:

Post a Comment