Run Part of the Code Only Once

Hello people.

I tried the second addon from the Addon Tutorial in the API documentation.
A change I want to do: In the example it changes the end position when I change the steps and the cursor position is already changed. I want to avoid that. I want to save the cursor position once and after that the end position shouldn’t change anymore.
I tried several things to achieve this. Either it didn’t work or Blender crashed. Could you please explain me how it works?

cu
Mr.Yeah

this script?

import bpy
from bpy import context

scene = context.scene
cursor = scene.cursor_location
obj = scene.objects.active

# Use a fixed value for now, eventually make this user adjustable
total = 10

# Add 'total' objects into the scene
for i in range(total):
    obj_new = obj.copy()
    scene.objects.link(obj_new)

    # Now place the object in between the cursor
    # and the active object based on 'i'
    factor = i / total
    obj_new.location = (obj.location * factor) + (cursor * (1.0 - factor))

and what do you wanna achieve? Restore the original cursor location?

Yes, but the final version of that which has the operator property implemented. I want to achieve that the variable ‘cursor’ stays constant while adjusting ‘total’ so the end of the array of objects doesn’t change. Atm it changes when the position of the 3D cursor is changed.

It does stay constant, as there’s no assignment in the code:

<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>
<Vector (-0.4285, -5.9186, -1.3020)>

Of course you can move the cursor by clicking with LMB in 3d view. You can’t lock it. But you can change the code to use a defined location instead of cursor location.

Try this




import bpy
from bpy import context

from mathutils import Vector # < -----

scene = context.scene
cursor = scene.cursor_location
obj = scene.objects.active

end_position = Vector((4.0, 4.0, 4.0))# < -----


# Use a fixed value for now, eventually make this user adjustable
total = 10

# Add 'total' objects into the scene

for i in range(total):obj_new = obj.copy()
scene.objects.link(obj_new)
# Now place the object in between the cursor
# and the active object based on 'i'
factor = i / total
#obj_new.location = (obj.location * factor) + (cursor * (1.0 - factor))
obj_new.location = (obj.location * factor) + (end_position * (1.0 - factor)) # < -----




You will have to create a different gui and add float vector property to be able to enter coordinates for end position, but it will not change every time the cursor position changes.