While writing a rather simple add-on to create cylinders based on the built-in add cylinder operator in blender, I came upon a strange phenomenon:
The add-on reads the 3D cursor location and saves them to a variable to place the new cylinder at the obtained coordinates. As soon as I begin to change some settings in the operator properties panel, the coordinates jump to random values in absolutely regular intervals (the random coordinates are also consistent over time). E.g. if I set the vertex count of the cylinder, every third click on the IntProperty panel triggers the jump in coordinates. The object of course jumps to these coordinates, too. The next click resets the location to the correct until the third change, like this:
class InnerDiameterCylinder(bpy.types.Operator):#bl_idname and so on go here
#props go here
vertex_get = bpy.props.IntProperty(name = "Vertex Count", description = "", default = 10, min = 1, max = 100)
#define user_cursor_pos variable here to prevent the variable to assume the new cursor position each time changes in the properties are made
user_cursor_pos = (0.0, 0.0, 0.0)
initialized = False
def execute(self, context):
[INDENT=2]if not initialized:[/INDENT]
[INDENT=3]self.user_cursor_pos = bpy.context.scene.cursor_location
self.initialized = True
[/INDENT]
[INDENT=2]bpy.ops.mesh.primitive_cylinder_add(vertices = self.vertex_get, location = self.user_cursor_get)
[/INDENT]
If I just read the cursor location in the execute method directly, this behavior doesn’t happen. The object then follows the 3D cursor to a new (correct and non-random) location. However, I would like to make the behavior as close as possible to the original add cylinder functionality.
It would help if you’d post the actual non-working code instead of a ‘paraphrased version’ because I see at least two obvious bugs in that code snippet which may or may not be due to ‘paraphrasing errors’.
Sorry about the late reply, but I was busy with other stuff the last days…
By paraphrasing I actually meant leaving out parts like all the other property definitions and registering functions that already worked. But yeah, I also see a paraphrasing mistake: the object location should use self.user_cursor_pos and not self.user_cursor_get.
Apart from that, I guess the way I save the cursor location might be a problem for some reason.
Anyway, I have created a stripped-down version of the script to isolate the problem a little bit better:
Which actually makes more sense since now, the behavior of the standard add_cylinder is properly replicated in my modification (location and rotation settings also didn’t work before).