0
$\begingroup$

In this code for 2.8, that works, I am creating a mirror modifier assigned to a cube.

import bpy

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='CUBE', view_align=False, location=(bpy.context.scene.cursor_location))
OA = bpy.context.selected_objects[0]
bpy.context.object.name = "Mirror Axes"
bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
OB.modifiers["Mirror"].mirror_object = OA 

The problem is that I cannot use this script multiple times correctly because of this part:

OB.modifiers["Mirror"].mirror_object = OA 

The object will always have a modifier named "Mirror" and if I use this script two times the modifier named "Mirror" will always be the first one and I want it to be the same modifier.

So it needs to be more something like this:

OB = bpy.context.selected_objects[0]
bpy.ops.object.modifier_add(type='MIRROR')
         ***RENAME MIRROR MODIFIER TO A CUSTOM NAME THAT CHANGE EACH TIME***
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.empty_add(type='CUBE', view_align=False, location=(bpy.context.scene.cursor_location))
OA = bpy.context.selected_objects[0]
bpy.context.object.name = "Mirror Axes"
bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
OB.modifiers["***CUSTOM UNIQUE NAME THAT I JUST CREATED EARLIER***"].mirror_object = OA

---- ---- EDIT AFTER RESPONSE OF MARTINZ --- ---

or like this with this new "if" method that i dont know yet

import bpy 

A = bpy.context.selected_objects[0]
if "QuickMirror1" not in A.modifiers:
    bpy.ops.object.modifier_add(type='MIRROR')
    A.modifiers[len(A.modifiers)-1].name = "QuickMirror1"
    bpy.ops.object.empty_add(type='CUBE')
    bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
    E = bpy.context.selected_objects[0]
    E.name = "Mirror Axes1"
    A.modifiers["QuickMirror1"].mirror_object = E
if "QuickMirror1" in A.modifiers:
    if "QuickMirror2" not in A.modifiers:
        bpy.ops.object.modifier_add(type='MIRROR')
        A.modifiers[len(A.modifiers)-1].name = "QuickMirror2"
        bpy.ops.object.empty_add(type='CUBE')
        bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
        E = bpy.context.selected_objects[0]
        E.name = "Mirror Axes2"
        A.modifiers["QuickMirror2"].mirror_object = E
if "QuickMirror2" in A.modifiers:
    if "QuickMirror3" not in A.modifiers:
        bpy.ops.object.modifier_add(type='MIRROR')
        A.modifiers[len(A.modifiers)-1].name = "QuickMirror3"
        bpy.ops.object.empty_add(type='CUBE')
        bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
        E = bpy.context.selected_objects[0]
        E.name = "Mirror Axes3"
        A.modifiers["QuickMirror3"].mirror_object = E
if "QuickMirror3" in A.modifiers:
    if "QuickMirror4" not in A.modifiers:
        bpy.ops.object.modifier_add(type='MIRROR')
        A.modifiers[len(A.modifiers)-1].name = "QuickMirror4"
        bpy.ops.object.empty_add(type='CUBE')
        bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
        E = bpy.context.selected_objects[0]
        E.name = "Mirror Axes4"
        A.modifiers["QuickMirror4"].mirror_object = E

i got an error message

 Converting py args to operator properties:  enum "MIRROR" not found in ()

i dont understand him but my code work fine four time in a row... it seams so absurd to me that i need to do this!

there's really no way inside of python to generate a name that change each time and choose the same name inside of the code ????

thanks

$\endgroup$
1
  • 1
    $\begingroup$ I think the issue here is not with what is possible in Python, but with our communication. Please try to keep it understandable and clear. Not making spelling and grammar mistakes would also improve it a lot. What is it that you want the script you are writing to do exactly? Why do you need this functionality - what's the context of your situation? $\endgroup$ Commented Dec 14, 2018 at 7:42

2 Answers 2

2
$\begingroup$

Test Script

  1. In test scripts I always use a context variable, will make cutting and pasting into Panel and Operator code easier.

  2. Can add a modifier with the API (See docs Modifiers.new()). Returns a reference to the newly added mod. No need to use op and go hunting for it.

  3. Use other modifiers to give a unique name eg "Mirror1", "Mirror2" on each run.

  4. Use context.object .. the first member of context.selected_objects is not necessarily the active object.

 

import bpy

context = bpy.context # 
scene = context.scene
ob = context.object

nmods = sum(1 for m in ob.modifiers if m.type == 'MIRROR')
i = 0
pref = "QuickMirror"

# give it a name based on count of mods.
name = "%s%d" % (pref, i)
while ob.modifiers.get(name):    
    i += 1
    name = "%s%d" % (pref, i)

m = ob.modifiers.new(name, type='MIRROR')

bpy.ops.object.mode_set(mode='OBJECT') # required?
bpy.ops.object.empty_add(type='CUBE', view_align=False, location=(scene.cursor_location))
axis_ob = context.object
axis_ob.name = "Mirror Axes"
bpy.ops.transform.resize(value=(0.01, 1, 1), constraint_axis=(True, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='ENABLED', proportional_edit_falloff='SMOOTH', proportional_size=0.00439353)
m.mirror_object =  axis_ob
$\endgroup$
1
$\begingroup$
import bpy

C = bpy.context

if C.object is not None:
    obj = C.object # Let's work with the active object
    if "The One" not in obj.modifiers: #only create it if it's not there already
        bpy.ops.object.modifier_add(type='MIRROR')
        obj.modifiers[len(obj.modifiers)-1].name = "The One" #rename the new modifier
                                                             #that is now the last modifier
    bpy.ops.object.empty_add(type='CUBE') #The default locatioon is at cursor anyway. 
                                          #It becomes active after creation.
    obj.modifiers["The One"].mirror_object = C.object #use the active 
                                                      #object(that is the empty)
                                                      #as the mirror object
    C.object.select_set(False) #deselect the empty
    C.view_layer.objects.active = obj
    C.object.select_set(True) #Let's select the original object as it was(probably)
else:
    print("No active object")

Obviously you can also control other properties of the modifier and you could make it into an operator that you can assign to hotkeys - see the Templates menu in the header of the Text Editor.

$\endgroup$
2
  • $\begingroup$ thansk a lot for your help! unfortunatly this method doesnt allow me to stack multiple modifiers it will stop at one modifier only, i want to be able to reuse this script multiple time, right low i can inly use it once with an object $\endgroup$
    – Fox
    Commented Dec 13, 2018 at 23:14
  • $\begingroup$ so i tried to use this new "if " method that i dont know yet on this script, ill show what i mean in the main question because i cant show my code here $\endgroup$
    – Fox
    Commented Dec 13, 2018 at 23:24

You must log in to answer this question.

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