forked from bartvdbraak/blender
code cleanup: dont use 'bpy.context' when 'context' is available
This commit is contained in:
parent
6b51bb39c8
commit
fd35d42bf4
@ -114,14 +114,15 @@ def GlobalBB_HQ(obj):
|
||||
return Vector((left, front, up)), Vector((right, back, down))
|
||||
|
||||
|
||||
def align_objects(align_x,
|
||||
def align_objects(context,
|
||||
align_x,
|
||||
align_y,
|
||||
align_z,
|
||||
align_mode,
|
||||
relative_to,
|
||||
bb_quality):
|
||||
|
||||
cursor = bpy.context.scene.cursor_location
|
||||
cursor = context.scene.cursor_location
|
||||
|
||||
Left_Front_Up_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 = []
|
||||
|
||||
for obj in bpy.context.selected_objects:
|
||||
for obj in context.selected_objects:
|
||||
matrix_world = obj.matrix_world.copy()
|
||||
bb_world = [matrix_world * Vector(v[:]) for v in obj.bound_box]
|
||||
objs.append((obj, bb_world))
|
||||
@ -150,7 +151,7 @@ def align_objects(align_x,
|
||||
|
||||
# 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_y = (Left_Front_Up[1] + Right_Back_Down[1]) / 2.0
|
||||
@ -386,7 +387,8 @@ class AlignObjects(Operator):
|
||||
|
||||
def execute(self, context):
|
||||
align_axis = self.align_axis
|
||||
ret = align_objects('X' in align_axis,
|
||||
ret = align_objects(context,
|
||||
'X' in align_axis,
|
||||
'Y' in align_axis,
|
||||
'Z' in align_axis,
|
||||
self.align_mode,
|
||||
|
@ -72,7 +72,7 @@ class QuickFur(Operator):
|
||||
)
|
||||
|
||||
def execute(self, context):
|
||||
fake_context = bpy.context.copy()
|
||||
fake_context = context.copy()
|
||||
mesh_objects = [obj for obj in context.selected_objects
|
||||
if obj.type == 'MESH']
|
||||
|
||||
@ -161,7 +161,7 @@ class QuickExplode(Operator):
|
||||
)
|
||||
|
||||
def execute(self, context):
|
||||
fake_context = bpy.context.copy()
|
||||
fake_context = context.copy()
|
||||
obj_act = context.active_object
|
||||
|
||||
if obj_act is None or obj_act.type != 'MESH':
|
||||
@ -311,7 +311,7 @@ class QuickSmoke(Operator):
|
||||
)
|
||||
|
||||
def execute(self, context):
|
||||
fake_context = bpy.context.copy()
|
||||
fake_context = context.copy()
|
||||
mesh_objects = [obj for obj in context.selected_objects
|
||||
if obj.type == 'MESH']
|
||||
min_co = Vector((100000.0, 100000.0, 100000.0))
|
||||
@ -432,7 +432,7 @@ class QuickFluid(Operator):
|
||||
)
|
||||
|
||||
def execute(self, context):
|
||||
fake_context = bpy.context.copy()
|
||||
fake_context = context.copy()
|
||||
mesh_objects = [obj for obj in context.selected_objects
|
||||
if (obj.type == 'MESH' and not 0.0 in obj.dimensions)]
|
||||
min_co = Vector((100000, 100000, 100000))
|
||||
|
@ -23,7 +23,8 @@ from bpy.types import Operator
|
||||
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
|
||||
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):
|
||||
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 delta:
|
||||
@ -180,6 +181,7 @@ class RandomizeLocRotSize(Operator):
|
||||
#scale_min = self.scale_min
|
||||
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'}
|
||||
|
@ -32,7 +32,7 @@ class CopyRigidbodySettings(Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
obj = bpy.context.object
|
||||
obj = context.object
|
||||
return (obj and obj.rigid_body)
|
||||
|
||||
def execute(self, context):
|
||||
@ -101,22 +101,22 @@ class BakeToKeyframes(Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
obj = bpy.context.object
|
||||
obj = context.object
|
||||
return (obj and obj.rigid_body)
|
||||
|
||||
def execute(self, context):
|
||||
bake = []
|
||||
objs = []
|
||||
scene = bpy.context.scene
|
||||
scene = context.scene
|
||||
frame_orig = scene.frame_current
|
||||
frames = list(range(self.frame_start, self.frame_end + 1, self.step))
|
||||
|
||||
# filter objects selection
|
||||
for ob in bpy.context.selected_objects:
|
||||
if not ob.rigid_body or ob.rigid_body.type != 'ACTIVE':
|
||||
ob.select = False
|
||||
for obj in context.selected_objects:
|
||||
if not obj.rigid_body or obj.rigid_body.type != 'ACTIVE':
|
||||
obj.select = False
|
||||
|
||||
objs = bpy.context.selected_objects
|
||||
objs = context.selected_objects
|
||||
|
||||
if objs:
|
||||
# store transformation data
|
||||
@ -124,31 +124,31 @@ class BakeToKeyframes(Operator):
|
||||
scene.frame_set(f)
|
||||
if f in frames:
|
||||
mat = {}
|
||||
for i, ob in enumerate(objs):
|
||||
mat[i] = ob.matrix_world.copy()
|
||||
for i, obj in enumerate(objs):
|
||||
mat[i] = obj.matrix_world.copy()
|
||||
bake.append(mat)
|
||||
|
||||
# apply transformations as keyframes
|
||||
for i, f in enumerate(frames):
|
||||
scene.frame_set(f)
|
||||
ob_prev = objs[0]
|
||||
for j, ob in enumerate(objs):
|
||||
obj_prev = objs[0]
|
||||
for j, obj in enumerate(objs):
|
||||
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':
|
||||
ob.rotation_quaternion = mat.to_quaternion()
|
||||
obj.rotation_quaternion = mat.to_quaternion()
|
||||
elif rot_mode == 'AXIS_ANGLE':
|
||||
# this is a little roundabout but there's no better way right now
|
||||
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
|
||||
# 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)
|
||||
|
||||
@ -156,8 +156,8 @@ class BakeToKeyframes(Operator):
|
||||
bpy.ops.rigidbody.objects_remove()
|
||||
|
||||
# clean up keyframes
|
||||
for ob in objs:
|
||||
action = ob.animation_data.action
|
||||
for obj in objs:
|
||||
action = obj.animation_data.action
|
||||
for fcu in action.fcurves:
|
||||
keyframe_points = fcu.keyframe_points
|
||||
i = 1
|
||||
@ -219,30 +219,30 @@ class ConnectRigidBodies(Operator):
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
obj = bpy.context.object
|
||||
objs = bpy.context.selected_objects
|
||||
obj = context.object
|
||||
objs = context.selected_objects
|
||||
return (obj and obj.rigid_body and (len(objs) > 1))
|
||||
|
||||
def execute(self, context):
|
||||
|
||||
objs = bpy.context.selected_objects
|
||||
ob_act = bpy.context.active_object
|
||||
objs = context.selected_objects
|
||||
obj_act = context.active_object
|
||||
|
||||
for ob in objs:
|
||||
if ob == ob_act:
|
||||
for obj in objs:
|
||||
if obj == obj_act:
|
||||
continue
|
||||
if self.pivot_type == 'ACTIVE':
|
||||
loc = ob_act.location
|
||||
loc = obj_act.location
|
||||
elif self.pivot_type == 'SELECTED':
|
||||
loc = ob.location
|
||||
loc = obj.location
|
||||
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.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.object1 = ob_act
|
||||
con.object2 = ob
|
||||
con.object1 = obj_act
|
||||
con.object2 = obj
|
||||
|
||||
return {'FINISHED'}
|
||||
|
||||
|
@ -516,7 +516,7 @@ def lightmap_uvpack(meshes,
|
||||
|
||||
def unwrap(operator, context, **kwargs):
|
||||
|
||||
is_editmode = (bpy.context.object.mode == 'EDIT')
|
||||
is_editmode = (context.object.mode == 'EDIT')
|
||||
if is_editmode:
|
||||
bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
|
||||
|
||||
|
@ -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.
|
||||
path_addons = bpy.utils.user_resource('SCRIPTS', "addons", create=True)
|
||||
else:
|
||||
path_addons = bpy.context.user_preferences.filepaths.script_directory
|
||||
path_addons = context.user_preferences.filepaths.script_directory
|
||||
if path_addons:
|
||||
path_addons = os.path.join(path_addons, "addons")
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user