How to make a DC that when instanced, has a Material already attached?

I have made a DC which has a Material embedded within in, which was assigned to both a hidden Face and also to the single Instance of the DC in the creation model before I saved the Component out. The DC has the canned “Material” attribute set to the name of that Material.

When I load this DC (Sketchup.active_model.definitions.load ) into a completely empty scene, both the Component and the Material appear in their respective browser dialogs, as expected.

When I instance the Component for the first time, the Material appears to be attached to the Instance, which is what I want to happen every time.

However, when I instance it a second time, the Instance Material is nil, although the Component Options dialog still shows the material name. I have to type a different Material name (e.g. “red”) into that attribute, at which point that Material is attached to the Instance (and created first, if it doesn’t already exist).

The Component Options dialog only updates to show the correct Material for each Instance if there is actually a Material attached, but retains the name of the Material of the previously visited Instance if you pick one which has no Material attached, the dialog is therefore misleading.

How can I make an Instance always have that Material attached when created?

This is all within the context of a custom Extension, so presumably I could do some kind of Observer trickery to spot the new Instance and make it good, but I’d really rather not have to.

Thanks in advance.

I just tested this out and was able to reproduce what you describe if I just have a material name in the attribute field, like this:

But if I put =“Wood_Floor” in the field, the material seems to stick with all instances correctly.

Is your material in quotes?

Interesting. Thank you for this.

Entering =“<material_name>” instead of just <material_name> does indeed always result in the creation of Instances with that Material already attached, which is one success.

However…

If you change the Material attribute using the CO dialog, you also have to be sure to use the equals and quotes, otherwise it reverts to the previous behaviour (subsequent Instances do not have the Material attached).

Also, whatever you type in there (with or without equals and quotes) replaces the default =“MyMaterial” in the Component itself and will then be used for all subsequent Instances. If you do use equals and quotes, then those subsequent Instances get that other Material attached. If not, they get nothing, as before.

I’d really like the users not to have to enter the equals and quotes into the CO dialog, given that the existing value is presented there without them. Is this somehow possible by indirecting that attribute via another basic string attribute? In other words, Material=“$Color”.

I’d also like all new Instances to get the default Material, instead of the last one you changed an Instance to, as the use of alternative Materials is the exception not the rule.

OK…

If you add a user attribute (e.g. Color) and then set Material to be hidden and =Color it solves the first problem. You can enter the name of an existing Material (or the name/value of one to be created on demand) without needing to use equals or quotes, and everything behaves.

However, the last string you entered into the CO dialog still persists to subsequent new Instances.

Also, if you enter an invalid string (neither the name of an existing Material nor a color/value for a new one), you don’t see any error.

That is a tricky one Simon… If you find an answer I’d be interested to know what it is. I tried a few things without any luck but I got to thinking there used to be a way to make a counter of sorts with DCs where every new instance had a value of zero. Then there was some formula that said “if this value is zero, the material is white, otherwise set it to myVal”… then once the material was set once, the trigger value would never be zero again… I don’t remember all of the hacky details but it might give you an idea or two.

– Matt