code cleanup: dont use 'bpy.context' when 'context' is available

This commit is contained in:
Campbell Barton
2013-01-23 07:52:31 +00:00
parent 6b51bb39c8
commit fd35d42bf4
6 changed files with 49 additions and 45 deletions

View File

@@ -114,14 +114,15 @@ def GlobalBB_HQ(obj):
return Vector((left, front, up)), Vector((right, back, down)) return Vector((left, front, up)), Vector((right, back, down))
def align_objects(align_x, def align_objects(context,
align_x,
align_y, align_y,
align_z, align_z,
align_mode, align_mode,
relative_to, relative_to,
bb_quality): bb_quality):
cursor = bpy.context.scene.cursor_location cursor = context.scene.cursor_location
Left_Front_Up_SEL = [0.0, 0.0, 0.0] Left_Front_Up_SEL = [0.0, 0.0, 0.0]
Right_Back_Down_SEL = [0.0, 0.0, 0.0] Right_Back_Down_SEL = [0.0, 0.0, 0.0]
@@ -130,7 +131,7 @@ def align_objects(align_x,
objs = [] objs = []
for obj in bpy.context.selected_objects: for obj in context.selected_objects:
matrix_world = obj.matrix_world.copy() matrix_world = obj.matrix_world.copy()
bb_world = [matrix_world * Vector(v[:]) for v in obj.bound_box] bb_world = [matrix_world * Vector(v[:]) for v in obj.bound_box]
objs.append((obj, bb_world)) objs.append((obj, bb_world))
@@ -150,7 +151,7 @@ def align_objects(align_x,
# Active Center # Active Center
if obj == bpy.context.active_object: if obj == context.active_object:
center_active_x = (Left_Front_Up[0] + Right_Back_Down[0]) / 2.0 center_active_x = (Left_Front_Up[0] + Right_Back_Down[0]) / 2.0
center_active_y = (Left_Front_Up[1] + Right_Back_Down[1]) / 2.0 center_active_y = (Left_Front_Up[1] + Right_Back_Down[1]) / 2.0
@@ -386,7 +387,8 @@ class AlignObjects(Operator):
def execute(self, context): def execute(self, context):
align_axis = self.align_axis align_axis = self.align_axis
ret = align_objects('X' in align_axis, ret = align_objects(context,
'X' in align_axis,
'Y' in align_axis, 'Y' in align_axis,
'Z' in align_axis, 'Z' in align_axis,
self.align_mode, self.align_mode,

View File

@@ -72,7 +72,7 @@ class QuickFur(Operator):
) )
def execute(self, context): def execute(self, context):
fake_context = bpy.context.copy() fake_context = context.copy()
mesh_objects = [obj for obj in context.selected_objects mesh_objects = [obj for obj in context.selected_objects
if obj.type == 'MESH'] if obj.type == 'MESH']
@@ -161,7 +161,7 @@ class QuickExplode(Operator):
) )
def execute(self, context): def execute(self, context):
fake_context = bpy.context.copy() fake_context = context.copy()
obj_act = context.active_object obj_act = context.active_object
if obj_act is None or obj_act.type != 'MESH': if obj_act is None or obj_act.type != 'MESH':
@@ -311,7 +311,7 @@ class QuickSmoke(Operator):
) )
def execute(self, context): def execute(self, context):
fake_context = bpy.context.copy() fake_context = context.copy()
mesh_objects = [obj for obj in context.selected_objects mesh_objects = [obj for obj in context.selected_objects
if obj.type == 'MESH'] if obj.type == 'MESH']
min_co = Vector((100000.0, 100000.0, 100000.0)) min_co = Vector((100000.0, 100000.0, 100000.0))
@@ -432,7 +432,7 @@ class QuickFluid(Operator):
) )
def execute(self, context): def execute(self, context):
fake_context = bpy.context.copy() fake_context = context.copy()
mesh_objects = [obj for obj in context.selected_objects mesh_objects = [obj for obj in context.selected_objects
if (obj.type == 'MESH' and not 0.0 in obj.dimensions)] if (obj.type == 'MESH' and not 0.0 in obj.dimensions)]
min_co = Vector((100000, 100000, 100000)) min_co = Vector((100000, 100000, 100000))

