Friday, April 22, 2011

Select, Extrude, and Move?

I'm trying this code. But after extrude part, nothing to happen.
What's wrong?

<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




# SELECTING FACES POINTING UP


# Import the math and mathtutils so we can work with vectors and such
import math, mathutils


# Make a vector (direction) pointing up
up = mathutils.Vector((0.0,0.0,1.0))


# Lets loop through all the faces in the mesh
for f in ob.data.faces:


# If the angle between up and the face's normal (direction) is smaller than 45...
# The face must be pointing up
# To compare the angle we need 45 degrees in radians, not in degrees!
# Math with angles is usually all in radians
   if f.normal.angle(up) < math.radians(45):
     # Set select to true
     f.select = True
   else:
     f.select = False


# Extrude the selection (do not move it)
  def extrude(self):
     bpy.ops.mesh.extrude(type='REGION')
  # Move the selection (always relative to the normal)
  # val = (0, 0, 1.0)
     def translate(self, val):
         bpy.ops.transform.translate(value=val, constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional=bpy.context.tool_settings.proportional_edit, snap=bpy.context.tool_settings.use_snap, release_confirm=False)

</code>

Oh, about def, I got it. That's for function. How is next?


<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


# SELECTING FACES POINTING UP

# Import the math and mathtutils so we can work with vectors and such
import math, mathutils

# Make a vector (direction) pointing up
up = mathutils.Vector((0.0,0.0,1.0))

# Lets loop through all the faces in the mesh
for f in ob.data.faces:

# If the angle between up and the face's normal (direction) is smaller than 45...
# The face must be pointing up
# To compare the angle we need 45 degrees in radians, not in degrees!
# Math with angles is usually all in radians
   if f.normal.angle(up) < math.radians(45):
     # Set select to true
     f.select = True
   else:
     f.select = False

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

# Move the selection (always relative to the normal)
bpy.ops.transform.translate(value=(0, 0, 1.0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional=bpy.context.tool_settings.proportional_edit, snap=bpy.context.tool_settings.use_snap, release_confirm=False)

</code>


Hmmm, I got an error for 'extrude' part.  :-(
And try next,


<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


# SELECTING FACES POINTING UP

# Import the math and mathtutils so we can work with vectors and such
import math, mathutils

# Make a vector (direction) pointing up
up = mathutils.Vector((0.0,0.0,1.0))

# Lets loop through all the faces in the mesh
for f in ob.data.faces:

# If the angle between up and the face's normal (direction) is smaller than 45...
# The face must be pointing up
# To compare the angle we need 45 degrees in radians, not in degrees!
# Math with angles is usually all in radians
   if f.normal.angle(up) < math.radians(45):
     # Set select to true
     f.select = True
   else:
     f.select = False

# Add This line ---
bpy.ops.object.editmode_toggle()     

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

# Move the selection (always relative to the normal)
bpy.ops.transform.translate(value=(0, 0, 1.0), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional=bpy.context.tool_settings.proportional_edit, snap=bpy.context.tool_settings.use_snap, release_confirm=False)

</code>

I got extruded shape!


No comments:

Post a Comment