I am trying to overwrite the bpy.ops.object.move_to_layer
operator in Blender so that I can move other objects when the selected objects are moved.
When I try to move the selected objects by pressing the 'm' key and clicking on the new layer in the invoked props popup (from the 'layers' BoolVectorProperty), though, the object still remains in the old layer as well as being added to the new layer because both layers are active, as if I was holding the shift
key in the original operator.
The following is my code to overwrite and replicate the bpy.ops.object.move_to_layer
operator (I omitted extra code run when the operator is executed for simplicity):
# Blender imports
import bpy
from bpy.types import Operator
from bpy.props import *
class move_to_layer_override(Operator):
"""Move to Layer"""
bl_idname = "object.move_to_layer"
bl_label = "Move to Layer"
bl_options = {'REGISTER', 'INTERNAL', 'UNDO'}
################################################
# Blender Operator methods
@classmethod
def poll(self, context):
return True
def execute(self, context):
self.runMove(context)
return {'FINISHED'}
def invoke(self, context, event):
# sets self.layers to active layers of selected objects
idxs = [i for i in range(20)]
for obj in context.selected_objects:
for i in idxs:
layer = obj.layers[i]
if layer:
self.layers[i] = True
idxs.remove(i)
# Run confirmation popup for delete action
return context.window_manager.invoke_props_popup(self, event)
###################################################
# class variables
# This does not behave like scn.layers.
# only one layer should be selected at a time unless shift is held
layers = BoolVectorProperty(
name="Layers",
subtype="LAYER",
description="Object Layers",
size=20,
)
################################################
# class methods
def runMove(self, context):
scn = context.scene
for obj in bpy.context.selected_objects:
obj.layers = self.layers
# insert extra code to run when objects are moved here
def register():
bpy.utils.register_module(__name__)
def unregister():
addon_updater_ops.unregister()
if __name__ == "__main__":
register()
It should behave exactly like the original operator – when a new layer is selected in the invoked props popup, the old layer(s) should be deselected unless the shift
key is held. Could someone help me figure out how to do this?
shift
key event can change the behavior of the BoolVectorProperty in the invoked popup, not whether it can change the behavior of the operator itself. $\endgroup$ – Christopher Gearhart Jun 27 '18 at 16:44