-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
## context managers for Bmesh editing | ||
## Author: Salai Vedha Viradhan : from https://salaivv.com/2023/07/07/context-mangers-blender | ||
|
||
import bpy | ||
import bmesh | ||
from contextlib import contextmanager | ||
|
||
# Bmesh operations on a mesh object | ||
@contextmanager | ||
def BMeshObject(mesh_object): | ||
# Setup process | ||
mesh = bmesh.new() | ||
mesh.from_mesh(mesh_object.data) | ||
|
||
try: | ||
# Yielding mesh to the outer context to execute | ||
# the statements inside the 'with' block | ||
yield mesh | ||
finally: | ||
# Teardown process | ||
mesh.to_mesh(mesh_object.data) | ||
mesh.free() | ||
|
||
## Usage: | ||
# with BMeshObject(mesh_object) as mesh: | ||
# bmesh.ops.remove_doubles(mesh, verts=mesh.verts, dist=0.0001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## context managers for edit mode | ||
## Author: Salai Vedha Viradhan : from https://salaivv.com/2023/07/07/context-mangers-blender | ||
|
||
import bpy | ||
import contextlib | ||
|
||
# Edit mode operations on passed object | ||
@contextlib.contextmanager | ||
def EditMode(active_object): | ||
# Setup process | ||
bpy.ops.object.mode_set(mode='OBJECT') | ||
bpy.ops.object.select_all(action='DESELECT') | ||
bpy.context.view_layer.objects.active = active_object | ||
bpy.ops.object.mode_set(mode='EDIT') | ||
|
||
try: | ||
# Yielding execution to the outer context to execute | ||
# the statements inside the 'with' block | ||
yield | ||
finally: | ||
# Teardown process | ||
bpy.ops.object.mode_set(mode='OBJECT') | ||
|
||
## usage: | ||
# with EditMode(mesh_object): | ||
# bpy.ops.mesh.select_all(action='SELECT') | ||
# bpy.ops.mesh.remove_doubles() | ||
# bpy.ops.mesh.dissolve_limited() | ||
# bpy.ops.mesh.tris_convert_to_quads() |
File renamed without changes.