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;








1












$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









share|improve this question











$endgroup$




















    1












    $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









    share|improve this question











    $endgroup$
















      1












      1








      1





      $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









      share|improve this question











      $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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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























          2 Answers
          2






          active

          oldest

          votes


















          3













          $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)





          share|improve this answer











          $endgroup$






















            3













            $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





            share|improve this answer









            $endgroup$

















              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
              );



              );













              draft saved

              draft discarded


















              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









              3













              $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)





              share|improve this answer











              $endgroup$



















                3













                $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)





                share|improve this answer











                $endgroup$

















                  3














                  3










                  3







                  $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)





                  share|improve this answer











                  $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)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 2 hours ago

























                  answered 5 hours ago









                  brockmannbrockmann

                  1,5226 silver badges31 bronze badges




                  1,5226 silver badges31 bronze badges


























                      3













                      $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





                      share|improve this answer









                      $endgroup$



















                        3













                        $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





                        share|improve this answer









                        $endgroup$

















                          3














                          3










                          3







                          $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





                          share|improve this answer









                          $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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 12 hours ago









                          Ray MairlotRay Mairlot

                          23.3k6 gold badges72 silver badges102 bronze badges




                          23.3k6 gold badges72 silver badges102 bronze badges






























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                              Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                              199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單