Writing plugins for Blender

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Writing plugins for Blender

Post by sakky »

What's up? I'm new to this program and these forums. I never used Blender before at all. I've played around with it for a bit; it seams okay. I'm use to LightWave, that's the problem.

I'm currently working on a little game. I've made my own 3D file format for it too. It's pretty simple, nothing really to complex. It's just that LightWave is a pain in the ass to write plug ins for. It's got some really stupid crap and work around for just doing the most simple things. For instance, file io routines. You have to start, yes start the write process and then you have like 20 write routines that write specific data making it harder then hell to write you own custom data. then you have to stop the write routine. Any ways, LightWave's complexity has finally pissed me off!

So I looked for a more simpler program to let me do what I want. I need to be able to create content quick and easy. I also need to be able to write plug ins for that program that will write to my custom file formats.

So, I chose Blender. A lot of my friends told me it would be my best bet. So how do I write plug ins for Blender? I've downloaded Python, but I'd rather use C/C++. This is because I do not know how Python handles file IO, not do I know if it will write the information in the same size. I've think I've read something that Python could wrap C. IF this is true, then Blender is truly the program I need because I can write a ton of shit for it in C and wrap it in Python.

Any ways, if any o you have helpful advice, please please give lend my your knowledge. :)

SirDude
Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)
Contact:

Post by SirDude »

Well for blender you have two options.

Write a python script to import/export your fileformat. I would recomend
this method because its the easiest/fastest. Just look at some of the
other import/exporters that are bundled with blender and off you go.

You could also write the import/export stuff in c. If your going to go
this route look at:
blender/src/blender/source/blender/blenkernel/intern/exotic.c

joeri
Posts: 96
Joined: Fri Jan 10, 2003 6:41 pm
Contact:

Post by joeri »

I'm glad sirDude responded, he knows his stuff.
I'd go for python as well. But as I export some obj from blender I would not say it's fastest. Unless you mean fast to implement, once using it can get pretty slow. So where do you want to lose/win time?

To get a good idea of how blender handles it's data it's probably best to use python and do the export in the backgrond.
But I would not mind if more exporters would be a little faster :)

stiv
Posts: 0
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv »

This is because I do not know how Python handles file IO, not do I know if it will write the information in the same size.
Python I/O is pretty much like C I/O. Look at the struct module for writing binary data
I've think I've read something that Python could wrap C. IF this is true, then Blender is truly the program I need because I can write a ton of shit for it in C and wrap it in Python.
Look at the docs on extending and embedding on python.org. Our Blender python extensions are written in C. The initial goal of the bpy interface was to enable writing importers and exporters.

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Good! Then I will use Python for now until I learn more of Blender. Once I do, however, I will switch to C and implement most of my needed functionality through it.

About the speed issue, it's not a big deal. I'm only exporting the models from Blender and not using Python to implement any crucial part of the game. However, I will use it to export my models for two reasons. First, I would like to learn the programming or scripting language(if there is a difference) because it might help out with other projects in the future. Second, learning new program and it's API can be a pain. LightWave lost me on this one! Not because it's to hard, but because I though it was horrible to work with and needs a serious re-implementation. Any ways, I though it would help me learn Blender a little better and easier using a higher level language.

So the, I guess it's off to reading Python and Blender docs I go. I'm more looking for actual API reference(similar to MSDN) for Blender. It helps in the development process to have this resource. Could any of you post a link? Unless of course you already did ;)

Also, I downloaded the Blender source. I was hoping to get a better understanding of the technology it uses. Well, and I was cheating by reading some of the online docs. However I didn't find a suitable search/help reference for it though.

Any ways, thanks for all your replies guys!

SirDude
Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)
Contact:

Post by SirDude »

On the speed thing I ment will be faster for you to finish it. In general your probably not too worried about the speed of the importer. I don't think the
difference between python importing and c importing is generally an issue.
I'm sure someone could come up with something though ;)

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Well, I've been studying Python. I haven't found any really good tutorials that actually go into detail about it's artcitecture. What I'm looking for is the typical "Hello, World!" examples that introduction to Python like an introduction to C would be. I know they are two different languages, but still. I'd rather have a more solid foundation of it, then just readin some ones garbage. I mean thati n a nice way too. I guess I'm just really picky when it comes to programming.

I know how to program in C/C++ very well. I about 5 1/2 years experience with it. So programming in C is not to trivial for me. But learning a new language sort of is. It's just that the tutoial structure of mostly everything I've read isn't that great. It's like, more geared for begginers to programming. Also, it's like none of them give a good explanation of the syntax or semantics. That's what I'm interested in because it seams that eacch example I look at is a little different. So it's hard for me to form a skill with limited material.

I've already wrote the file io routines in C. I can compile them just fine, so all I need to do now is find a way to incorporate them into Blender. So I guess I'll just keep looking, something has got to come up.

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Found it-> Python Reference Manual! This is what I was looking for. And I didn't even have to search the net, just my hard drive :)

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

From what I see, Python doesn't write binary files-only flat files(text based). Is this true?

I'm reading some tutorials that only describe write routines that use string literals and do not except binary data. I tried the fallowing

>>> f = open( '../test.dat', 'wb' )
>>> a = 128
>>> s = str( a )
>>> f.write( s )
>>> f.close( )
>>>
>>> f = open( '../test.dat', 'wb' )
>>> a = 128
>>> f.write( a )

Traceback (most recent call last):
File "<pyshell#8>", line 1, in -toplevel-
f.write( a )
TypeError: argument 1 must be string or read-only buffer, not int

