collection iteration and renaming, output unpredictable results #107027

Closed
opened 2023-04-17 10:26:28 +02:00 by bar · 8 comments

System Information
Operating system: Linux-5.19.0-38-generic-x86_64-with-glibc2.35 64 Bits
Graphics card: NVIDIA GeForce RTX 3080 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 515.105.01

Blender Version
Broken: version: 3.4.1, branch: blender-v3.4-release, commit date: 2022-12-19 17:00, hash: rB55485cb379f7
Worked: (newest version of Blender that worked as expected)

Short description of error
[Please fill out a short description of the error here]

collection iteration and renaming, output unpredictable results
please run the script with attached file and check the results.

this script suppose to rename all object with lower case and prefix.
unfortunately, after running the script on N number of objects the results are unpredictable


import bpy

prefix = "blender_is_great"
print("##########################")


if bpy.data.objects:
    for obj in bpy.data.objects: # workaround list(bpy.data.objects)
        print(obj)
       
        new_name = f"{prefix}_{obj.name.lower()}"
        obj.name = new_name
**System Information** Operating system: Linux-5.19.0-38-generic-x86_64-with-glibc2.35 64 Bits Graphics card: NVIDIA GeForce RTX 3080 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 515.105.01 **Blender Version** Broken: version: 3.4.1, branch: blender-v3.4-release, commit date: 2022-12-19 17:00, hash: `rB55485cb379f7` Worked: (newest version of Blender that worked as expected) **Short description of error** [Please fill out a short description of the error here] collection iteration and renaming, output unpredictable results please run the script with attached file and check the results. this script suppose to rename all object with lower case and prefix. unfortunately, after running the script on N number of objects the results are unpredictable ------- ``` import bpy prefix = "blender_is_great" print("##########################") if bpy.data.objects: for obj in bpy.data.objects: # workaround list(bpy.data.objects) print(obj) new_name = f"{prefix}_{obj.name.lower()}" obj.name = new_name ```
bar added the
Severity
Normal
Status
Needs Triage
Type
Report
labels 2023-04-17 10:26:28 +02:00
Member

Hi @barakooda , there's a name length limit to all ID data blocks (64 characters). That's why when your prefix gets too long, they will get truncated (if that's what you are referring to as "unpredictable").

Hi @barakooda , there's a name length limit to all ID data blocks (64 characters). That's why when your prefix gets too long, they will get truncated (if that's what you are referring to as "unpredictable").
Author

Hi @ChengduLittleA
no just run the code and check this image for reference:

do you see the unwanted repeating ?
image

Hi @ChengduLittleA no just run the code and check this image for reference: do you see the unwanted repeating ? ![image](/attachments/52e80c10-ec4a-45a8-ade5-7cbc67d73b05)
330 KiB
Member

@barakooda Looks that's what exactly what your script does?

@barakooda Looks that's what exactly what your script does?
Author

@ChengduLittleA

no,
the script adds the prefix only a single time.

@ChengduLittleA no, the script adds the prefix only a single time.
Member

Hi @barakooda , I can confirm that's indeed what's happening. I think I know the reason why the script is doing it in such a way. When you change the name of an object, blender will sort the object list, so bpy.data.objects would change. By naming stuff this way, you will potentially iterate over the same object because the list order changed.

Instead, you could use for ob in bpy.data.objects.values(), this way it preserves the list order and you only iterate over objects once.

Hi @barakooda , I can confirm that's indeed what's happening. I think I know the reason why the script is doing it in such a way. When you change the name of an object, blender will sort the object list, so `bpy.data.objects` would change. By naming stuff this way, you will potentially iterate over the same object because the list order changed. Instead, you could use `for ob in bpy.data.objects.values()`, this way it preserves the list order and you only iterate over objects once.
Member

This is sort of touched by https://docs.blender.org/api/3.6/info_gotcha.html

Closest to it is here:

This could be more explicit, but I do think this is the same ballpark.

@ideasman42 , @sergey : would you agree?

This is sort of touched by https://docs.blender.org/api/3.6/info_gotcha.html Closest to it is here: - https://docs.blender.org/api/3.6/info_gotcha.html#help-my-script-crashes-blender - https://docs.blender.org/api/3.6/info_gotcha.html#unfortunate-corner-cases This could be more explicit, but I do think this is the same ballpark. @ideasman42 , @sergey : would you agree?
Philipp Oeser added
Module
Python API
Status
Needs Info from Developers
and removed
Status
Needs Triage
labels 2023-04-21 11:38:06 +02:00

Indeed. Maybe can have a bullet point of common cases (without giving full-blown code snippet) ?

Indeed. Maybe can have a bullet point of common cases (without giving full-blown code snippet) ?

Noted in docs, closing.

Noted in docs, closing.
Blender Bot added
Status
Archived
and removed
Status
Needs Info from Developers
labels 2023-04-21 13:43:43 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107027
No description provided.