Help with SUComponentBehavior


#1

I am adding my behavior like this:

                SUComponentBehavior behavior;
		behavior.component_always_face_camera = true;
		SUComponentDefinitionSetBehavior(comp_def, &behavior);

and then later I add an instance of my component definition:

                SUComponentDefinitionRef componentDefs[1];
		size_t retrieved = 0;

		SUModelGetComponentDefinitions(model, 1, componentDefs, &retrieved);

		SUComponentInstanceRef instance = SU_INVALID;
		SUComponentDefinitionCreateInstance(*componentDefs, &instance);

		SUEntitiesAddInstance(entities, instance, NULL);

But it is not behaving the correct way. It does not always face camera. When it opens it waits for the user to click then it adds it at the origin. Help please.


#2

I tried getting the SUComponentDefinitionEntities and filling those, then adding the componentdefinition to the model, but it says it cannot open an empty component. I really need help please. @tt_su


#3

Got a complete code snippet that demonstrate the issue?


#4

This is how I create my faces. I tried using SUComponentDefintionGetEntities, but when I save and open, I get the error cannot open empty component.

 SUEntitiesRef entities = SU_INVALID;
    	SUModelGetEntities(model, &entities);
    
    	SUGeometryInputRef input = SU_INVALID;
    	SUGeometryInputCreate(&input);
    
    	SUPoint3D* vertices = new SUPoint3D[3];
    
    	SUPoint2D* uvCoords = new SUPoint2D[3];
    	
    	for (int i=0; i < count; i++) {
    		SUPoint3D t;
    		t.x = pPoints[i].x;
    		t.y = pPoints[i].y;
    		t.z = pPoints[i].z;
    		vertices[i] = t;
    	}
    
    
    	for (int p=0; p < count; p++) {
    		SUPoint2D a;
    		a.x = uvPoints[p].x;
    		a.y = uvPoints[p].y;
    		uvCoords[p] = a;
    	}
    
    	
    	SUGeometryInputSetVertices(input, 3, vertices);
    
    	if(count > 2){
    		SULoopInputRef loop = SU_INVALID;
    		SULoopInputCreate(&loop);
    
    	
    		for(size_t j=0; j < count; j++)
    		{
    			SULoopInputAddVertexIndex(loop, j);
    		}
    
    		size_t face_index0;
    		SUGeometryInputAddFace(input, &loop, &face_index0);
    		
    		SUMaterialInput mat_input;
    		mat_input.material = mat;
    		mat_input.num_uv_coords = 3;
    		
    		for(int k=0, b=2; k < 3; k++, b--)
    		{
    			mat_input.vertex_indices[k] = k;
    			mat_input.uv_coords[k] = uvCoords[k];
    		}
    
    		SUGeometryInputFaceSetFrontMaterial(input, face_index0, &mat_input);
    		SUGeometryInputFaceSetBackMaterial(input, face_index0, &mat_input);
    		SUEntitiesFill(entities, input, true);

This is how I create my component definition and instance

      SUComponentDefinitionRef comp_def = SU_INVALID;
        	SUComponentDefinitionCreate(&comp_def);
        	
        	SUComponentBehavior behavior;
        	behavior.component_always_face_camera = true;
        
        	SUComponentDefinitionSetBehavior(comp_def, &behavior);
        
        	SUModelAddComponentDefinitions(model, 1, &comp_def);
        
        	SUComponentInstanceRef instance = SU_INVALID;
        
        	SUComponentDefinitionCreateInstance(comp_def, &instance);
    
            SUEntitiesAddInstance(entities, instance, NULL);

This is how I am saving my model:

    SUModelSaveToFile(model, Filename);

#5

As I mentioned in the SO thread - that code will add the faces to the root of the model - and then create an empty definition with an instance of that definition in the model root. The definition have no use here and it will be purged by SketchUp automatically.

Not sure why you see problems when using SUComponentDefintionGetEntities - but without a complete example to plugin into the compiler and run I cannot say much. There could be details between the fragments of code you posted that cause it.

Have you looked at the examples that ship with the SDK?

(Please mark blocks of code as Preformatted Text when you post - otherwise it very hard to read the code. I edited the last post for you.)


#6

I noticed that when I use the SUComponentDefinitionGetEntities, The component is not directly placed in the model. When I save out the model and open, it is empty. But if I open the component window, I see my component in there and have to click on it to place. Is there a way to load it automatically into the model?


#7

When you use SUComponentDefintionGetEntities - you still use SUEntitiesAddInstance, right?


#8

Yes, right before I save and add my component definition

        SUComponentInstanceRef instance = SU_INVALID;
	SUComponentDefinitionCreateInstance(component, &instance);

	SUEntitiesAddInstance(entities, instance, NULL);
	
	SUModelAddComponentDefinitions(model, 1, &component);

	SUModelSaveToFile(model, Filename);

#9

I suspect that adding the definition after trying to add the instance is creating issues.
(A bit hard to say exactly without a full overview of what’s going on. That’s why I’ve been requesting a complete example.)

But try this if its not already your current structure:

  1. Create your definition.
  2. Add the geometry to the definition
  3. Add the definition to the model
  4. Add the instance to the model

Adding an instance of a definition that hasn’t been added to the model doesn’t sound like it would work. I’d be surprised if it did.


#10

Thank you very much for your help and getting back to me quickly. I was not using my head and was using SUEntitiesAddInstance with the component entities instead of the model entities.
Again thanks for your help and getting back to me.


#11

No problem.

So have you resolved your problem now?


#12

Oh - hm… that would lead to a recursive model. The API should not allow that. Did you get any error status returned from the functions when you tried that?