1
$\begingroup$

I assume this code defines "i" as the selected objects while the selected objects X location is set to the value from the active object. Right?

def LocX(context):
    for i in bpy.context.selected_objects:
        i.location.x = bpy.context.active_object.location.x

Then there is the following code with the poll function and then the LocX command is being executed. Does this mean it will check each non active object and execute the LocX command onto it? As far as I understand the return {'FINISHED'} is a statement to say that an evaluation is done.

@classmethod
def poll(cls, context):
    return context.active_object != None

def execute(self, context):
    LocX(context)
    return {'FINISHED'}
$\endgroup$
  • 1
    $\begingroup$ At this point you will benefit a lot more from learning Python beyond the context of Blender - even if your sole goal is to be able to make an add-on. pythonprogramminglanguage.com $\endgroup$ – zeffii Jan 8 '16 at 19:53
4
$\begingroup$

You can think of the poll function as the bouncer that determines if the operator should be allowed to go on and process something. The return value of the poll function determines if the execute function is called.

The poll in this case checks if the scene has an active_object, if it does then context.active_object will evaluate to True, and therefore be not None or (different syntax same meaning) != None.

The LocX function here doesn't even use the context parameter that was passed. The same could have been written this way:

def LocX():
    for i in bpy.context.selected_objects:
        i.location.x = bpy.context.active_object.location.x

....

@classmethod
def poll(cls, context):
    return context.active_object != None

def execute(self, context):
    LocX()
    return {'FINISHED'}

or this way

def LocX(context):
    for i in context.selected_objects:
        i.location.x = context.active_object.location.x

....

@classmethod
def poll(cls, context):
    return context.active_object != None

def execute(self, context):
    LocX(context)
    return {'FINISHED'}

The for-loop in LocX loops over the collection of selected_objects and for each object therein sets its location.x to be the same as active_object.location.x.


An aside

As a convention we tend to use o or ob or obj for temporary references to objects. i is usually (again this is a convention) used for numbers, integers, indices.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.