View File

@@ -23,7 +23,8 @@ from bpy.types import Operator
from mathutils import Vector from mathutils import Vector
def randomize_selected(seed, delta, loc, rot, scale, scale_even, scale_min): def randomize_selected(context, seed, delta,
loc, rot, scale, scale_even, scale_min):
import random import random
from random import uniform from random import uniform
@@ -33,7 +34,7 @@ def randomize_selected(seed, delta, loc, rot, scale, scale_even, scale_min):
def rand_vec(vec_range): def rand_vec(vec_range):
return Vector(uniform(-val, val) for val in vec_range) return Vector(uniform(-val, val) for val in vec_range)
for obj in bpy.context.selected_objects: for obj in context.selected_objects:
if loc: if loc:
if delta: if delta:
@@ -180,6 +181,7 @@ class RandomizeLocRotSize(Operator):
#scale_min = self.scale_min #scale_min = self.scale_min
scale_min = 0 scale_min = 0
randomize_selected(seed, delta, loc, rot, scale, scale_even, scale_min) randomize_selected(context, seed, delta,
loc, rot, scale, scale_even, scale_min)
return {'FINISHED'} return {'FINISHED'}

View File

@@ -32,7 +32,7 @@ class CopyRigidbodySettings(Operator):
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
obj = bpy.context.object obj = context.object
return (obj and obj.rigid_body) return (obj and obj.rigid_body)
def execute(self, context): def execute(self, context):
@@ -101,22 +101,22 @@ class BakeToKeyframes(Operator):
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
obj = bpy.context.object obj = context.object
return (obj and obj.rigid_body) return (obj and obj.rigid_body)
def execute(self, context): def execute(self, context):
bake = [] bake = []
objs = [] objs = []
scene = bpy.context.scene scene = context.scene
frame_orig = scene.frame_current frame_orig = scene.frame_current
frames = list(range(self.frame_start, self.frame_end + 1, self.step)) frames = list(range(self.frame_start, self.frame_end + 1, self.step))
# filter objects selection # filter objects selection
for ob in bpy.context.selected_objects: for obj in context.selected_objects:
if not ob.rigid_body or ob.rigid_body.type != 'ACTIVE': if not obj.rigid_body or obj.rigid_body.type != 'ACTIVE':
ob.select = False obj.select = False
objs = bpy.context.selected_objects objs = context.selected_objects
if objs: if objs:
# store transformation data # store transformation data
@@ -124,31 +124,31 @@ class BakeToKeyframes(Operator):
scene.frame_set(f) scene.frame_set(f)
if f in frames: if f in frames:
mat = {} mat = {}
for i, ob in enumerate(objs): for i, obj in enumerate(objs):
mat[i] = ob.matrix_world.copy() mat[i] = obj.matrix_world.copy()
bake.append(mat) bake.append(mat)
# apply transformations as keyframes # apply transformations as keyframes
for i, f in enumerate(frames): for i, f in enumerate(frames):
scene.frame_set(f) scene.frame_set(f)
ob_prev = objs[0] obj_prev = objs[0]
for j, ob in enumerate(objs): for j, obj in enumerate(objs):
mat = bake[i][j] mat = bake[i][j]
ob.location = mat.to_translation() obj.location = mat.to_translation()
rot_mode = ob.rotation_mode rot_mode = obj.rotation_mode
if rot_mode == 'QUATERNION': if rot_mode == 'QUATERNION':
ob.rotation_quaternion = mat.to_quaternion() obj.rotation_quaternion = mat.to_quaternion()
elif rot_mode == 'AXIS_ANGLE': elif rot_mode == 'AXIS_ANGLE':
# this is a little roundabout but there's no better way right now # this is a little roundabout but there's no better way right now
aa = mat.to_quaternion().to_axis_angle() aa = mat.to_quaternion().to_axis_angle()
ob.rotation_axis_angle = (aa[1], ) + aa[0][:] obj.rotation_axis_angle = (aa[1], ) + aa[0][:]
else: # euler else: # euler
# make sure euler rotation is compatible to previous frame # make sure euler rotation is compatible to previous frame
ob.rotation_euler = mat.to_euler(rot_mode, ob_prev.rotation_euler) obj.rotation_euler = mat.to_euler(rot_mode, obj_prev.rotation_euler)
ob_prev = ob obj_prev = obj
bpy.ops.anim.keyframe_insert(type='BUILTIN_KSI_LocRot', confirm_success=False) bpy.ops.anim.keyframe_insert(type='BUILTIN_KSI_LocRot', confirm_success=False)
@@ -156,8 +156,8 @@ class BakeToKeyframes(Operator):
bpy.ops.rigidbody.objects_remove() bpy.ops.rigidbody.objects_remove()
# clean up keyframes # clean up keyframes
for ob in objs: for obj in objs:
action = ob.animation_data.action action = obj.animation_data.action
for fcu in action.fcurves: for fcu in action.fcurves:
keyframe_points = fcu.keyframe_points keyframe_points = fcu.keyframe_points
i = 1 i = 1
@@ -219,30 +219,30 @@ class ConnectRigidBodies(Operator):
@classmethod @classmethod
def poll(cls, context): def poll(cls, context):
obj = bpy.context.object obj = context.object
objs = bpy.context.selected_objects objs = context.selected_objects
return (obj and obj.rigid_body and (len(objs) > 1)) return (obj and obj.rigid_body and (len(objs) > 1))
def execute(self, context): def execute(self, context):
objs = bpy.context.selected_objects objs = context.selected_objects
ob_act = bpy.context.active_object obj_act = context.active_object
for ob in objs: for obj in objs:
if ob == ob_act: if obj == obj_act:
continue continue
if self.pivot_type == 'ACTIVE': if self.pivot_type == 'ACTIVE':
loc = ob_act.location loc = obj_act.location
elif self.pivot_type == 'SELECTED': elif self.pivot_type == 'SELECTED':
loc = ob.location loc = obj.location
else: else:
loc = (ob_act.location + ob.location) / 2 loc = (obj_act.location + obj.location) / 2.0
bpy.ops.object.add(type='EMPTY', view_align=False, enter_editmode=False, location=loc) bpy.ops.object.add(type='EMPTY', view_align=False, enter_editmode=False, location=loc)
bpy.ops.rigidbody.constraint_group_add() bpy.ops.rigidbody.constraint_group_add()
con = bpy.context.active_object.rigid_body_constraint con = context.active_object.rigid_body_constraint
con.type = self.con_type con.type = self.con_type
con.object1 = ob_act con.object1 = obj_act
con.object2 = ob con.object2 = obj
return {'FINISHED'} return {'FINISHED'}

View File

@@ -516,7 +516,7 @@ def lightmap_uvpack(meshes,
def unwrap(operator, context, **kwargs): def unwrap(operator, context, **kwargs):
is_editmode = (bpy.context.object.mode == 'EDIT') is_editmode = (context.object.mode == 'EDIT')
if is_editmode: if is_editmode:
bpy.ops.object.mode_set(mode='OBJECT', toggle=False) bpy.ops.object.mode_set(mode='OBJECT', toggle=False)

View File

@@ -1730,7 +1730,7 @@ class WM_OT_addon_install(Operator):
# don't use bpy.utils.script_paths("addons") because we may not be able to write to it. # don't use bpy.utils.script_paths("addons") because we may not be able to write to it.
path_addons = bpy.utils.user_resource('SCRIPTS', "addons", create=True) path_addons = bpy.utils.user_resource('SCRIPTS', "addons", create=True)
else: else:
path_addons = bpy.context.user_preferences.filepaths.script_directory path_addons = context.user_preferences.filepaths.script_directory
if path_addons: if path_addons:
path_addons = os.path.join(path_addons, "addons") path_addons = os.path.join(path_addons, "addons")