3

Quick question:

I'm trying to set up a driver which uses the Y-scale of whatever object the driver is on (which means that I don't want to use the Y-scale of a specific object, but rather any object).

I've read that the option "Use Self" is perfect this exact scenario, but I can't figure out how to use the self variable.

Someone suggested I use bpy.data.objects[self.id_data.user.name].scale.y in the python expression but that returns an ERROR: Invalid python expression.

What am I doing wrong? Thanks!

alterdings
  • 121
  • 1
  • 6
  • I don't have the time to test it right now, but have you tried self.scale.y? –  May 28 '17 at 14:20
  • Hi Duane, thanks for the fast reply, but that too yields the "ERROR: Invalid Python Expression" message. – alterdings May 28 '17 at 14:32

1 Answers1

4

Using self in a driver is not as straight forward as thought, though it is really easy once you know where to put it. In short: In your driver, simply don't declare a variable, but use self in the expression itself to reference to the object. Check this screenshot here:

using self

This driver is hooked up to the scale.y property of an object. The important bit here is to check Use Self as indicated in the upper screenshot, so Blender actually declares what object self is pointing to automatically.

Now you can copy and paste this driver to any other object, it will take the scale.x from there as a driving source.

aliasguru
  • 11,231
  • 2
  • 35
  • 72
  • Hi aliasguru, thanks for your elaborate explanation! Unfortunately, this does not work either in my case, the Driver Value is still at 0 (although there's no Error message this time around).

    I should add that the scale.y value is animated on the object in question, should that pose a problem?

    – alterdings May 28 '17 at 16:58
  • @alterdings just to make sure, which property shall be driven by the y scale which is animated? I need to edit my answer to reflect that, as in my answer yscale is driven by xscale, so the other way around – aliasguru May 28 '17 at 17:54
  • In my case, the y scale drives the factor of a mix shader (see screenshot: http://imgur.com/a/smUh0) Thanks for taking the time to help me out! – alterdings May 28 '17 at 18:11
  • 1
    @alterdings That's the reason why you're having trouble. self in this case is not what you might think! If you read the tooltip on the Use_Self checkbox, you'll see that self can be an object, but also a bone, etc. Trouble is, that your driver lives on a Shader Node Tree. I'm not sure if self in this case is the Node Tree or the Material, but I'm almost certain it's not the object. And it can never be, because Blender allows materials to be shared across multiple objects. If now you had, say two objects using the exact same material, BOTH would try to alter the mix node of THE SAME material. – aliasguru May 28 '17 at 18:44
  • @alterdings Consider asking a separate question, explaining why you want to switch a material node property based on the object scale. There could be other solutions than drivers (at the moment Animation Nodes come into my mind, but there might be trivial approaches to the problem). – aliasguru May 28 '17 at 18:51
  • In my scene, there's a load (hundreds) of cubes which all have an animated y-scale property (all offset/different keyframes). My goal is to have the cubes switch from one shader to another as soon as the animation occurs. -> I'd only have to animate the scale and the driver takes care of the material switch. Thanks again for helping me out here. EDIT: I'll look into animation nodes, thx for the suggestion! – alterdings May 28 '17 at 18:56
  • This is interesting. If I have a driver that gets its value from the distance between an empty and the object being driven, in that context how would I use self? – Mentalist Feb 26 '21 at 04:36
  • 2
    @Mentalist You can mix the workflows, as self is just a keyword referring back to the object. So your driver can have an expression like self.location.x - var where var is a custom variable pointing to the X location of the Empty for instance. To compute the distance you'd need to evaluate all three channels and do some vector math afaik – aliasguru Mar 01 '21 at 13:55
  • Do you know how to access custom properties of the current object using self? – tobiasBora Mar 11 '22 at 18:12
  • @tobiasBora same as you would in Python: If you have a Custom Property on your object called height, you could access it using self['height'] from the driver editor – aliasguru Mar 14 '22 at 11:06
  • @aliasguru I can't make it work, I tried in Geometry nodes and Shaders: https://blender.stackexchange.com/questions/256376/use-self-in-drivers-to-return-a-custom-properties – tobiasBora Mar 14 '22 at 11:19