I'm trying to make something like map editor for my project. I'm rather new in Blender Python scripting and I have some troubles.

I made "Material" panel and I want to set "textureName" property for my object. What I need is to make some list of loaded in blender images (like in UV Editor you can load image and it appears on list).

In my panel I want just to choose one image and assign name of that image to my property. I can list loaded images by bpy.data.images but I can't dynamically add new images to property (for now I'm using EnumProperty assigned to bpy.types.Object.textureName). I was trying to make same UI widget like blender have (http://i.imgur.com/6VJVAsN.png) but I have no idea how it works.

Can somebody help me with this?

  • $\begingroup$ That built-in widget is added to the UI via template_ID() and uses an ID Property to store the reference to an ID datablock (based on what you picked). The problem is, that we can't create new ID Properties with Python, so you can't reference a datablock. You can, however, store the name of an object in a StringProperty(). Use the prop_search() on e.g. bpy.data, "images" $\endgroup$ – CodeManX Oct 1 '13 at 10:16
  • $\begingroup$ @CoDEmanX Shouldn't this be posted as an answer and not a comment? Comments are generally for clarification or constructive criticism, not answers. $\endgroup$ – Ray Mairlot Oct 1 '13 at 10:43
  • 3
    $\begingroup$ @Ray Mairlot you can also post a incomplete answer as comment as far you think it is not clear enough for a strict expalining answer and want to give others a note what they should remind for a correct answer. so at all answering partial is ok in comments. $\endgroup$ – Zaibis Oct 1 '13 at 11:56

Adding a "real" datablock widget won't quite work and it's not possible (or at least not easy) to have a dynamically populated EnumProperty object. Fortunately, there is the prop_search() UILayout function. You can use that to assign the name of a datablock to a StringProperty variable. So, you should have something along the lines of this for that StringProperty:

bpy.types.Object.textureName = bpy.props.StringProperty(name="Texture")

With that done, all objects now have a new string property called textureName that defaults to an empty string. Now, in your panel, add something akin to the following in the draw() function:

def draw(self, context):
    ob = context.active_object

    layout = self.layout
    layout.prop_search(ob, "textureName", bpy.data, "images")

Of course, this only assigns the name of the texture to that StringProperty variable. You still need to write code that does something with this property.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.