Frustrating! So I guess I use Python to wrap my C io routines with the Blender API...

blendix
Posts: 51
Joined: Wed Oct 16, 2002 1:00 pm

Post by blendix »

As stivs mentioned, use the struct module:
http://docs.python.org/lib/module-struct.html

Example (writes float and unsigned int):

import struct
f = open("file.txt", "wb")
f.write(struct.pack("fI", 3.14, 10))
f.close()

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Okay, I'm having problems using Python. I've wrote my own C routines and some of them worked fine. But, the errors were design because I could not use them to ftell() or what ever Python uses.

So I figured I would write the format using Python because it seams lots of Python is implemented in C and that dat a types sort of interchange. so I need to write these structures

typedef struct _MODVERTEX {
float X, Y, Z ;
unsigned long RGB ;
float U, V ;
} MODVERTEX ;

typedef struct _MODHEADER {
unsigned long Sig ;
unsigned long numVertices ;
unsigned long offVertices ;
unsigned long numPolygons ;
unsigned long offPolygons ;
char MaterialName[ 16 ] ;
} MODHEADER ;

As you can probebly tell, those are C structures. The Sig variable is ASCII "MOD1". I built it in C++ with a custom macro--> ( DWORD )( 'M' | 'O' | 'D' | '1' ). The off* variables are offsets into the file(duh) where the vertices and polygon data is. A polygon is just an array of 3 WORD(unsigned short)s that are used as indices to the vertex chunk.

Like I said befoer, really simple! I don't need a ton of information just for an inanimate object and that's what this format is for. Also, the engine doesn't use lighting(not yet, any ways). I'll probebly change small things in the format here and there, but most of it will stay the same. I really don't want to go through all the truble of converting them to Python.

So from what I've been told, I use the 'struct' module to write binary data. I know how to make a class, but not an array of them. Also, I'm having a hell of a time trying to find Blender Python API tutorials. Looking around in a script is hardly what I would call a resource. So I was wondering if any of you had good links Blender Python API tutorials or atleast reference to it so I can research it my self instead of browsing user script files.

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Okay never mind, I found it! I hate it when I look in the wrong place!

Any ways, the plug in worked! It took me long enough, but I think I learned a lot out of it. But I still have a problem

The problem isn't that the plug-in didn't work because it exported successfully, but the polygon information is all screwed up-sort of. All the vertices seam to be in the right place. I know this because I tested it with drawing points. But when I draw polygons, the polygons are all messed up.

I'm rendering with D3DPT_TRIANGLSTRIP (i.e. Direct3D triangle strips. Do any of you know how I can figure out the order of the vertices and how I can use this information? Use, as in how do I know-what face is in what type of configuration.

Damn, so close!

Brandano
Posts: 0
Joined: Mon Apr 19, 2004 6:03 pm

Post by Brandano »

It's in the Blender Python API docs
The NMesh object has both a list of vertices and a list of
faces.
Each face has a list of vertices (I believe as indices of the NMesh.verts list, but they could be NMVert objects, I got to check that), these are the ones you are looking for. A face can have up to 4 vertices, so you might want to break them down to triangles first, depending on your model format. Anyway, seen as you are working with Direct3D, and probably are using the DirectX SDK, wouldn't be easier to just use the .X exporter?

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

Yes and no! Yes if I just want to use some else's model format. No, if I want to build a custom format the suites my engine.

Besides, how much can you actually learn by always using some else's stuff? I see many little demo programs that all use MD*, BSP or 3DS level formats. That may be fine, but what happens when you need something specific to your engine? I suppose you can rewrite a lot of you code or try to convert everything, but to me that seams a waste of time. Besides, its far more cooler having designed and built your own custom format. I really don't like Direct3D's X file format any ways. I rather write my own routines for specific data that my engine works with. This is one reason why I like OpenGL because it lets you do this with a lot less slack. And it's some times faster then Direct3D.

I know what the face indices are. My problem was that the order in which they are drawn or stored. If you have ever programmed 3D graphics before, then you should remember that lists, fans or strips have different requirements for the vertex order. This is the problem with Direct3D.

Though, I think I may have stumbled across a solution. My format only supports triangles, so I converted the mesh to triangles. A list seams to be rendering them correctly, but now the texture coords are not working.

So I think I need to run through the list of faces, record the UVs then write them using pared list (i.e. verts and uvs). I'm currently using NMMesh.verts.uvco for texture coordinates. The docs say, that there sticky! What ever the hell that means?! All the example scripts I've seen use the face uvs, so I guess I have to build a list of those texture coordinates.

I really don't know how to texture in Blender that well (uv map etc.) so that may have something to do with. In LightWave, you had to save the UV configuration and once you associated as UV with an object, blamo...it has UV coordinates. I'm taking Blender handles it a little different but very much the same pattern?

sakky
Posts: 0
Joined: Mon Apr 25, 2005 12:59 pm

Post by sakky »

It's still not working. I got thje texture to show up, but all the coordinates are messed up. The texture is like chopped uo all over the model.

Also, how do you delete UV coordinates 'per uv' in the UV window? Because I ws working on the UV map and it kept adding in the grbge and I had no way to delete it-X didn't work. Also, how do I force sticky coordinates so that the vertices will have the texture coordinates and not the facecs?

I'm having problems figuring out how the UVs work in Blender. I only want on texture per model. And I only want the information for those texture coordinates to reside in the vertex. If it shows up in the face, oh well, but it they must be in the vertex!

Post Reply