Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Null Pointer Issue when Generating New Material from existing material in C++

pts.user2
Contributor

Null Pointer Issue when Generating New Material from existing material in C++

pts.user2
Contributor
Contributor

Hello everyone,

I'm currently working on a project where I'm attempting to create a new material from an existing one.

However, I'm encountering an issue in my C++ code where the pointer is returning NULL pointer for the new Material generated. Surprisingly, when I use the same code in Python, the new material is successfully generated. 

 

  • C++ Code

 

Ptr<MaterialLibraries> matLibs = app->materialLibraries();
Ptr<MaterialLibrary> matLib = matLibs->itemByName("Fusion 360 Material Library");
Ptr<Materials> materials = matLib->materials();
Ptr<Material> existingMaterial = materials->item(0);
Ptr<Material> newMaterial = materials->addByCopy(existingMaterial, "NewMaterial");​

 

  • Python Code

 

matlibs = app.materialLibraries
matLib = matlibs.itemByName('Fusion 360 Material Library')
materials = matLib.materials
existingMaterial = materials.item(0)
newMaterial = design.materials.addByCopy(existingMaterial, "NewMaterial")​

 

I was wondering if anyone could assist me in identifying any potential issues within the code.

Thank you in advance for your help!

0 Likes
Reply
Accepted solutions (1)
280 Views
1 Reply
Reply (1)

boopathi.sivakumar
Autodesk
Autodesk
Accepted solution

You are not creating the material in the current design i guess which you are doing it in the python.
the code should like this

	Ptr<MaterialLibraries> matLibs = app->materialLibraries();
	Ptr<MaterialLibrary> matLib = matLibs->itemByName("Fusion 360 Material Library");
	Ptr<Materials> materials = matLib->materials();
	Ptr<Material> existingMaterial = materials->item(0);
	Ptr<Material> newMaterial = design->materials()->addByCopy(existingMaterial, "NewMaterial");

 


Boopathi Sivakumar
Senior Technology Consultant

2 Likes