2
$\begingroup$

I want to code a Addon to simplify my workflow. I need to change the offset of my Arraytool regularly, so I want to import the regular slider from the Array Tool into my GUI. Is there any way to do that? BR

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes, but you'll have to find a way to get the object's modifiers and it's easier if you know in advance the modifier's name and are sure it won't change.

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        obj = bpy.data.objects.get("Cube")  # Case sensitive !!
        if not obj:
            return
        modifiers = obj.modifiers
        array_modifier = modifiers.get("Array")  # Case sensitive !!
        if not array_modifier:
            return
        layout.prop(array_modifier, "relative_offset_displace")
        layout.prop(array_modifier, "constant_offset_displace")


def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Result :

enter image description here

Here's a second solution that relies on the active object and automatically adds the information for each array in the object modifiers :

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout
        obj = context.active_object
        if not obj:
            return
        modifiers = obj.modifiers
        array_modifiers = (m for m in modifiers if m.type == "ARRAY")
        for array in array_modifiers:
            box = layout.box()
            box.label(text=array.name)
            box.prop(array, "relative_offset_displace")
            box.prop(array, "constant_offset_displace")


def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Say you have 3 array modifiers :

enter image description here

Result :

enter image description here

$\endgroup$
4
  • 1
    $\begingroup$ Hey there, the second version seems to be the perfect fit for my usecase. Thank u VERY much for the help. $\endgroup$
    – Kr3o
    Dec 9, 2021 at 11:52
  • $\begingroup$ Cheers ! Note you can add any property you want that way, just make sure to enable developer extras in the preferences and hover your mouse over a field, it will tell you the python attribute name you can reuse in the layout.prop expression $\endgroup$
    – Gorgious
    Dec 9, 2021 at 11:57
  • $\begingroup$ Developer extras are already enabled. I've been trying to get this work for over a week and have gotten nowhere close to a final result. So thanks again mate. $\endgroup$
    – Kr3o
    Dec 9, 2021 at 11:59
  • $\begingroup$ Yeah it's really hard to grasp everything at start. I suggest you study the python templates in the Text editor (Templates > Python) and try to understand how things are linked, begin with the simple ones at first. Good luck ! $\endgroup$
    – Gorgious
    Dec 9, 2021 at 12:01

You must log in to answer this question.

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