Why can't I access the 'name' of an object when looping through the scene's objects?How to get 3d view grid properties?How to create properties for new object typesWhy is Pie empty when no object in layerHow to emulate tracking to objects like the 'Nokia snake game'?TypeError: 'Scene' object is not iterable“'NoneType' object has no attribute 'action'” error when looping over objects with PythonAttributeError while changing the display units using python scriptPython 2.8 - why does linking the image texture node throw an error?How to remove certain part of the object name for hundreds of objects?
Can you board the plane when your passport is valid less than 3 months?
What should come first—characters or plot?
Tex Quotes(UVa 272)
about to retire but not retired yet, employed but not working any more
Is first Ubuntu user root?
How do I feed my black hole?
LINQ for generating all possible permutations
Can $! cause race conditions when used in scripts running in parallel?
Why did my folder names end up like this, and how can I fix this using a script?
How would a low-tech device be able to alert its user?
Can you grapple/shove when affected by the Crown of Madness spell?
Limitations with dynamical systems vs. PDEs?
Duplicate instruments in unison in an orchestra
Changing JPEG to RAW to use on Lightroom?
Are game port joystick button circuits more than plain switches? Is this one just faulty?
Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?
Why is getting a PhD considered "financially irresponsible"?
Expressing the act of drawing
What's special ammo?
Talk interpreter
How were medieval castles built in swamps or marshes without draining them?
Is one hour layover sufficient at Singapore Changi Airport (Indian citizen travelling from US to India)?
Redacting URLs as an email-phishing preventative?
Nothing like a good ol' game of ModTen
Why can't I access the 'name' of an object when looping through the scene's objects?
How to get 3d view grid properties?How to create properties for new object typesWhy is Pie empty when no object in layerHow to emulate tracking to objects like the 'Nokia snake game'?TypeError: 'Scene' object is not iterable“'NoneType' object has no attribute 'action'” error when looping over objects with PythonAttributeError while changing the display units using python scriptPython 2.8 - why does linking the image texture node throw an error?How to remove certain part of the object name for hundreds of objects?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
I am trying to delete all the objects that start with 'LTR' but am getting the following error: 'tuple' object has no attribute 'name'
Any suggestions are much appreciated!
for obj in enumerate(bpy.context.scene.objects):
namestr=obj.name #this throws the error
if len(namestr)>=3:
name2=namestr[3:]
if name2=='LTR':
delete_object(namestr) #this is a function that deletes the object
python scripting objects
$endgroup$
add a comment |
$begingroup$
I am trying to delete all the objects that start with 'LTR' but am getting the following error: 'tuple' object has no attribute 'name'
Any suggestions are much appreciated!
for obj in enumerate(bpy.context.scene.objects):
namestr=obj.name #this throws the error
if len(namestr)>=3:
name2=namestr[3:]
if name2=='LTR':
delete_object(namestr) #this is a function that deletes the object
python scripting objects
$endgroup$
add a comment |
$begingroup$
I am trying to delete all the objects that start with 'LTR' but am getting the following error: 'tuple' object has no attribute 'name'
Any suggestions are much appreciated!
for obj in enumerate(bpy.context.scene.objects):
namestr=obj.name #this throws the error
if len(namestr)>=3:
name2=namestr[3:]
if name2=='LTR':
delete_object(namestr) #this is a function that deletes the object
python scripting objects
$endgroup$
I am trying to delete all the objects that start with 'LTR' but am getting the following error: 'tuple' object has no attribute 'name'
Any suggestions are much appreciated!
for obj in enumerate(bpy.context.scene.objects):
namestr=obj.name #this throws the error
if len(namestr)>=3:
name2=namestr[3:]
if name2=='LTR':
delete_object(namestr) #this is a function that deletes the object
python scripting objects
python scripting objects
edited 12 hours ago
Ray Mairlot
23.3k6 gold badges72 silver badges102 bronze badges
23.3k6 gold badges72 silver badges102 bronze badges
asked 13 hours ago
vndepvndep
867 bronze badges
867 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
enumerate(iterable, start=0)
returns a tuple for each element from any given iterable and its position based on the actual iteration. Proof using the Console:
>>> for i in enumerate(["Hello","World"]):
>>> print (i)
(0, "Hello")
(1, "World")
A tuple is a composite data type, composed out of two elements (item1, item2)
. If you want to access its individual components (left, right)
you can either use the index operator []
or create 2 variables for the actual return value (tuple) on the fly: left, right = ("Hello", "World")
:
>>> my_tuple = ("Hello", "World")
>>> my_tuple[0]
"Hello"
left, right = ("Hello", "World")
>>> left
"Hello"
Means in your case that you can't access the object and its name this way because the returned tuple by the enumerate()
function in your loop has no name
property. Proof using the Console again:
>>> for ob in enumerate(C.scene.objects):
... print (ob)
(0, bpy.data.objects['Cube'])
(1, bpy.data.objects['Light'])
(2, bpy.data.objects['Camera'])
In summary: you have to find a way splitting the components of the tuple if you want to access each element separately which basically leads into the following pattern:
>>> for left, right in enumerate(C.scene.objects):
... print (left, right, "Name:", right.name)
0 <bpy_struct, Object("Cube")>, Name: Cube
1 <bpy_struct, Object("Light")> Name: Light
2 <bpy_struct, Object("Camera")> Name: Camera
Also notice that python has a lot of awesome tools dealing with strings. In order to determine whether a string is part of a another string (substring), you could use the in
operator to test its "membership":
>>> "ell" in "HelloWorld"
True
To remove the objects, you might want to create some kind of black list before removing it. The following example removes all objects in the scene that have "Cam" in their name (substring):
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if "Cam" in ob.name:
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
There is also str.startswith(prefix)
returning True
if the string begins with the prefix
passed:
>>> "Hello World".startswith("Hell")
True
The following example removes all found objects whose names begin with "Cam":
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if ob.name.startswith("Cam"):
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
$endgroup$
add a comment |
$begingroup$
When you use enumerate
, an item in the list and an index/counter of where you are in the loop is returned to the variable (obj
) instead of just the item. This is why you get the error, because in this case obj
is a tuple ((index, listItem)
) which does not have a name
property itself.
The common loop syntax when using enumerate
is:
for index, obj in enumerate(bpy.context.scene.objects):
obj
will now contain the actual object as expected.
However, if you don't need an index of where you are in the loop you do not need to use enumerate
. Remove it and it will work fine, e.g.:
for obj in bpy.context.scene.objects:
You could also access the second item in the tuple when trying to access the name
:
namestr=obj[1].name
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "502"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f149785%2fwhy-cant-i-access-the-name-of-an-object-when-looping-through-the-scenes-obje%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
enumerate(iterable, start=0)
returns a tuple for each element from any given iterable and its position based on the actual iteration. Proof using the Console:
>>> for i in enumerate(["Hello","World"]):
>>> print (i)
(0, "Hello")
(1, "World")
A tuple is a composite data type, composed out of two elements (item1, item2)
. If you want to access its individual components (left, right)
you can either use the index operator []
or create 2 variables for the actual return value (tuple) on the fly: left, right = ("Hello", "World")
:
>>> my_tuple = ("Hello", "World")
>>> my_tuple[0]
"Hello"
left, right = ("Hello", "World")
>>> left
"Hello"
Means in your case that you can't access the object and its name this way because the returned tuple by the enumerate()
function in your loop has no name
property. Proof using the Console again:
>>> for ob in enumerate(C.scene.objects):
... print (ob)
(0, bpy.data.objects['Cube'])
(1, bpy.data.objects['Light'])
(2, bpy.data.objects['Camera'])
In summary: you have to find a way splitting the components of the tuple if you want to access each element separately which basically leads into the following pattern:
>>> for left, right in enumerate(C.scene.objects):
... print (left, right, "Name:", right.name)
0 <bpy_struct, Object("Cube")>, Name: Cube
1 <bpy_struct, Object("Light")> Name: Light
2 <bpy_struct, Object("Camera")> Name: Camera
Also notice that python has a lot of awesome tools dealing with strings. In order to determine whether a string is part of a another string (substring), you could use the in
operator to test its "membership":
>>> "ell" in "HelloWorld"
True
To remove the objects, you might want to create some kind of black list before removing it. The following example removes all objects in the scene that have "Cam" in their name (substring):
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if "Cam" in ob.name:
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
There is also str.startswith(prefix)
returning True
if the string begins with the prefix
passed:
>>> "Hello World".startswith("Hell")
True
The following example removes all found objects whose names begin with "Cam":
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if ob.name.startswith("Cam"):
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
$endgroup$
add a comment |
$begingroup$
enumerate(iterable, start=0)
returns a tuple for each element from any given iterable and its position based on the actual iteration. Proof using the Console:
>>> for i in enumerate(["Hello","World"]):
>>> print (i)
(0, "Hello")
(1, "World")
A tuple is a composite data type, composed out of two elements (item1, item2)
. If you want to access its individual components (left, right)
you can either use the index operator []
or create 2 variables for the actual return value (tuple) on the fly: left, right = ("Hello", "World")
:
>>> my_tuple = ("Hello", "World")
>>> my_tuple[0]
"Hello"
left, right = ("Hello", "World")
>>> left
"Hello"
Means in your case that you can't access the object and its name this way because the returned tuple by the enumerate()
function in your loop has no name
property. Proof using the Console again:
>>> for ob in enumerate(C.scene.objects):
... print (ob)
(0, bpy.data.objects['Cube'])
(1, bpy.data.objects['Light'])
(2, bpy.data.objects['Camera'])
In summary: you have to find a way splitting the components of the tuple if you want to access each element separately which basically leads into the following pattern:
>>> for left, right in enumerate(C.scene.objects):
... print (left, right, "Name:", right.name)
0 <bpy_struct, Object("Cube")>, Name: Cube
1 <bpy_struct, Object("Light")> Name: Light
2 <bpy_struct, Object("Camera")> Name: Camera
Also notice that python has a lot of awesome tools dealing with strings. In order to determine whether a string is part of a another string (substring), you could use the in
operator to test its "membership":
>>> "ell" in "HelloWorld"
True
To remove the objects, you might want to create some kind of black list before removing it. The following example removes all objects in the scene that have "Cam" in their name (substring):
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if "Cam" in ob.name:
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
There is also str.startswith(prefix)
returning True
if the string begins with the prefix
passed:
>>> "Hello World".startswith("Hell")
True
The following example removes all found objects whose names begin with "Cam":
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if ob.name.startswith("Cam"):
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
$endgroup$
add a comment |
$begingroup$
enumerate(iterable, start=0)
returns a tuple for each element from any given iterable and its position based on the actual iteration. Proof using the Console:
>>> for i in enumerate(["Hello","World"]):
>>> print (i)
(0, "Hello")
(1, "World")
A tuple is a composite data type, composed out of two elements (item1, item2)
. If you want to access its individual components (left, right)
you can either use the index operator []
or create 2 variables for the actual return value (tuple) on the fly: left, right = ("Hello", "World")
:
>>> my_tuple = ("Hello", "World")
>>> my_tuple[0]
"Hello"
left, right = ("Hello", "World")
>>> left
"Hello"
Means in your case that you can't access the object and its name this way because the returned tuple by the enumerate()
function in your loop has no name
property. Proof using the Console again:
>>> for ob in enumerate(C.scene.objects):
... print (ob)
(0, bpy.data.objects['Cube'])
(1, bpy.data.objects['Light'])
(2, bpy.data.objects['Camera'])
In summary: you have to find a way splitting the components of the tuple if you want to access each element separately which basically leads into the following pattern:
>>> for left, right in enumerate(C.scene.objects):
... print (left, right, "Name:", right.name)
0 <bpy_struct, Object("Cube")>, Name: Cube
1 <bpy_struct, Object("Light")> Name: Light
2 <bpy_struct, Object("Camera")> Name: Camera
Also notice that python has a lot of awesome tools dealing with strings. In order to determine whether a string is part of a another string (substring), you could use the in
operator to test its "membership":
>>> "ell" in "HelloWorld"
True
To remove the objects, you might want to create some kind of black list before removing it. The following example removes all objects in the scene that have "Cam" in their name (substring):
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if "Cam" in ob.name:
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
There is also str.startswith(prefix)
returning True
if the string begins with the prefix
passed:
>>> "Hello World".startswith("Hell")
True
The following example removes all found objects whose names begin with "Cam":
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if ob.name.startswith("Cam"):
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
$endgroup$
enumerate(iterable, start=0)
returns a tuple for each element from any given iterable and its position based on the actual iteration. Proof using the Console:
>>> for i in enumerate(["Hello","World"]):
>>> print (i)
(0, "Hello")
(1, "World")
A tuple is a composite data type, composed out of two elements (item1, item2)
. If you want to access its individual components (left, right)
you can either use the index operator []
or create 2 variables for the actual return value (tuple) on the fly: left, right = ("Hello", "World")
:
>>> my_tuple = ("Hello", "World")
>>> my_tuple[0]
"Hello"
left, right = ("Hello", "World")
>>> left
"Hello"
Means in your case that you can't access the object and its name this way because the returned tuple by the enumerate()
function in your loop has no name
property. Proof using the Console again:
>>> for ob in enumerate(C.scene.objects):
... print (ob)
(0, bpy.data.objects['Cube'])
(1, bpy.data.objects['Light'])
(2, bpy.data.objects['Camera'])
In summary: you have to find a way splitting the components of the tuple if you want to access each element separately which basically leads into the following pattern:
>>> for left, right in enumerate(C.scene.objects):
... print (left, right, "Name:", right.name)
0 <bpy_struct, Object("Cube")>, Name: Cube
1 <bpy_struct, Object("Light")> Name: Light
2 <bpy_struct, Object("Camera")> Name: Camera
Also notice that python has a lot of awesome tools dealing with strings. In order to determine whether a string is part of a another string (substring), you could use the in
operator to test its "membership":
>>> "ell" in "HelloWorld"
True
To remove the objects, you might want to create some kind of black list before removing it. The following example removes all objects in the scene that have "Cam" in their name (substring):
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if "Cam" in ob.name:
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
There is also str.startswith(prefix)
returning True
if the string begins with the prefix
passed:
>>> "Hello World".startswith("Hell")
True
The following example removes all found objects whose names begin with "Cam":
import bpy
objects_to_remove = []
for ob in bpy.context.scene.objects:
if ob.name.startswith("Cam"):
objects_to_remove.append(ob)
bpy.ops.object.delete("selected_objects": objects_to_remove)
edited 2 hours ago
answered 5 hours ago
brockmannbrockmann
1,5226 silver badges31 bronze badges
1,5226 silver badges31 bronze badges
add a comment |
add a comment |
$begingroup$
When you use enumerate
, an item in the list and an index/counter of where you are in the loop is returned to the variable (obj
) instead of just the item. This is why you get the error, because in this case obj
is a tuple ((index, listItem)
) which does not have a name
property itself.
The common loop syntax when using enumerate
is:
for index, obj in enumerate(bpy.context.scene.objects):
obj
will now contain the actual object as expected.
However, if you don't need an index of where you are in the loop you do not need to use enumerate
. Remove it and it will work fine, e.g.:
for obj in bpy.context.scene.objects:
You could also access the second item in the tuple when trying to access the name
:
namestr=obj[1].name
$endgroup$
add a comment |
$begingroup$
When you use enumerate
, an item in the list and an index/counter of where you are in the loop is returned to the variable (obj
) instead of just the item. This is why you get the error, because in this case obj
is a tuple ((index, listItem)
) which does not have a name
property itself.
The common loop syntax when using enumerate
is:
for index, obj in enumerate(bpy.context.scene.objects):
obj
will now contain the actual object as expected.
However, if you don't need an index of where you are in the loop you do not need to use enumerate
. Remove it and it will work fine, e.g.:
for obj in bpy.context.scene.objects:
You could also access the second item in the tuple when trying to access the name
:
namestr=obj[1].name
$endgroup$
add a comment |
$begingroup$
When you use enumerate
, an item in the list and an index/counter of where you are in the loop is returned to the variable (obj
) instead of just the item. This is why you get the error, because in this case obj
is a tuple ((index, listItem)
) which does not have a name
property itself.
The common loop syntax when using enumerate
is:
for index, obj in enumerate(bpy.context.scene.objects):
obj
will now contain the actual object as expected.
However, if you don't need an index of where you are in the loop you do not need to use enumerate
. Remove it and it will work fine, e.g.:
for obj in bpy.context.scene.objects:
You could also access the second item in the tuple when trying to access the name
:
namestr=obj[1].name
$endgroup$
When you use enumerate
, an item in the list and an index/counter of where you are in the loop is returned to the variable (obj
) instead of just the item. This is why you get the error, because in this case obj
is a tuple ((index, listItem)
) which does not have a name
property itself.
The common loop syntax when using enumerate
is:
for index, obj in enumerate(bpy.context.scene.objects):
obj
will now contain the actual object as expected.
However, if you don't need an index of where you are in the loop you do not need to use enumerate
. Remove it and it will work fine, e.g.:
for obj in bpy.context.scene.objects:
You could also access the second item in the tuple when trying to access the name
:
namestr=obj[1].name
answered 12 hours ago
Ray MairlotRay Mairlot
23.3k6 gold badges72 silver badges102 bronze badges
23.3k6 gold badges72 silver badges102 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Blender Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fblender.stackexchange.com%2fquestions%2f149785%2fwhy-cant-i-access-the-name-of-an-object-when-looping-through-the-scenes-obje%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown