[041] How to Precisely Target Faces Using comp.index in CityEngine CGA

Learn how to find and use the comp.index value via the Inspector panel to precisely target and apply rules to individual faces.

In procedural modeling, selecting a specific face among hundreds can be challenging with general orientation-based selectors. This tutorial demonstrates how to identify the unique index of a component and utilize it within conditional CGA rules for absolute precision in your 3D urban designs.

I previously covered split.index, but as I began creating more complex forms, I realized that comp.index deserved its own explanation, so I’ll briefly cover it here.

Why Traditional Selectors Are Sometimes Not Enough

As you proceed with rules, the number of faces increases. To pinpoint and select a specific face, you need to use a combination of various selectors. However, selectors such as front selectors, object coordinate selectors, and world coordinate selectors operate within specific spatial conditions and select all elements that meet those conditions, often resulting in multiple elements being selected. Furthermore, since you have to consider the axis, scope, and pivot when choosing a selector, selecting a specific desired element can be tricky.

Using Index Values for Precise Face Selection

In such cases, you can select only the desired element by using the index values generated when using the split or comp commands.

Attempting Face Selection Without comp.index

Let's assume we are creating a shape like the one below.

Extruded face on a sphere
img 1 : A specific face of a sphere extended using the extrude command.

This is a sphere where a specific face has been extended using extrude.

If you were to create this without using an index, you would have to find the first edge, consider which selector to use in comp, and then combine various methods.

Let's try creating it.


Lot --> primitiveSphere(10, 10, 10) A
# Created a sphere primitive.

A --> comp(f){right = B | all = X.}
# Separating all faces on the right side of the sphere.

B -->
    alignScopeToAxes(world.xyz)
    # Aligned the scope to the world axes.
    comp(f){ top : color(1,0,0) C | all = X.}
    # Selecting
 only the top face.

C --> extrude(10) X.

The code above separates the faces based on the first edge, aligns the scope of that face, selects only the top part, and extrudes it by 10m.

The result is:

Incorrect selection result
img 3 : A neighboring face behind the intended target was extruded instead.

Hmm... unlike the first image, the face one step behind was extruded.

Because selectors pick faces based on a defined range or orientation, it is difficult to select the exact face you want with total certainty.

Selecting Faces Directly with comp.index

Now, let's try creating it using the index value.


Lot --> primitiveSphere(10, 10, 10) A
# Created a sphere primitive.

A --> comp(f){all : B}
# Separating all faces of the sphere.

B --> 
    case comp.index == 72 : 
        extrude(10) color(1,0,0) X.
    else : X.
# Targeting the face whose comp.index value is 72,
# and extruding it by 10m.

The result is as follows:

Correct selection result
img 5 : The intended face is correctly targeted and extruded.

This time, the intended face was selected correctly.

How to Find comp.index Values in CityEngine

So, how did I know the index value of that face was 72?

Previously, when we discussed split.index, I showed you how to check the value in the Model Hierarchy.

I will do the same this time.

First, let's run the rule up to the stage where the sphere is separated into individual faces and open the Model Hierarchy panel.


Lot --> primitiveSphere(10, 10, 10) A

A --> comp(f){all : B}
# Separating all right-side faces of the sphere.
Model Hierarchy selection
img 7 : Finding the highlighted face's position in the Model Hierarchy panel.

When I selected that face, the corresponding position was highlighted in the Model Hierarchy panel.

Now, you just have to count down in order from the top. Ah! Counting from the bottom might be faster.

Yes, counting that way, I found the index was 72.

It takes a bit of time, but it's not difficult.

However, what if there are 1,000 elements separated by comp? Or 10,000?

You would waste a huge amount of time just counting indices manually.

Using the Inspector Panel to Check comp.index

In such cases, you can easily find the index value using the Model Hierarchy panel and the Inspector panel together as shown below.

Video: Checking comp.index values using the Inspect Model feature.

After clicking 'Inspect model' in the Model Hierarchy panel and selecting the desired element in the 3D View, you can check the comp.index value in the Inspector panel. (In the case of split, you can obtain the split.index value, along with various other information.)

You simply need to use this value as a condition in a case-else statement.

Try extruding different faces by changing the index value.

Next, we will look at how to use split.index and comp.index together to create a shape.

"I was about to turn twenty-one, an age when people do not yet think the past was better. Only later do we realize we were already living in the days we would one day miss."
— Jean-Baptiste Andrea, Watching Over Her
Continue Learning
Explore complete tutorial collections for CityEngine and Global Mapper.

Comments

Popular posts from this blog

029] How to Split Polygon Areas with Intersecting Lines in Global Mapper

[008] How to Understand CGA Rule Structure in CityEngine (Beginner Guide)

046] How to Create Parallel Offset Lines in Global Mapper