KirilStrezikozin / BakeMaster-Blender-Addon

Welcome to BakeMaster, a powerful and feature-packed baking solution created for Blender - an open-source 3D Computer graphics software.
Other
34 stars 6 forks source link

PROBABLYBUG: Objects are not added respecting their order in the Blender Outliner #58

Closed KirilStrezikozin closed 6 months ago

KirilStrezikozin commented 8 months ago

This bug report is:

Describe the bug When multiple objects are added to BakeMaster, they do not follow the order they had in Blender Outliner.

To Reproduce Steps to reproduce the behavior:

  1. Select several objects.
  2. Click on 'Add' in BakeMaster.
  3. The order of objects in BakeMaster is not the same as in the Outliner.

Expected behavior Should the order of objects be preserved?

Screenshots 1a. Order in the Outliner: image 1b. Order in BakeMaster: image

The reason probably is BakeMaster respects the alphabetical order of Meshes: image

The behavior of the following case is unknown: 2a. Order in the Outliner: image 2b. Order in BakeMaster: image

Desktop (please complete the following information):

Additional context Found and reported by @KirilStrezikozin.

KirilStrezikozin commented 7 months ago

Investigating the reason

  1. Using the meshes listed in behavior 2a above, the built-in bpy.context.selected_objects contains objects of order put into BakeMaster (BakeMaster itself does not contain any order manipulations unless name matching is used):

    # Console input:
    >>> C.selected_objects
    # Console Output:
    [
    bpy.data.objects['Circle'],
    bpy.data.objects['Sphere'],
    bpy.data.objects['Plane'],
    bpy.data.objects['Torus']
    ]
  2. The type of bpy.context.selected_objects is <class 'list'>. Lists are ordered:

    assert type(C.selected_objects) == list
  3. It is noticeable that the order of objects in bpy.context.selected_objects is the order they were edited to a scene. Thus, a conclusion is that BakeMaster adds objects from Blender Outliner in the order these objects were added to a Blender scene in the first place.

  4. Blender Outliner has an Alphabetic Sorting option that is defined in each Outliner area.

Solution

~Introduce a property in BakeMaster Addon Preferences~ The best idea is to adapt to each situation. Every time the BakeMaster Object Add operator is called (and name matching is not used), find the first Outliner area in the current screen. If it has alphabetic object sorting, add selected objects to BakeMaster via:

sorted(C.selected_objects, key=lambda x: x.name)

If it doesn't have alphabetic sorting, plain add objects to BakeMaster. Overall, this way BakeMaster adds objects in the order they are in the Outliner, which makes it more convenient and visually appealing to the user.