Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Fusion360 crashing when applying Materials to bodies

4 REPLIES 4
Reply
Message 1 of 5
pts.user2
241 Views, 4 Replies

Fusion360 crashing when applying Materials to bodies

Hi all,

 

I've been encountering an issue while trying to apply materials to the body of a component using the Fusion API.

Most of our models are successfully accepting materials, but there's one particular model where this operation is causing the application to crash.

We've observed a "pure virtual function" error during debugging.

Ptr<MaterialLibraries> materialLibraries = app->materialLibraries();
for (int index = 0; index < materialLibraries->count(); index++)
{
    Ptr<MaterialLibrary> library = materialLibraries->item(index);
    if (library) {
        Ptr<Materials> materials = library->materials();
        if (materials) {
            Ptr<Material> material = materials->itemById(newMaterialId);
            if (material) {
                bool status = body->material(material);
                break;
            }
        }
    }
}

I'm wondering if anyone could offer some guidance or suggestions on what might be going wrong in this case. 

Any help or insights would be greatly appreciated.

 

Thank you in advance for your assistance!

4 REPLIES 4
Message 2 of 5
jeff.pek
in reply to: pts.user2

What sort of body is it that's triggering this problem?

 

The best way for someone to help with this is if you could share a model that it's happening with, and a script that triggers the problem.

 

Jeff

Message 3 of 5
pts.user2
in reply to: jeff.pek

Hi Jeff,

I have attached the model for reference and also below is the code I'm trying.

struct ComponentInfo {
    Ptr<Component> componentId;
    adsk::core::Ptr<adsk::fusion::BRepBody> bodycomp;
    std::string applyMaterialId;
};

std::vector<ComponentInfo> componentInfos;

//Below componentIds will be changed so please fetch the componentId for the components after saving the model.
std::vector<std::string> componentIds = {
    "4b04a1a9-606f-4bc5-9b81-89ee7b8e64ac", //Component Id of a Auger from a model
    "df365492-ba4b-4d0e-9b8d-dbbaefe4c5b7", //Component Id of a 7200K1 Motor:1 from a model
    "6c648293-2ec9-41bd-bb87-48dd1f0eb9a8", //Component Id of a Chasis:1 from a model
    "96537c81-da6a-4a2e-8a21-7d5033aad28b"  // Component Id of a Compression Spring:1 and Compression Spring:2 from a model
};

for (const std::string& componentId : componentIds) {
    Ptr<Component> component = allComponents->itemById(componentId);
    adsk::core::Ptr<adsk::fusion::BRepBody> bodycomp = component->bRepBodies()->item(0); // Body on which material is to be applied
    std::string applyMaterialId; // Material Id of material to be applied

    if (componentId == "4b04a1a9-606f-4bc5-9b81-89ee7b8e64ac") {
        applyMaterialId = "PrismMaterial-230";
    } else if (componentId == "df365492-ba4b-4d0e-9b8d-dbbaefe4c5b7") {
        applyMaterialId = "PrismMaterial-164";
    } else if (componentId == "6c648293-2ec9-41bd-bb87-48dd1f0eb9a8") {
        applyMaterialId = "PrismMaterial-230";
    } else if (componentId == "96537c81-da6a-4a2e-8a21-7d5033aad28b") {
        applyMaterialId = "PrismMaterial-092";
    }

    componentInfos.push_back({ component, bodycomp, applyMaterialId });
}

for (const ComponentInfo& info : componentInfos) {
    Ptr<Component> componentId = info.componentId;
    adsk::core::Ptr<adsk::fusion::BRepBody> bodycomp = info.bodycomp;
    std::string applyMaterialId = info.applyMaterialId;

    for (int index = 0; index < materialLibraries->count(); index++) {
        Ptr<MaterialLibrary> library = materialLibraries->item(index);
        if (library) {
            Ptr<Materials> materials = library->materials();
            if (materials) {
                Ptr<Material> material = materials->itemById(applyMaterialId);
                if (material) {
                    bool status = bodycomp->material(material);
                    break;
                }
            }
        }
    }
}

 

Message 4 of 5
KrisKaplan
in reply to: pts.user2

Hi,

 

Sorry for the delay. I was able to reproduce the crash with this design and these materials. I created issue FUS-140521 to track this. It looks like it is related to a deleted visual material reference, and seems to be related to the material changes in this pattern being done in a script without the scene getting a chance to update between.

 

FWIW: While we look into fixing this, it looks like you might be able to work around this by allowing Fusion to update the viewport between each material change with a call to adsk::doEvents().

 

Kris



Kris Kaplan
Message 5 of 5
pts.user1
in reply to: KrisKaplan

Hi @KrisKaplan,

 

Thank you for creating issue FUS-140521 to track the crash issue.

I will try to use adsk::doEvents() and will let you know if it's working.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report