1

New to Blender and python, but not programming. I have been following a youtube series which creates a panel in the 3D window. The author was using blender 2.8 and I have 3.3.12.

I want to create a mesh cube at location .5, .5, .5 and with size=1 With the current code as follows, I can only either add size or location but not both. Is there a way to do this?

Here is my current code;

bl_info = {
    "name":  "ObjectAdder",
    "author":  "ezywingman",
    "version":  (1, 0, 0),
    "blender":  (3, 3, 12),
    "location":  "View3d > Add > Mesh > ObjectAdder",
    "description": "Example",
    "warning":  "",
    "doc_url":  "",
    "category":  "Add Mesh",
}

Load the Python language into the system

import bpy

class TestPanel(bpy.types.Panel): bl_label = "ObjectAdder" bl_idname = "PT_TestPanel" # set where the panel will reside bl_space_type = 'VIEW_3D' # select the region where the panel will reside # in this instance 'UI' is the side of the 3D panel bl_region_type = 'UI' # place in a current tab, or create a new one # in this instance it's a new one bl_category = "Object_Creation"

def draw(self, context):
    layout = self.layout
    row    = layout.row()
    row.label(text = "Add an object", icon = 'CUBE')
    row    = layout.row()
    row.operator("mesh.primitive_cube_add", icon='CUBE').location=(.5,.5,.5)
    row    = layout.row()
    row.operator("mesh.primitive_uv_sphere_add", icon='SPHERE').location=(1,1,1)
    row    = layout.row()
    row.operator("object.text_add", icon = 'FILE_FONT', text="Font Button")
    row    = layout.row()

class PanelA(bpy.types.Panel): bl_label = "Scaling" bl_idname = "PT_PanelA" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Object creation" bl_parent_id = 'PT_TestPanel' bl_options = {'DEFAULT_CLOSED'}

def draw(self, context):
    layout = self.layout
    obj = context.object
    row    = layout.row()
    row.label(text = "Select an option to scale your object", icon = 'FONT_DATA')
    row    = layout.row()
    row.operator("transform.resize")
    col = layout.column() 
    col.prop(obj, "scale")

class PanelB(bpy.types.Panel): bl_label = "Specials" bl_idname = "PT_PanelB" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Object creation" bl_parent_id = 'PT_TestPanel' bl_options = {'DEFAULT_CLOSED'}

def draw(self, context):
    layout = self.layout
    row    = layout.row()
    row.label(text = "Select a special Option", icon = 'COLOR_BLUE')
    row    = layout.row()                
    row.operator("object.shade_smooth", icon='MOD_SMOOTH', text= "Set Smooth Shading")
    row.operator("object.subdivision_set")
    row    = layout.row()
    row.operator("object.modifier_add")



def register(): bpy.utils.register_class(TestPanel) bpy.utils.register_class(PanelA) bpy.utils.register_class(PanelB)

def unregister(): bpy.utils.unregister_class(TestPanel) bpy.utils.unregister_class(PanelA) bpy.utils.unregister_class(PanelB)

if name == "main": register()

now sorted using this updated part if the code thanks to 'Gorgious'

    def draw(self, context):
        layout = self.layout
        row    = layout.row()
        row.label(text = "Add an object", icon = 'CUBE')
        row    = layout.row()
        op = row.operator("mesh.primitive_cube_add", icon='CUBE')
        op.location=(.5,.5,.5)
        op.size = (1)
        row     = layout.row()
ezywingman
  • 11
  • 3

1 Answers1

0

Credit to Gorgious, I re-write part of your code below:

def draw(self, context):
    .... etc
    row    = layout.row()
    op = row.operator("mesh.primitive_cube_add", icon='CUBE')
    op.location=(.5,.5,.5)
    row    = layout.row()
    ... etc

In other words (from the linked solution), operator() returns it's object identity, so you can then use "op.another_method()" to operate on the object stored in variable "op".

james_t
  • 5,446
  • 8
  • 29
  • That's it. Many thanks, I think I get it now. New code now looks like;
    def draw(self, context):
        layout = self.layout
        row    = layout.row()
        row.label(text = "Add an object", icon = 'CUBE')
        row    = layout.row()
        op = row.operator("mesh.primitive_cube_add", icon='CUBE')
        op.location=(.5,.5,.5)
        op.size = (1)
        row     = layout.row()
    
    – ezywingman Dec 19 '23 at 21:32