2
$\begingroup$

I can not change the x dimension of an object I have created using this code:

    import numpy as np
    template_object = bpy.data.objects.get('Cube')
    for y in range(3):
        for x in range(5): 
            ob = template_object.copy()
            ob.location.y = y*12
            ob.location.x = x*12
            width = np.random.randint(3,10)
            height = np.random.randint(3,10)
            print(ob.dimensions.x)
            ob.dimensions.x = height
            ob.dimensions.y = width
            bpy.context.collection.objects.link(ob)

Is this a known issue like Problems with assigning dimensions through python script? Or I am doing something wrong?

$\endgroup$
2
  • 1
    $\begingroup$ I stumbled into the issue as well. I solved it simply by changing the scale rather than dimension. $\endgroup$ Jan 21, 2022 at 16:21
  • $\begingroup$ @MarkusvonBroady thanks for your comment I just found a solution I don't know if it is correct but it works $\endgroup$
    – G M
    Jan 21, 2022 at 16:29

2 Answers 2

3
$\begingroup$

You need to update the View Layer after assigning the value to the dimensions:

import bpy
from bpy import context as C

o = C.active_object

print(o.dimensions)
o.dimensions.x = 2
C.view_layer.update()
print(o.dimensions)
o.dimensions.y = 3
C.view_layer.update()
print(o.dimensions)
o.dimensions.z = 4
C.view_layer.update()
print(o.dimensions)

Output for Suzanne:

<Vector (2.7344, 1.7031, 1.9688)>
<Vector (2.0000, 1.7031, 1.9688)>
<Vector (2.0000, 3.0000, 1.9688)>
<Vector (2.0000, 3.0000, 4.0000)>
$\endgroup$
4
  • $\begingroup$ thanks actualy the first dimension that i set works well is the second that does not $\endgroup$
    – G M
    Jan 21, 2022 at 16:39
  • $\begingroup$ thanks for your answer, unfortunatly this solution does not work when I try to change the y also the dimension remains unchanged even after the update. I have however upvoted it. $\endgroup$
    – G M
    Jan 21, 2022 at 16:46
  • $\begingroup$ @GM see edit - you don't get the same results? $\endgroup$ Jan 21, 2022 at 16:49
  • $\begingroup$ Now it works as expected, thanks! $\endgroup$
    – G M
    Jan 21, 2022 at 16:53
2
$\begingroup$

It seems that you can do the operation on the dimension only once, if you change first the y dimension you will not able to change the x dimension, I have managed to change multiple dimensions at once using this code:

ob.dimensions.xyz = [1,2,3]

Where 1,2,3 are the dimension of x,y,z of course.

import numpy as np
template_object = bpy.data.objects.get('Cube')
for y in range(3):
    for x in range(5): 
        ob = template_object.copy()
        ob.location.y = y*12
        ob.location.x = x*12
        width = np.random.randint(3,10)
        height = np.random.randint(3,10)
        ob.dimensions.xyz = [width,height,3]
        bpy.context.collection.objects.link(ob)
$\endgroup$
7
  • $\begingroup$ Have you tried printing the new values just after setting them? I still get old values printed. $\endgroup$ Jan 21, 2022 at 16:32
  • $\begingroup$ @MarkusvonBroady yes but one I print the values they are always the same I will edit my question, but this solution actually change the size of the object correctly $\endgroup$
    – G M
    Jan 21, 2022 at 16:48
  • 2
    $\begingroup$ You can also use ob.dimensions = (height, width,ob.dimensions.z) if you don't want to change the Z dimension. $\endgroup$ Jan 21, 2022 at 17:06
  • $\begingroup$ @MartyFouts [] or () are optional in multiple assignment in Python. You could also do ob.dimensions.xy = width, height, but I imagine setting all 3 values is some trick solving the issue - I couldn't reproduce the issue, though, both in Blender 2.93 and Blender 3.0... My answer solves the problem of the property not updating immediately and print displaying an outdated value (which was edited out from the question). $\endgroup$ Jan 21, 2022 at 21:36
  • 3
    $\begingroup$ @MarkusvonBroady All I was doing was observing that you can use the existing Z value in the assignment. and that ob.dimensions was sufficient, you don't need .xyz; but sure ob.dimensions = width, height, ob.dimensions.z is valid syntax. $\endgroup$ Jan 21, 2022 at 23:34

You must log in to answer this question.

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