How to change active object display type on select?

Idea is to do xray for the active object only. But from what I can tell turning real xray on/off is an all or nothing thing. No single object xray for now, correct?

So to detour around that for the moment, I am trying to make some changes to object_select.c

  1. When selecting a mesh in object mode, if a checkbox in overlay settings is true, AND if there is an object already selected, change its display type (aka Object.display_type, accessed in Object Properties->Viewport Display->Display As) to SOLID or TEXTURED, so it doesn’t stay in WIRE

  2. Change the new object’s viewport display to WIRE

Here’s an example of what I’ve tried for the first half of that, setting current object to SOLID before setting the new object to WIRE, in
‘source-source->blender->editors->object->object_select.c’

void ED_object_base_select(Base *base, eObjectSelect_Mode mode, ViewLayer *view_layer)
{
  if (mode == BA_INVERT) {
    mode = (base->flag & BASE_SELECTED) != 0 ? BA_DESELECT : BA_SELECT;
  }

  if (base) {
    switch (mode) {
      case BA_SELECT:
        if ((base->flag & BASE_SELECTABLE) != 0) {
          view_layer->basact->object->data.display_type = OB_SOLID;
          base->flag |= BASE_SELECTED;
        }
        break;

Seems pretty straightforward, but I can’t get it working. Whatever I try to tell it, I get something like

error C2039: 'display_type': is not a member of 'Object'

OR

error C2224: left of '.display_type' must have struct/union type     

I’ve tried quite a few variations of

view_layer->basact->object->data.display_type = OB_SOLID;

and replaced ‘display_type’ with ‘shading.type’ like you see in ‘view3d_edit.c’

Maybe I got stuff going in the wrong place, as far as getting stuff doing exactly what I want. That’s fine, it’s usually a matter of time before figuring that part out.

The real issue is I have no idea what Members exist, and what they belong to. It’s pretty annoying to be this close to what I need to do, but have no way forward other than blind guessing what magic words I need to type.

https://wiki.blender.org/wiki/Source/Architecture/Context

There is no central place that lists all context members.

Great…

To find out which context members are available, 
it is best to look at the relevant callback. I.e. if you're 
working in the image window, look at the context callback 
function defined in space_image.c. 

Far as I can tell, none of the scripts in ‘source->blender->editors->object’ have the ‘default callbacks’ that you can find elsewhere.

In python, it's easy to look at what's in the context at runtime: 
print(dir(context))

If I type that into a text and run it inside blender I just get this

  File "\Text", line 1, in <module>
NameError: name 'context' is not defined

PLEASE HELP THANK YOU :crazy_face:

This is unrelated to context. You need to look at where the data structures you are using are defined. Most IDEs will be able to tell you this (Go to Definition).

In this particular case, you need to look at Object in DNA_object_types.h. Another good starting point is rna_object.c, which wraps this data structure for Python API and can tell you how internal property names correspond to API names.

1 Like

Thanks a lot! I saw this in DNA_object_types

/** Viewport draw type. */
char dt;

So at least I can change draw type with

base->object->dt = OB_WIRE;

It just isn’t working right. ‘basact’ is not acting the way I was hoping. What I thought it would do is

// set previously selected object to solid 
view_layer->basact->object->dt = OB_SOLID;
 
// set selected object to active object 
view_layer->basact = base;

// set selected object to wire 
base->object->dt = OB_WIRE;

But all it does is make everything wire, never sets it back to solid. Best I can do at the moment is make it happen on deselect, which is not good because it doesn’t consider clicking a different object a deselect. So you have to manually deselect everything to make it solid again.

Then on top of everything else, it doesn’t redraw until you move it or something similar.

Getting somewhere, but a long way to go.

Yes, changing the actual active object or object properties is not going to work well (and is problematic for e.g. multithreading). Really you need to modify the code that uses these properties to make decisions in the drawing code.

1 Like

Thanks again, saved me wasting my time in there.

After looking around a lot I think ‘view3d_edit.c -> Toggle Shading Operator’ has a lot of the information I need. I’ll start messing around making another operator in that script that tries to do something like I’m wanting.

That or ‘view3d_select.c’ I dunno crazy amount of stuff just for a glorified macro:

  1. Set display_type to solid before selecting new object, or deselect
  2. Set display_type to wire for newly selected object