-1

Okay, I have an object var with some values:

mm = 0.001

screenDims = {}
screenDims['width'] = mm * 500
screenDims['height'] = mm * 300
screenDims['depth'] = mm * 10

I've created a tiny function to use these values. Inside my function I have a line:

def createScreen(screenDims,radialdist,theta):

    #... some code that creates the object fine ...

    bpy.ops.transform.resize(value=(screenDims.width, screenDims.height, screenDims.depth), ... )

When I execute this, it throws an error that my object variable screenDims doesn't have a value for width.

Is there some secret for passing in object vars? Is the variable out of scope because its inside the resize function? or value designator?

I'm confused.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
Dr Tyrell
  • 125
  • 8

2 Answers2

4

To access elements in a dictionary, you have to read them like you write them:

    bpy.ops.transform.resize(value=(screenDims["width"], screenDims["height"], screenDims["depth"]), ... )

There is a difference between attributes and keys in a dictionary. Keys are accessed using the bracket notation (like are elements in an array, so there is the analogy).

Attributes are used for methods and values whose existence is usually determined by the type of the object, while the index notation is used for elements in the container. This helps distinguishing methods such as screenDims.clear() which would delete all elements in the dictionary from an element set with

screenDims["clear"] = ...

More informaiton on dictionaries is found in the official Python Tutorial, section Dictionaries.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
2

There are two ways of accessing a dictionary element:-

  1. Using get() method of dictionary -

bpy.ops.transform.resize(value=(screenDims.get("width"), screenDims.get("height"), screenDims.get("depth")), ... )

  1. You can read them the same way you wrote to create the dictionary-

bpy.ops.transform.resize(value=(screenDims["width"], screenDims["height"], screenDims["depth"]), ... )

The benefit with using get method is that you can provide some default value to be returned in case the key is not present inside the dictionary. e.g.-

screenDims.get("height","default_value")

. (dot) operator is used for accessing attributes of an object, which can either be value or method.

Chandan Kumar
  • 387
  • 3
  • 13