ObjectCollection.createWithArray Method

Parent Object: ObjectCollection
Defined in namespace "adsk::core" and the header file is <Core/Application/ObjectCollection.h>

Description

Creates a new ObjectCollection that is initialized with the content of the provided array.

Syntax

This is a static method.

returnValue = adsk.core.ObjectCollection.createWithArray(array)
This is a static method.

#include <Core/Application/ObjectCollection.h>

returnValue = adsk::core::ObjectCollection::createWithArray(array);

Return Value

Type Description
ObjectCollection Returns the newly created ObjectCollection or null in the case of failure.

Parameters

Name Type Description
array Base[] An array of Fusion objects that are used to populate the ObjectCollection. For this method to succeed, getting the input type correct is critical. The term "array" is used generically in the API documentation and describes different things depending on the language being used.

When using C++, std::vector is used to input and output a list of items.However, this particular method requires that the content of the vector be of type core.base. If you have a vector of other types, you need to convert it to core.base. The sample below illustrates converting a vector of Occurrence objects into a vector of core.Base objects.

std::vector<Ptr<adsk::fusion::Occurrence>> occArray = rootComp->occurrences()->asArray();

std::vector<Ptr<adsk::core::Base>> occs{ occArray.begin(), occArray.end() };

Ptr<ObjectCollection> objColl = ObjectCollection::createWithArray(occs);


When using Python, a Python List or Tuple is used as input. Something not obvious is that when an array is returned from a method or property it's not returned as a standard Python List but is a special API-specific class called "vector". Typically, you don't notice this isn't a List because it supports Python iteration like a List does. Because the createWithArray method requires a standard Python list as input, you need to convert it to a standard list before using it in the createWithArray method. For example, the Occurrences.asArray method returns an "array" of the occurrences, which really returns a vector object of the occurrences. The code below converts the vector into a standard list so it can be used to create an ObjectCollection.

occList = list(root.Occurrences.asArray())

objColl = adsk.core.ObjectCollection.craeteWithArray(occList)

Version

Introduced in version May 2023