0
$\begingroup$

I want to dynamically create planes from within a script. These planes have to have a certain dimension. I'm struggling to set the dimension from the script.

Here is an excerpt from the script:

            bpy.ops.mesh.primitive_plane_add(size=1, enter_editmode=False, align='WORLD', location=(plane_x, 0, plane_z), scale=(1, 1, 1))
            bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, snap=False, snap_elements={'INCREMENT'}, use_snap_project=False, snap_target='CLOSEST', use_snap_self=True, use_snap_edit=True, use_snap_nonedit=True, use_snap_selectable=False)


            plane = bpy.data.objects['Plane']
            plane.name = image_name
            plane = bpy.data.objects[image_name]

            print("setting plane x dimension to ", dimension_x)
            plane.dimensions[0] = dimension_x
            print("plane x dimension is ", plane.dimensions[0])

            plane.dimensions[1] = dimension_y

The lines for creating the plane (as well as rotating it) have been taken from Blender's Info output that are shown when I add and rotate a plane through the GUI. Not sure whether this is best practice but it seems to work so far.

In the script I'm also renaming the plane to a generated name, which is also working fine.

What is failing is the change of the dimension. I can change it as shown in the script, but when I re-read it it still contains the old value. Therby it is strange that I can change the dimension very well with more or less the same command for the newly created/rotated/renamed plane object in the Python console.

So what I'm missing here?

Thanks, Mario

$\endgroup$
3
  • 1
    $\begingroup$ I think it's a quirk of the API. The dimensions are not an actual property of the object, it's derived from its mesh contents and the object scale. So my guess is that it is updated on the next frame after your script is executed. Try using bpy.app.timers.register(lambda: print(plane.dimensions[0]), first_interval=0.001) for example. ie add a timer function to execute your code with a slight delay for the value to be updated. You may be able to force update the property manually but I don't know how. There might be something in the docs about it. $\endgroup$
    – Gorgious
    May 8, 2023 at 21:40
  • $\begingroup$ @Gorgious the problem is not the print() command. I added this just for debugging purposes here. Btw., the problem is the same when I attempt to change the scaling of the plane. In the Python console it's working as expected, but in the script nothing is happening. $\endgroup$
    – Mario
    May 8, 2023 at 22:01
  • $\begingroup$ @Mario: i would prefer if you would provide a code which we could run - and don't have to manually change a lot of things just to try it out. We are helping you in our free time so i would - if i were you - make it as easy as possible for those who help me $\endgroup$
    – Chris
    May 9, 2023 at 5:02

1 Answer 1

0
$\begingroup$

this worked for me:

import bpy

bpy.ops.mesh.primitive_plane_add(size=1, enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))
bpy.ops.transform.rotate(value=1.5708, orient_axis='X', orient_type='GLOBAL', orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type='GLOBAL', constraint_axis=(True, False, False), mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, snap=False, snap_elements={'INCREMENT'}, use_snap_project=False, snap_target='CLOSEST', use_snap_self=True, use_snap_edit=True, use_snap_nonedit=True, use_snap_selectable=False)

image_name = "JOE"
dimension_x = 5
dimension_y = 15

plane = bpy.data.objects['Plane']
plane.name = image_name
plane = bpy.data.objects[image_name]

print("setting plane x dimension to ", dimension_x)
plane.dimensions[0] = dimension_x

bpy.context.view_layer.update()

print("plane x dimension is ", plane.dimensions[0])
$\endgroup$
1
  • $\begingroup$ This is working indeed, but apparently only "by accident" since there is only changed plane.dimensions[0]. The problem is really associated with the same issue as addressed here: blender.stackexchange.com/questions/44307/… The trick is that all three dimension parameters have to be assigned as a single tuple. Not sure, but I guess this is a bug of blender. $\endgroup$
    – Mario
    May 9, 2023 at 17:39

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