3

Python code to add objects to the 3D Cursor

I added a cube and looked at the Python Console

bpy.ops.mesh.primitive_cube_add(enter_ editmode=False, align='WORLD', location=(0, 0, 0)))

The code looked like this

When you run this code, it adds the cube to the first location you put it in, not the location of the 3D Cursor (location=(0, 0, 0))

I want to use this add-on, so the Python code has to be one line

https://github.com/InamuraJIN/CommandRecorder

How do I add an Object to the 3D Cursor's position?

Translated with www.DeepL.com/Translator (free version)

InamuraJIN
  • 157
  • 9
  • 2
    Cursor belongs to scene = context.scene then in operator location=scene.cursor.location or alternatively use align='CURSOR' See https://blender.stackexchange.com/questions/13828/precisely-move-the-3d-cursor and https://blender.stackexchange.com/questions/164734/how-to-move-selected-object-to-the-curser-location-using-a-python-command/164779#164779 – batFINGER Jul 04 '20 at 06:06
  • Thank you. I'm terrible at Python.The following code could not be executed How should I describe it?

    bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD', location=scene.cursor.location and bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, scene = context.scene, location=scene.cursor.location

    – InamuraJIN Jul 04 '20 at 06:15
  • 1
    bpy.ops.mesh.primitive_cube_add(size=2, location=bpy.context.scene.cursor.location) or bpy.ops.mesh.primitive_cube_add(size=2, align='CURSOR') Enter edit mode False is default. Defaults need not be included in call. – batFINGER Jul 04 '20 at 06:18
  • Thank you so much! Now you can Add object to the cursor position BTW, can I disable Cursor rotation? – InamuraJIN Jul 04 '20 at 06:21
  • Use first if only want to use location. Can set cursor rotation to zero AFAIK can't disable it (could hide the UI) – batFINGER Jul 04 '20 at 06:23
  • It's done! Thank you so much! – InamuraJIN Jul 04 '20 at 06:33
  • Sorry. I'm new to this site and I'm not sure How can I rate you as the Best Answer? – InamuraJIN Jul 04 '20 at 06:42

1 Answers1

2

Add new object at cursor

Default values.

If you auto complete on the operator in the python console, it shows the properties and their default values. There is no need to pass any default value to the operator.

>>> bpy.ops.mesh.primitive_cube_add(
primitive_cube_add()
bpy.ops.mesh.primitive_cube_add(size=2, calc_uvs=True, enter_editmode=False, align='WORLD', location=(0, 0, 0), rotation=(0, 0, 0), scale=(0, 0, 0))
Construct a cube mesh

To add a cube with location and rotation of 3D cursor

bpy.ops.mesh.primitive_cube_add(align='CURSOR')

To add at cursor location.

bpy.ops.mesh.primitive_cube_add(location=bpy.context.scene.cursor.location)

or equivalently, align to cursor but override rotation. Even though the zero rotation passed is default, just passing it (in tests so far) overrides cursor rotation.

bpy.ops.mesh.primitive_cube_add(align='CURSOR', rotation=(0, 0, 0))

Related

Precisely move the 3D cursor

How to move selected object to the curser location using a python command?

batFINGER
  • 84,216
  • 10
  • 108
  • 233