0

In the following post, I learned how to create cubes of the size I want.

how to set a custom scale size for an object once shortcut button is pressed?

def draw(self, context):
    object = context.object
    layout = self.layout
row = layout.row()
row.operator("mesh.primitive_cube_add").scale=(1, 1, 1)

I used this code to add an additional location and rotation value. However, it does not work

I want to add position, scale and rotation values to the row.operator("mesh.primitive_cube_add") code.

Here's the code:

def draw(self, context):
    object = context.object
    layout = self.layout
row = layout.row()
row.operator("mesh.primitive_cube_add").location=(0, 0, 5), rotation=(0, 0, 10), scale=(1, 1, 1)

======================== Add ===========================

How to pass multiple operator properties via UI layout?

brockmann's answer in the next post is the way I want it to be.

I write it down as below, and all functions work fine.

I want to write the rotation value in one line like location or scale, but I do not know how to write it, so I wrote it in 3 lines.

Thank you brockmann!

import bpy
import math
.
.
.
def draw(self, context):
    object = context.object
    layout = self.layout 

    row = layout.row()
    props = row.operator("mesh.primitive_cube_add")
    props.enter_editmode=False
    props.location = (0, 0, 5)
    props.rotation[0] = math.radians(0)
    props.rotation[1] = math.radians(0)
    props.rotation[2] = math.radians(10)
    props.scale = (1, 1, 1)

minia
  • 35
  • 6
  • 1
    What are the symptoms of "does not work"? Do you get a python exception / error, or are the specified location and rotation overridden? – Marty Fouts Sep 28 '21 at 14:02
  • There are many Sidebars that I am using, and I have registered this script in one of them and am using it. But when I put that code, the sidebar doesn't appear for some reason. Errors are usually checked in INFO, but no text is shown. – minia Sep 28 '21 at 14:48
  • props.rotation = (math.radians(0), math.radians(1), math.radians(2)) should do the trick, see: https://docs.blender.org/api/current/bpy.ops.mesh.html?highlight=primitive_cube_add#bpy.ops.mesh.primitive_cube_add ((float array of 3 items in [-inf, inf], (optional))) – brockmann Oct 01 '21 at 06:21
  • Thank you brockmann! May I ask you one more thing? I wonder if it is possible to create a cube by specifying the name I want when creating a cube. – minia Oct 01 '21 at 07:02
  • If you'd like to use primitive_cube_add(), answer is no (click here). Otherwise you'd have to use the Bmesh module: https://docs.blender.org/api/current/bmesh.html?highlight=bmesh#module-bmesh How to create a cube using Bmesh has already been explained over here -> https://blender.stackexchange.com/questions/63546/create-a-cube-in-blender-from-python and there are a lot of high quality Q&A's, just search for it: https://blender.stackexchange.com/search?tab=votes&q=bmesh – brockmann Oct 01 '21 at 07:12

0 Answers0