BPY Module
BPY Module
ops:
This sub module contains blender operators. These are primarily functions for
manipulating objects, similarly to the way Blender artists manipulate objects in
the default interface.
The most important classes in this sub modules are
1) bpy.ops.object
2) bpy.ops.mesh
bpy.ops.object:
This class contains functions to manipulating multiple object at same
time.
bpy.ops.mesh:
The mesh class contains functions for manipulating vertices, edges, and
faces of objects one at a time, typically in Edit Mode.
bpy.context:
This module is used to access objects and areas of blender by
various status criteria. The primary function of this sub module is to
give Python developers a means of accessing the current data that a
user is working with.
bpy.context.object it refer to selected (active) 3D object in
interface
bpy.context.object.scale[0] = 1.45 it scales 3D object which is
selected(active) by user in interface.
The bpy.context sub module is great for fetching lists of objects based
on their state within Blender.
bpy.context.object outputs bpy.data.objects[Active object in
viewport]
bpy.context.space_data.viewport_shade = ‘WIREFRAME’ changes
view shades
bpy.data:
This sub module is used to access Blender’s internal data. Used
access the 3D object by its name.
1. bpy.data.objects it refers to structure containing all 3D object
names and their object properties.
list(bpy.data.object)
output:
[[bpy.data.objects['Camera'], bpy.data.objects['Cube'],
bpy.data.objects['Lamp'], bpy.data.objects['Sphere'],
bpy.data.objects['Torus']]
Ex2: bpy.data.object.keys()
['Camera', 'Cube', 'Lamp', 'Sphere', 'Torus']
Remove 3D object
# remove mesh Cube
if "objectName" in bpy.data.meshes:
mesh = bpy.data.meshes["objectName "]
print("removing mesh", mesh)
bpy.data.meshes.remove(mesh)
SELECTING Objects from SCRIPTS:
import bpy
def mySelector(objName, additive=False):
# By default, clear other selections
if not additive:
bpy.ops.object.select_all(action='DESELECT')
# Set the 'select' property of the datablock to True
bpy.data.objects[objName].select = True
import bpy
#Deselect all the objects in working space
bpy.ops.object.select_all(action='DESELECT')
#Specifying Certain object and selecting it
bpy.data.objects['Cube'].select = True
#activating specified object
bpy.context.scene.objects.active = bpy.data.objects['Cube']
#or
bpy.context.scene.objects.active = bpy.data.objects['Sphere']
Miscellaneous Functions:
Rename object:
bpy.context.object.name = SomeName It renames activated object.
Delete object:
To delete selected object
bpy.ops.object.delete(use_global=False)
Deselecting all the vertices, faces and edges after entering EDIT mode.
bpy.ops.object.mode_set(mode = ‘EDIT’)
bpy.ops.mesh.select_all(action=’DESELECT’)
# Select a face
bm.faces.ensure_lookup_table()
bm.faces[0].select = True
# Select a vertex
bm.verts.ensure_lookup_table()
bm.verts[5].select = True
# This is how we can access to different parts of object
import bpy
import bmesh
# Creating a CUBE
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(-3, 0, 0))
# Switching into Edit Mode
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
# Set to face mode for transformations
bpy.ops.mesh.select_mode(type = "FACE")
#assigning mesh object to variable
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.faces.ensure_lookup_table()
bm.faces[1].select = True
# Transformation
bpy.ops.transform.rotate(value = 0.3, axis = (0, 1, 0))
# Switching back to edit mode.
bpy.ops.object.mode_set(mode='OBJECT')
Extruding:
Bpy.ops.mesh.extrude_edges_indiv(
extrude_edges_move(
extrude_faces_indiv(
extrude_faces_move(
extrude_region(
extrude_region_move(
extrude_region_shrink_fatten(
extrude_repeat(
extrude_vertices_move(
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate =
{"value": (0.3, 0.3, 0.3), "constraint_axis": (True, True, True),
"constraint_orientation" :'NORMAL'})
CODE:
import bpy
import bmesh
# Will fail if scene is empty
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='SELECT')
# Clearing scene
bpy.ops.object.delete()
Miscellaneous
To display vertexes/faces/faceNormals
bpy.context.object.data.show_normal_vertex = True
bpy.context.object.data.show_normal_loop = True
bpy.context.object.data.show_normal_face = True
G Global Coordinates
L Local Coordinates
G = T*L
T Transformation Matrix
G is very useful for positioning objects relative to others, and L is very easy to
loop through to fetch indices.
2 3 1 0 1
No. of parametric direction,
plot dimensions,
no. of patches
no. of interfaces
Don’t know what it is.
No. of patches:
PATCH 1
Generally we use single patch
Degrees:
3 3
Degree of U parametric direction.
Degree of V parametric direction.
Control Point Count:
4 4
Count of control points along U parametric direction
Count of control points along V parametric direction
Knot Vector:
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
Knot vectors along Each parametric direction (U & V)