creating models from files.

I’m writing an importer. I use from_pydata() to create vertices and faces.

  1. I can’t find out how to assign materials and textures which I created from my file to the mesh I created.

  2. I can’t find a way to set a color, uv, bone name and bone weight of vertices.

  3. I can’t find a way to remove doubles after the import, the context operator won’t work as I’m in the script window when running the script (unsurprisingly).

bpy.ops.mesh.select_all(action = 'TOGGLE')
bpy.ops.mesh.remove_doubles(limit = 0.0001)

I suggest you post what you have so far, and what the import format is.

import bpy
bpy.ops.mesh.select_all(action = 'SELECT')
bpy.ops.mesh.remove_doubles(limit = 0.0001)

works from the script window, if you have a mesh object selected.

A lot of answers are to be found here:

e.g how to assign a material to a mesh and much more!

Oh sorry, the doubles was my mistake.

Haven’t gotten into making the skeleton yet. But I can’t find out how to set vertex colors.

I couldnt think of anything, so “bump”

bump. Still cant find out how to set vertex color.

Hi Truman. I did try that and I got this:

IndexError: bpy_prop_collection[index]: index 0 out of range, size 0

I thought the API changed for 2.6 or something.

Maybe I need to enable vertex colors on the mesh first, but i don’t see any method or boolean variable to set that.

Hi wynk,

Yes, you will need to add vertex colours first. Try using:['Cube']"VertexCol1")

You can also add multiple vertex colour layers.


The format I use supports RGBA vertex colors, but Blender only seems to support RGB. Information will get lost after importing and exporting again. Can anything be done?

Also, I still don’t know how to set vertex weight for individual vertex.

BTW, how can you assign a material to vertex group, not mesh? Or can you perhaps assign a material to mesh given the face index and number of faces to assign the material? So you can only assign a material to some faces?

sorry for bumping this, but, well, bump.

Learn a little bit to answer your questions yourself!

E.g make a cube and subdivide once.
give it two materials (different!)
go to edit mode and select e.g one face
go to the menu where you can set vertex groups
add a new one select your face vertices deselect select (to see it workded)
select the OHTHER material and press Assign
drag the info part (click boarder) to below and you see your API actions like this

bpy.ops.object.material_slot_assign() #this is the only part which is not yet clear

But such API work on the ‘latest’ active … (whatever) …
Meaning you have to find out to make the good material slot active and
something like this you can ask here

BUT WITHOUT that stupid “BUMP” … just tell what you do not know to do (IMHO).
And after some more activity you may add pictures and or a .blend file here! (is dependent on the number of post must be greater than x (x around ??? 20???)

Creating bone weights and bone indexes for a vertice cant be guessed by doing it in GUI and looking at the Info window output, because the only way i see of doing it by GUI is the weight paint tool.

I thought there would be a faster way to add vertices to vertexgroups than selecting the vertices, assigning them, and then deselecting again. Same for material assignment.

My other question was, like I said, is there no way to use RGBA colors for vertices? My format uses Alpha for vertex colors so passing it through Blender will make it lose information.

How can you select an object by code? Not everything gets printed in the Info window, like this. I know you can set an object as active, but active != selected, some operators wont work on an active object, unless it’s also selected, like shade_smooth().

I have nothing else to add to my questions, so I could write a pointless sentence like others, but it would have the same meaning as “bump”. Sorry.

Maybe you don’t need blender as you have your format.

lol … batFINGER,
what would be (internet-)life without trolls?

so this code-snippet may be for interest for any real blender-script(?kiddy)er;
its pupose is to run blender to generate one render-image and it was tested
with blender-2.61 (the original download from, the linux-64bit-version)

saved in a file with name:

# test-dr -- tested with blender-2.61 like this:
# ./blender -b fireworks1.blend -P 60
# then renders frame 60 and exits
import bpy
import sys
print( sys.argv)
sframe = bpy.context.scene.frame_start
eframe = bpy.context.scene.frame_end
#the place to store the image
bpy.context.scene.render.filepath="\	mp\\dummy1"

#what renderer to use
bpy.context.scene.render.engine = "BLENDER_RENDER"
#bpy.context.scene.render.engine = "CYCLES"

renderframe = 1
# was an arg at the end after the script-name
# then use it as the frame to be rendered
if sys.argv[-1] != "":
  renderframe = int(sys.argv[-1])
bpy.context.scene.frame_end = renderframe
bpy.context.scene.frame_start = renderframe
bpy.context.scene.frame_end = renderframe
bpy.context.scene.frame_current = renderframe


and if running from a terminal with the shell,
one could do some simple looping like this:

for f in 10 20 30 40; do ./blender -b myblend.blend -P $f; done

creating the images for the frames 10, 20, 30, and 40 … if it does not crash …:wink:

@batFINGER: What are you trying to say?

And why do you quote my questions before saying it?

You assumed that I’m using my own format (but I’m not) and thought if I think I’m so smart to use my own format, maybe I think I’m so smart that I don’t need Blender and can write my own program.
Firstly, I’m not using a 3d format I invented and even if I did that doesn’t mean anything.

Man, you just made some wrong assumptions. But even if you were right, it doesn’t allow you to be offensive.

@test-dr: I don’t see how your script answers any of my [email protected]

sorry “wynk”,
if you did not notice, it was special answer for batFINGER… :wink:

Didn’t post that. Not completely innocent. Been teaching a mate how to use blender on the weekends. part of which is how to use the forums to get answers. When I posted post number 2 in this thread last weekend, I had a little wager that no code would be produced… and guess what?.. I won, cheers. We had a good laugh about it… probably came up with the above smart ass reply… tell you what glad he used that one. Wondered why he was grinning like a Cheshire cat when he left after paying up.

Pointless getting offended by silly boys after a few sherbets…

import bpy
#select the batfinger object
ob =["Cube"] = "batFINGER"

#link to the doghouse scene
if "doghouse" in
    sce =["doghouse"]
    print("there is no doghouse %s, otherwise you'd be in it"

This is really funny and all, but can we get back to the topic.

I figured out how to select an object by code: bpy.ops.object.select_name(name = “”). Would be easier to guess from another name like just select(name = “”), or select_by_name(name = “”), but its figured out.

Still no idea how you set bone weights (per-vertex that is, not per vertex group).
And still no idea if I can set Alpha for vertex color as well.

old Blender see here:

New Blender I think no change …