Getting transformation from an instance from a model always returns "identity"

I want to read back transformations of instances. Currently SUComponentInstanceGetTransform of a parsed model always returns identity. Here is my code:

main(number, arguments)
int number; char *arguments[];
{
	//SUInitialize();
	SUModelRef model = SU_INVALID;

	SUModelCreateFromFile(&model, 3[arguments]);

	SUEntitiesRef entities = SU_INVALID;
	SUModelGetEntities(model, &entities);

	SUComponentInstanceRef instances[260];

	size_t szinstances;

	SUEntitiesGetInstances(entities, 260, instances, &szinstances);

	while (szinstances--)
	{
		struct SUTransformation matrix;
		SUComponentInstanceGetTransform(szinstances[instances], &matrix);

		struct FloatingPosition {float x,z,y,d;} pos = { .x = matrix.values[12], .z = matrix.values[14], .y = matrix.values[13], .d = matrix.values[15] };

		SUStringRef name = SU_INVALID; char szname[260];

		SUStringCreate(&name);

		SUComponentInstanceGetName(szinstances[instances], &name);

		SUStringGetUTF8(name, sizeof szname, szname, (size_t[]) { 0 });

		printf("%f, %f, %f - %s\n", pos.x, pos.y, pos.z, name);
	}

	SUTerminate();
	system("PAUSE");
}

Ignore the fact that there is no error checking - I converted the original calls - initially they were in the form: b(SUfunction, args) where b was defined as:

#define b(function,...)(assert(function(__VA_ARGS__)==SU_ERROR_NONE))

The problem is that the above program always returns 0 for the translation of the matrix.

And I’m sure it isn’t zero because I’ve written the model myself with the same program.

SUComponentInstanceGetTransform works correctly so that is not the problem.

You will get better help if you post minimal code that compiles and shows the issue.

Well that’s the code - the above - it’s minimal code that compiles - you only need to add prototype.

I edited the OP with function definition.

I can only guess without more information. Can we examine the model?

Here are my working minimal example and model:
sasho.txt (982 Bytes)
instances.skp (2.1 MB)

and the output

instance count: 11
returned instances count: 11
-178.889076, 143.010187, 0.000000
-254.044233, 194.434463, 0.000000
-7.293671, -80.092599, 0.000000
123.744403, -32.692322, 0.000000
-92.678913, 15.909223, 0.000000
-26.666266, 318.855231, 0.000000
-39.474006, 115.481302, 0.000000
97.426479, 63.498999, 0.000000
44.138565, 34.973466, 0.000000
1.000000, 1.000000, 1.000000
-31.832072, -37.813784, 0.000000

Here is my new_model.skp (12.2 MB) and output:

instance count: 80
returned instances count: 80
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000
0.000000, 0.000000, 0.000000

You have not created your components correctly.

This shows that the instance’s transformation.origin, the definition’s insertion point, and the model’s global origin are all at the same Point3d.

Ie, the component definition’s geometry is displaced away from the origin of it’s entities collection.

So the fault is in your model building code.


BTW, in SketchUp model space, the blue Z axis is pointing up.
(Looks like you are making models for one of the game engines.)

This is my model building code. On line 250@zone.c I clearly specify SUComponentInstanceSetTransform.

The input files I use for my program.

Additionally by the looks of things translations are being applied because when they are not the objects appears stacked at origin.

Aha I found the bug - it’s the fact that I’m saving individual components on line 256@zone.c - if you comment said line it’s showing the transformations back in the reader.

Still I think this is a bug in the sketchup api rather than my program.

I think that you may need to first add the instance to the model (or some other) entities collection, then apply a transform, set name, etc…

There are quite a few situations with the C API that a coder must do things in a certain order.
For an example of incorrect order and correct order, see topic …

Also there is an important note about using API v6.0+ (2018 and newer) with regard to releasing SUComponentInstance references. See …

Yeah I had problems with that but eventually I narrowed it down to acceptable exception on SUModelRelease which I mitigated by simply removing the call as my program is shutting down at this point anyway.

Interestingly enough - there were no problems of this sort when SUInitialize was omitted.

I think I am coming around to agree with you.

I think that using the SUComponentInstanceSaveAs(instance, "instance.skp"); statement works like the Make Component command in the application. It makes a unique definition and replaces the instance with an new instance of this uniqueified definition (hence why the “In Model” definition list is so bloated with cloned components.) These new instances have their entities origin at the model’s origin as a default it seems, which displaces their geometry.

I’d urge you to locate the source of those exceptions. They indicate something isn’t doing the right thing - which makes it very hard to debug any other problem. Ensure you have an error free base first.

Another thing to note, when adding definitions, materials etc, using the SU*Create functions, add them to the model before you do any further work on them.

1 Like