2

Currently I have this script.(IMAGE) but also attached the blend file for better understanding enter image description here

https://www.mediafire.com/file/fjg3ger7lbukmlk/Text_s.py/file

https://www.mediafire.com/file/lr8yc0cttw3nkgh/test.blend/file

So what My code basically is doing for a selected object is as follows:- finding if object has Mirror modifier or not. If exists, I delete the already existing [Mirror] Modifier and make a new modifier Mirror with but with some pre-defined changes of its options:- like in my code, changing UV offset X to 1.0 from 0.0

My code works fine If I select just a object and run the script, but as soon as I select two objects. It gets frozen and eventually crashes.

I know this code description is too complicated. Don't mind it. For now I basically want to develop a code as follows:

If I select two or more objects in scene and when I run the script. my modifier[eg mirror] moves to top of modifier stack for both the objects.

NamanDeep
  • 461
  • 3
  • 12

1 Answers1

5

As by this Q&A: reorder modifier/constraints stack order in python

Use bpy.ops.object.modifier_move_up or ..._down. To avoid the need to make each object active before moving modifiers, use context override {'object': ...}

import bpy
from bpy import context as C

def check(obj, name): index = obj.modifiers.find(name) if index == -1: raise KeyError(f"Couldn't find '{name}' modifier inside '{obj.name}' object") return index

def move_up(obj, name): check(obj, name) bpy.ops.object.modifier_move_up({'object': obj}, modifier=name)

def move_down(obj, name): check(obj, name) bpy.ops.object.modifier_move_down({'object': obj}, modifier=name)

def move_top(obj, name): for i in range(check(obj, name)): move_up(obj, name)

def move_bottom(obj, name): for i in range(check(obj, name), len(obj.modifiers)-1): move_down(obj, name)

for o in C.selected_objects: move_top(o, 'Mirror')

Markus von Broady
  • 36,563
  • 3
  • 30
  • 99