0
$\begingroup$

Lets say I have 5 objects selected. The script will correctly export 5 fbx files, place them at 0-0-0 world space, export an fbx with the object name, and return them to their original position.

The problem is that each export includes all selected objects instead of 1 at a time. Shouldn't my for loop only grab each selected object 1 at a time? How do I select 5 objects, export them so a single object is inside each fbx with the corresponding name?

    def execute(self, context):
        # Get the selected objects.
        selected = bpy.context.selected_objects
        for obj in selected:

            # Store current object's name.
            obj_name = obj.name

        # Get the object's original position.
        obj_loc_x = str(obj.location.x)
        obj_loc_y = str(obj.location.y)
        obj_loc_z = str(obj.location.z)

        # Creates the path for the exported fbx.
        obj_path = os.path.join(context.scene.worth_group_tools.dir_path,
                                obj_name + "." + "fbx")

        # Sends object to zero vectors.
        obj.location.x = 0.0
        obj.location.y = 0.0
        obj.location.z = 0.0

        # Export object as fbx. Works, except all selected objects are
        # exported into single fbx instead of one at a time from the list.
        bpy.ops.export_scene.fbx(filepath=obj_path, use_selection=True)

        # Prints each object in list.  Works.
        print(obj_name)

        # Returns the object to its original position.
        obj.location.x = float(obj_loc_x)
        obj.location.y = float(obj_loc_y)
        obj.location.z = float(obj_loc_z)

    return {'FINISHED'}
$\endgroup$
1
$\begingroup$

Why your code below obj_name = obj.name is outside the for-loop? It should be in the same loop, isn't it?

Use select_set() for selection:

obj.select_set(True) : select the current object, useful for exporting selection. You need to manually deselect it after exporting, or call bpy.ops.object.select_all(action='DESELECT') to deselect all.

Minor things

Get the object's original position:

You can copy the location to a reference instead of casting it to string and convert it back.

org_obj_loc = obj.location.copy()

Sends object to zero vectors

You can assign a Vector() to it: obj.location = (0,0,0)

Whole code

def execute(self, context):
    for obj in bpy.context.selected_objects:
        bpy.ops.object.select_all(action='DESELECT')
        obj.select_set(True)
        org_loc = obj.location.copy()

        # Sends object to zero vectors.
        obj.location = (0,0,0)

        # Creates the path for the exported fbx.
        obj_path = os.path.join(context.scene.worth_group_tools.dir_path,
                                obj.name + "." + "fbx")
        # Export object as fbx. Works, except all selected objects are
        # exported into single fbx instead of one at a time from the list.
        bpy.ops.export_scene.fbx(filepath=obj_path, use_selection=True)

        # Prints each object in list.  Works.
        print(obj.name)

        # Returns the object to its original position.
        obj.location = org_loc

return {'FINISHED'}
$\endgroup$
  • $\begingroup$ I apologize for the indention error. It was correctly placed on my original code. Thank you so much! This works perfectly and is WAY cleaner. I was unaware of the obj.location.copy() function. Now everything makes sense! Thank you! $\endgroup$ – Teriander Aug 25 '19 at 18:10
  • $\begingroup$ Unfortunately I can't up-vote your solution. Says I need at least 15 rep. But this solution works! $\endgroup$ – Teriander Aug 25 '19 at 18:11

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.