Wednesday, April 27, 2011

I got a good result!

Today, I tried again, I got a good result. (^_^)
I don't know why I couldn't get this result yesterday, maybe it's cause of my mistakes.

Thanks Dolf, We could advance the project to next level. (^_^)

<CODE>

# So lets go do some face selecting in Blender 3D using the python api.
# I will give a few very simple examples

# BE AWARE THAT FOR THESE EXAMPLES YOU MUST HAVE A MESH SELECTED AND IN OBJECT MODE!
# AND FOR TESTING USE ONE OF THE EXAMPLES AT A TIME.

# 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

# MAKE SURE WE ARE IN OBJECT MODE FOR SELECTING
bpy.ops.object.mode_set(mode='OBJECT')

# SELECTING EVERY OTHER FACE

# Lets loop through all the faces in the mesh
for i, f in enumerate(ob.data.faces):
# See if the current iteration/number i is odd or even
if i % 5 == 0:
# Set select to true
f.select = True
else:
f.select = False

# MAKE SURE WE ARE IN EDIT MODE FOR EXTRUDE AND TRANSLATE
bpy.ops.object.mode_set(mode='EDIT')

# Extrude the selection (do not move it)
bpy.ops.mesh.extrude(type='FACES')

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

# Transform selected faces. tValue = move
tValue = -1.0
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 the Extruded Faces. sValue = scale
sValue = 0.7
bpy.ops.transform.resize(value=(sValue,sValue,sValue), constraint_axis=(True, True, True), constraint_orientation='LOCAL', 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)

# Set to the Object mode
bpy.ops.object.editmode_toggle()

</CODE>

1. Add a Cube, and deselect the all faces in [Edit] mode. Then return to [Object] mode.


2. Run the script, I got this result.


Of course, if I deselect the all faces in script first, the process of deselecting all faces in (1.)are no needed.

2 comments:

  1. From dolf--

    Ok, so yes... this is a known issue in Blender currently. I orginally came across it when I tried to transform things in "normal space" in stead of "world space" through the API... which is basicly the same problem you are having. In my case the workaround is to multiply the transformations by a custom matrix. In your case... well... maybe you need to do whatever you are doing for every face separately. Martin Poirier is the coder to talk to about this... he told me he would look into it, but he is extremely busy, so... I think we are better off finding our own solutions for now.

    ReplyDelete
  2. Thanks Dolf, I'll find own way. :-)

    Next big theme is 'Variation for Selecting the faces' and 'How to describe Gene System', I think.

    ReplyDelete