0
$\begingroup$

in this code

import bpy 

def find_collection(context, item):
    collections = item.users_collection
    if len(collections) > 0:
        return collections[0]
    return context.scene.collection

def make_collection(collection_name, parent_collection):
    if collection_name in bpy.data.collections:
        return bpy.data.collections[collection_name]
    else:
        new_collection = bpy.data.collections.new(collection_name)
        parent_collection.children.link(new_collection)
        return new_collection

for o in bpy.context.selected_objects:
    P = bpy.context.selected_objects[4]
    P_collection = find_collection(bpy.context, P)
    new_collection = make_collection("Quick Particle 01", P_collection)
    new_collection.objects.link(P)
    P_collection.objects.unlink(P)

im trying to move the whole selection to a new collection i only have a single little problem, for it to work, i need the number at the end of this code

    P = bpy.context.selected_objects[4]

to be the exact number of the number of objects inside of my selection -5.. so if i have 5 objects, i need [4] if its more it dont work,if it less it doesnt work, and if its nothing witouth any braquet like this

    P = bpy.context.selected_objects

i get an error message

so im searching to do something like that

    P = bpy.context.selected_objects[***   bpy.context.numberofselected_objects"   ***]
$\endgroup$
  • $\begingroup$ Hi. Please only ask one question at a time. This site prefers specific questions with specific answers, which is hard to do if the question asks multiple things. $\endgroup$ – Ray Mairlot Dec 17 '18 at 1:37
  • $\begingroup$ for o in context.selected_objects: print(o.name) $\endgroup$ – batFINGER Dec 17 '18 at 8:52
1
$\begingroup$

In Python indexing works from the beginning AND from the end. So if you need the second to last object in a list:

somelist = [ "a" , "b" ,"c" , "d" , "e" ]
print(somelist[-2])

would print

d
| improve this answer | |
$\endgroup$
  • $\begingroup$ yes but the objects change each time depending on the selection, so how am i supposed to do ? $\endgroup$ – DB3D Dec 17 '18 at 20:53
  • $\begingroup$ how do you know which of the objects in the list is the one you need? by name? you could go through your list and check if the one you are looping through right now is the right one and then do something with it, maybe? $\endgroup$ – morph3us Dec 18 '18 at 7:12
  • $\begingroup$ no i just want to know how many object are selected ? $\endgroup$ – DB3D Dec 18 '18 at 14:32
  • 1
    $\begingroup$ the print statement was just to show how the code works. it often helps to break down problems into smaller parts and visualizing what python does by typing in smaller samplecode in the cmd. too bad you didnt get what the brackets mean, because i explained that its indexing. too lazyto google what suare brackets mean in python? the letters are just placeholders. it can be anything, its just to fill a list. if you get a name of the object, you are almost there. take that name and iterate over the meshes you have and get its type. however, as your list already would consist of blender $\endgroup$ – morph3us Dec 18 '18 at 21:30
  • 1
    $\begingroup$ objects, you wouldnt even need to do that. $\endgroup$ – morph3us Dec 18 '18 at 21:30
0
$\begingroup$

okay so i resolve the problem by myself:

in the code, i didnt need to redifine P, because for o in bpy.context... already define every object inside of the selection, so i dont need at all an what i was asking in my case in this code ...

for o in bpy.context.selected_objects:
    o_collection = find_collection(bpy.context, o)
    new_collection = make_collection("Quick Particle 01", o_collection)
    new_collection.objects.link(o)
    o_collection.objects.unlink(o)

but still, if someone know what is the code to have the number of selected item in the selection it could be cool to share

thank you

| improve this answer | |
$\endgroup$

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.