2
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ Hello and welcome. While files, images, and external videos or links may be helpful additions they should not be the only way to obtain information about your issue. Don't make understanding your question rely on downloading a file, watching a video or visiting an external site. Use the builtin tools to upload images or gifs, along with thoroughly explaining the problem in written form so it can be indexed and searched for thus helping future visitors with similar issues. $\endgroup$ Commented Jan 9, 2022 at 22:43
  • $\begingroup$ @DuarteFarrajotaRamos Actually I did uploaded a image of code here, but it somehow didnt uploaded to post. $\endgroup$
    – NamanDeep
    Commented Jan 10, 2022 at 4:56
  • $\begingroup$ Also don't post images of code. If the code is relevant to the question post it as plain text using proper code tags, so people don't have to type it by hand. $\endgroup$ Commented Jan 10, 2022 at 10:31
  • $\begingroup$ WiP... A comprehensive list of operator overrides $\endgroup$ Commented Feb 19, 2022 at 14:55

1 Answer 1

5
$\begingroup$

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')

$\endgroup$
2

Not the answer you're looking for? Browse other questions tagged .