1
$\begingroup$

I'm not sure if this type of question belongs here, but I'm trying to write a simple add-on that exports game assets to FBX files based on collections. Each collection becomes a separate file. But an issue is that I don't want to process the root collection.

Now I am very new to python (started yesterday), and have not yet figured out how to iterate Blender's collections, so this may not be necessary. But I have a feeling the iteration will be linear (not hierarchy based). So I'm wondering if there is a way to identify the primary scene root collection of a scene? Would I just compare its name to "Scene Collection"? This seems like a bad way of handling it, but that's the best I have at the moment. On the other hand, if the very first collection of Blender's collection list is always the root, that might make it easier.

In case I find any Blender python developers willing to help, I had a couple other questions regarding Blender and python in general. FYI, I come from a decently strong C++ background with absolutely no python experience..

  1. Does python use spacing, tabs, and end of lines as part of its syntax? Really strange how this works. So for example, how does the python interpreter know where a for loop body starts and ends? Just based on spacing and indentation?

  2. Several add-ons appear to be assigning their own made-up variables to blender object types. Such as bpy.types.Scene.myVariable = make_pointer(MyObject) or bpy.types.Object.myBool = BoolProperty(..).. Does a simple line such as this create a new variable in the class type that the addon can use as custom data? If so, how do they not conflict with each other?

  3. scene = bpy.context.scene - I'm assuming this type of code does not copy the entire scene to a new instance, but rather copies the variable that represents a reference to the scene? Something like that?

  4. if obj.name in scene.objects: - Really curious as to what this is doing exactly. It almost looks like it is using the name property of the object to search in the scene list of objects for that specific name string. Is that the case? If so, wow.

Thanks for any assistance. I know I should be learning to use python correctly before I start diving into building a Blender add-on. Hoping I can get away with knowing just enough to pull this off.

Thanks!

$\endgroup$
2
$\begingroup$

The master collection of the current scene can be accessed using bpy.context.scene.collection. You can access all scenes regardless of the one currently active through bpy.data.scenes. For example if you want to access a scene by name and get its master collection, you could use bpy.data.scenes["Scene"].collection. Alternatively you can also access them by index.

Once you have the master collection of the scene you can get all objects regardless of the sub-collections within, using all_objects. So for example bpy.context.scene.collection.all_objects would give you all objects in the current scene. You can iterate through them like this (which prints the name of the objects):

for obj in bpy.context.scene.collection.all_objects:
    print(obj.name)

Regarding your other questions:

  1. Yes Python requires indentation to determine what part of the code is within a specific scope. I recommend using an editor that helps you write syntactically correct code.

  2. Add-on can add their own variables using code similar to the one you described and it's precisely so that they can store custom data. They can conflict each other if the have the same name, which is why their name is usually based on the add-on and often bundle their configuration in a PointerProperty which points to an object that contains the actual variables/settings/properties that the add-on uses. This avoids adding several variables.

  3. Correct, the scene variable just holds a references to the scene object.

  4. Correct. I think this works (might be slightly wrong) because SceneObjects, which is the type of bpy.context.scene.objects, implements a custom __eq__ method, that allows to determine equality by their unique name. If their names are equal they are the same object.

You can actually test of lot of these interactively in Blender's scripting workspace where you have a console that let's you run these commands in Blender without having to write an add-on right away.

$\endgroup$
  • $\begingroup$ Please note that storing variables for add-ons and other aspects that rely on bpy are exclusive to Blender's API and you won't encounter them when programming in Python outside of Blender. $\endgroup$ – Robert Gützkow Sep 4 at 7:50

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.