0% found this document useful (0 votes)
106 views10 pages

BPY Module

This document summarizes key aspects of the bpy module in Python for manipulating and interacting with 3D objects and data in Blender. It describes: 1) The bpy.ops submodule contains operators for manipulating objects similarly to the Blender interface, with important classes for object and mesh manipulation. 2) The bpy.context submodule accesses current Blender data like the active object. 3) The bpy.data submodule accesses Blender's internal data like objects by name. 4) The bmesh module is used to edit meshes and select/transform vertices, edges and faces in edit mode. Basic transformations, extruding, and accessing mesh parts are demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views10 pages

BPY Module

This document summarizes key aspects of the bpy module in Python for manipulating and interacting with 3D objects and data in Blender. It describes: 1) The bpy.ops submodule contains operators for manipulating objects similarly to the Blender interface, with important classes for object and mesh manipulation. 2) The bpy.context submodule accesses current Blender data like the active object. 3) The bpy.data submodule accesses Blender's internal data like objects by name. 4) The bmesh module is used to edit meshes and select/transform vertices, edges and faces in edit mode. Basic transformations, extruding, and accessing mesh parts are demonstrated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

bpy.

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.

Ex: bpy.ops.object.select_all() it selects all the objects in 3d


viewport.
Bpy.ops.object.select_all(action=’SELECT’) Selects all objects
Bpy.ops.object.select_all(action=’DESELECT’) deselects objects

Adding Modifier to selected object:


bpy.ops.object.modifier_add(type='CURVE')

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.

Ex: - If we are having cube, sphere, torus models in 3d view to access


every 3D object in blender’s 3D viewport.

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

Ex1: bpy.data.objects.items () outputs


[('Camera', bpy.data.objects ['Camera']), ('Cube', bpy.data.objects
['Cube']), ('Lamp', bpy.data.objects['Lamp']), ('Sphere',
bpy.data.objects['Sphere']), ('Torus', bpy.data.objects['Torus'])]

Ex2: bpy.data.object.keys()
['Camera', 'Cube', 'Lamp', 'Sphere', 'Torus']

bpy.data.objects["SurfSphere"].scale[0] = 1.45 it refer to


mentioned 3D object and scales it.
Print all the objects name
import bpy

# print all 3D objects


for obj in bpy.data.objects:
print(obj.name)

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:

Here we defined some functions to select only specified object

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

mySelector(‘Cube’) selects specified object


mySelector(‘Sphere’, additive=True) It won’t deselect preselected
objects in 3D viewport but selects specified object.

Once object selected we can perform any operation on it without using


mentioning it farther process like translation.
bpy.ops.transform.translate(value=(1, 0, 0))
Activate object;
Activation, like selection, is an object state in Blender. Unlike selection, only
one object can be active at any given time. This state is generally used for
vertex, edge, and face manipulation of single objects. This state also has a close
relationship with Edit Mode,

bpy.context.scene.objects.active = bpy.data.objects['Cube'] It actives assigned


object
bpy.context.object  It refers to active object in 3d view port

SELECTION ACTIVATION & SPECIFING:

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)

Delete All the objects


Function to delete all the objects
def delete_all():
if(len(bpy.data.objects) != 0):
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()

The bmesh Module

To edit any 3D object in blender we need to select that object and


then switch to edit mode.
Switching to EDIT mode:
1) bpy.ops.object.editmode_toggle()
2) bpy.ops.object.mode_set(mode="EDIT")
a. mode = ‘EDIT’
b. ‘SCUPLT’
c. ‘TEXTURE_PAINT’
d. ‘VERTEX_PAINT’
e. WEIGHT_PAINT’

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

Different types of submodes in edit mode are


(face = ‘FACE’, vertec = ‘VERT’, edge = ‘EDGE’)
bpy.ops.mesh.select_mode(type = ‘type’)

Selecting Different parts of object:


import bpy
import bmesh
£ Must start in object mode
bpy.ops.object.mode_set(mode='OBJECT')
£ Select all the object to clear working space
bpy.ops.object.select_all(action='SELECT')
£ Deleting working space
bpy.ops.object.delete()
£ Creating 3D model
bpy.ops.mesh.primitive_cube_add(radius=1, location=(0, 0, 0))

£ entering into EditMode


bpy.ops.object.mode_set(mode=’EDIT’)

£ Set to ‘FACE Mode’ for easier condition


bpy.ops.mesh.select_mode(type = "FACE")

£ registering bmesh object


bm = bmesh.from_edit_mesh(bpy.context.object.data)

# Deselect all vertices, edges and faces


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

Basic Transformations in BMESH

# Create a cube and rotate a face around the y-axis

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

# create a cube and pull an edge along the y-axis


bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(0, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.edges.ensure_lookup_table()
bm.edges[4].select = True
bpy.ops.transform.translate(value = (0, 0.5, 0))
bpy.ops.object.mode_set(mode='OBJECT')

# Create a cube and pull a vertex 1 unit


# along the y and z axes
# Create a cube and pull an edge along the y-axis
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(3, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.verts.ensure_lookup_table()
bm.verts[3].select = True
bpy.ops.transform.translate(value = (0, 1, 1))
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()

# Create a cube and extrude the top face away from it


bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(-3, 0, 0))
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")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
# Below line allow us to access the index the specified object.
bm.faces.ensure_lookup_table()
bm.faces[5].select = True
# Extruding
bpy.ops.mesh.extrude_region_move(TRANSFORM_OT_translate =
{"value": (0.3, 0.3, 0.3),
"constraint_axis": (True, True, True),
"constraint_orientation" :'NORMAL'})
bpy.ops.object.mode_set(mode='OBJECT')

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

 False hides the above things.

GLOBAL COORDINATES and LOCAL COORDINATES

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.

Object Vertices data vector:


1. bpy.data.objects['Cube'].vertices[0] returns
bpy.data.meshes['Cube.001'].vertices[0]
2. py.data.objects['Cube'].data.vertices[0].co returns vector of the vertices
coordinates.
3. bpy.data.objects['Cube'].data.vertices[0].co.angle([any vector])
returns angle between vertices coordinate and any chosen vector

BLENDER WORKING WITH NURBS


Orange colour represents U parametric Direction.
Magenta represents V parametric direction.

Geometry data extracted from Blender .txt file

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)

Control points and Weights:


-1.5 -1.5 -1.5 -1.5 -0.5 -0.5 -0.5 -0.5 0.5 0.5 0.5 0.5 1.5 1.5 1.5 1.5
-1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5 -1.5 -0.5 0.5 1.5
0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0
1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0

You might also like