1
$\begingroup$

I want to create an Addon for a shelf planning tool. The slider should change the offset of the Array Modifier. I already have the slider in my panel, but when I tell the offset to be the slider I always get the message: offset is too short? Even when the slider is 2 for example. How can I change the code to make it work?

import bpy
from bpy.types import Panel, Operator

class RGL_PT_Panel(Panel):
     bl_space_type = "VIEW_3D"
     bl_region_type = "UI"
     bl_label = "Regalplannung"
     bl_category = "Regalplannung"

     def draw(self, context):

         layout = self.layout

         row = layout.row()
         col = row.column()
         col.operator("object.regalplannung", text = "Aufüllen")  
     
         col.prop(context.object, 'Offset_X', text = "Abstand X", 
         slider=True)  
         col.prop(context.object, 'Offset_Y', text = "Abstand Y", 
         slider=True)  

class RGL_OT_regalplannung(Operator):
     """Vollständig gefüllt & Ausrichten"""
     bl_idname = "object.regalplannung"
     bl_label = "Regalplannung"
     bl_description = "Regalplannung in Blender"


     bpy.types.Object.Offset_X = bpy.props.FloatProperty(min = -5, max = 5)
     bpy.types.Object.Offset_Y = bpy.props.FloatProperty(min = -5, max = 5)

     def execute(self, context):

        selection = bpy.context.selected_objects

        for sel in selection:
            
            parent = sel.parent
            parent_0 = parent.dimensions [0]
            parent_1 = parent.dimensions [1]
            parent_2 = parent.dimensions [2]

            mod_array = sel.modifiers.new("Vollständig gefüllt", 'ARRAY')

            mod_array.fit_type = 'FIT_LENGTH'
            mod_array.relative_offset_displace[0] = 0

            if parent_0 < parent_1:
                mod_array.fit_length = parent.dimensions[0] - sel.dimensions[0]    
                mod_array.relative_offset_displace[0] = bpy.types.Object.Offset_X

            else:

                mod_array.fit_length = parent.dimensions[1] - sel.dimensions[1]
                mod_array.relative_offset_displace[1] = bpy.types.Object.Offset_Y
                        
        return {'FINISHED'}
     
bpy.utils.register_class(RGL_PT_Panel)
bpy.utils.register_class(RGL_OT_regalplannung)
```
$\endgroup$
3
  • $\begingroup$ it would be helpful, if you script is runnable...so please indent it right and it should be runnable and reproduce your error. Thanks. $\endgroup$
    – Chris
    Dec 6, 2021 at 10:25
  • $\begingroup$ @Chris sry I forgot to indent it, but now it should be runnable $\endgroup$
    – Kr3o
    Dec 6, 2021 at 10:32
  • $\begingroup$ Runnable... are you sure? The mentioned error occurs with line mod_array.relative_offset_displace[0] = 0 where you set all values for Relative Offset to 0. Your script breaks off here. $\endgroup$
    – relaxed
    Dec 6, 2021 at 12:37

1 Answer 1

2
$\begingroup$

Your problem is that you are trying to access bpy.types.Object.Offset_X as an object, but that's the type. instead, you need to access sel.Offset_X. You have the same problem with Offset_Y.

The modified code is at lines 51 and 56 in my version.

Here is a version of your script that works, provided I have the selected object parented to another object:

import bpy
from bpy.types import Panel, Operator

class RGL_PT_Panel(Panel):
     bl_space_type = "VIEW_3D"
     bl_region_type = "UI"
     bl_label = "Regalplannung"
     bl_category = "Regalplannung"

     def draw(self, context):

         layout = self.layout

         row = layout.row()
         col = row.column()
         col.operator("object.regalplannung", text = "Aufüllen")  
     
         col.prop(context.object, 'Offset_X', text = "Abstand X", 
         slider=True)  
         col.prop(context.object, 'Offset_Y', text = "Abstand Y", 
         slider=True)  

class RGL_OT_regalplannung(Operator):
     """Vollständig gefüllt & Ausrichten"""
     bl_idname = "object.regalplannung"
     bl_label = "Regalplannung"
     bl_description = "Regalplannung in Blender"


     bpy.types.Object.Offset_X = bpy.props.FloatProperty(min = -5, max = 5)
     bpy.types.Object.Offset_Y = bpy.props.FloatProperty(min = -5, max = 5)

     def execute(self, context):

        selection = bpy.context.selected_objects

        for sel in selection:
            
            parent = sel.parent
            parent_0 = parent.dimensions [0]
            parent_1 = parent.dimensions [1]
            parent_2 = parent.dimensions [2]

            mod_array = sel.modifiers.new("Vollständig gefüllt", 'ARRAY')

            mod_array.fit_type = 'FIT_LENGTH'
            mod_array.relative_offset_displace[0] = 0

            if parent_0 < parent_1:
                mod_array.fit_length = parent.dimensions[0] - sel.dimensions[0]    
                mod_array.relative_offset_displace[0] = sel.Offset_X

            else:

                mod_array.fit_length = parent.dimensions[1] - sel.dimensions[1]
                mod_array.relative_offset_displace[1] = sel.Offset_Y
                        
        return {'FINISHED'}
     
bpy.utils.register_class(RGL_PT_Panel)
bpy.utils.register_class(RGL_OT_regalplannung)
```
$\endgroup$
3
  • $\begingroup$ Hey there, omg thank you. I've been searching for a solution for ages. Thank you $\endgroup$
    – Kr3o
    Dec 8, 2021 at 7:29
  • $\begingroup$ Is there any way that the slider changes the value in real time? Not just when i press the button the add the Array modifier? $\endgroup$
    – Kr3o
    Dec 8, 2021 at 8:01
  • $\begingroup$ Yes, but that would require a modal operator. You might want to find a tutorial under 'blender python modal operator' search before you try it. $\endgroup$ Dec 8, 2021 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.