Cannot get a specific material from .skp file

The following code is used to get materials:

  // Get Material
  size_t matCount;
  SUModelGetNumMaterials(model, &matCount);
  std::vector<SUMaterialRef> su_materials(matCount);
  SUModelGetMaterials(model, matCount, &su_materials[0], &matCount);
  for (size_t i = 0; i < matCount; i++) {

      SUStringRef name = SU_INVALID;
      SUStringCreate(&name);
      SUMaterialGetName(su_materials[i], &name);

      size_t len;
      SUStringGetUTF8Length(name, &len);
      char* str = new char[len + 1];
      SUStringGetUTF8(name, len + 1, str, &len);
      SUStringRelease(&name);

      std::cout << "Material [" << i << "] " << str << std::endl;
  }

The output results:

Material [0] [Color M08]1
Material [1] Mat3d66-694071-5-7239
Material [2] Mat3d66-1025995-57-15
Material [3] Mat3d66-694071-7-2714

The material named “image2” in file “test.skp” is missing in the results: test.skp (653.6 KB)

We also have issues accessing materials in the Ruby API.
See: https://github.com/SketchUp/api-issue-tracker/issues/572

Normally the material collection attempts to hide “layer owned” and “image owned” materials because accessing them can crash the SketchUp application. (They were never meant to be directly manipulated.)

You seem to want to access an “image owned” material ? True?

If you want to use an “image owned” material you will need to create a normal “manager owned” material by creating a temporary ImageRep and assigning it as the texture for the new material.
See the: SUImageGetImageRep() function

Thanks for your reply.

Does an “image owned” material means an image reference created by directly importing a jpg/png file into the project? But the missing material I mentioned is not this type of material.

The missing material is not listed in the “In Model” material list. But it can be picked by the “Sample Paint”, which is unlike the “image owned” material.

Could you please check the attachment? It is very simple, because I have removed all the other things and only kept several faces.

I checked your post on github, tried the ruby code in the post. It turns that the missing material is the type of image.
I will try to do some further tests with C API.

Thanks again!

The following API solves the problem:

enum SUResult SUModelGetAllMaterials ( SUModelRef model,
size_t len,
SUMaterialRef materials,
size_t * count
)
Retrieves all the materials associated with a model object including those belonging to [SUImageRef] and [SULayerRef].

It makes the referencing simpler … but you must not assign any “image owned” or “layer owned” material to a SketchUp geometric entity. (The function you linked has a waring to this effect.)


And please post code correctly in the forum …
[How to] Post correctly formatted and colorized code on the forum?