0
我目前正在爲Blender創建一個導出腳本,但是我覺得我的問題更多的是基於Python,所以我在這裏發佈了它。用Python導出基於二進制的文件
一位朋友在java中爲.obj文件創建了一個轉換程序,該程序將其轉換爲自定義二進制文件格式。但是,我想跳過該過程並直接從Blender導出二進制文件。
該文件包含使用utf-8,utf-16和utf-32格式的文本,整數和浮點數。
到目前爲止,我將所有數據導出爲標準文本文件,因此我只需要以適當的編碼/格式輸出它。這是他在Java中使用的代碼數據寫入到不同編碼的文件:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class StreamConverter {
//---------------------------------------------------
public static void buffer_write_string(DataOutputStream out,String text) throws IOException{
byte[] bytes = text.getBytes();
for(int i =0;i<text.length();i++){
buffer_write_u8(out,bytes[i]);
}
}
public static void buffer_write_u32(DataOutputStream out,int i) throws IOException{
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putInt(i);
out.write(b.array());
}
public static void buffer_write_u16(DataOutputStream out,int i) throws IOException{
out.write((byte) i);
out.write((byte) (i >> 8));
}
public static void buffer_write_s16(DataOutputStream out,int i) throws IOException{
out.write((byte) i);
out.write((byte) (i >> 8));
}
public static void buffer_write_s32(DataOutputStream out,int i) throws IOException{
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putInt(i);
out.write(b.array());
}
public static void buffer_write_u8(DataOutputStream out,int i) throws IOException{
out.writeByte((byte) i);
}
public static void buffer_write_s8(DataOutputStream out,int i) throws IOException{
out.writeByte((byte) i);
}
public static void buffer_write_f64(DataOutputStream out,double i) throws IOException{
ByteBuffer b = ByteBuffer.allocate(8);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putDouble(i);
out.write(b.array());
}
public static void buffer_write_f32(DataOutputStream out,float i) throws IOException{
ByteBuffer b = ByteBuffer.allocate(4);
b.order(ByteOrder.LITTLE_ENDIAN);
b.putFloat(i);
out.write(b.array());
}
}
我不知道怎麼做,這是Python的,我想這個,看看我是否可以在至少得到整數輸出正確,但沒有運氣。
def write_u8(file, num):
enum = num.encode('utf-8')
file.write(enum)
def write_u16(file, num):
enum = num.encode('utf-16')
file.write(enum)
def write_u32(file, num):
enum = num.encode('utf-32')
file.write(enum)
用法示例:
write_u32(file, u'%i' % (vertex_count))
也試過這樣:
counts = bytes([vertex_count,tex_count,normal_count])
file.write(counts)
我有點這整個二進制/編碼的東西丟了,我已經通過Python文檔閱讀,但它沒有幫助。
任何指向教程或示例的鏈接都會很棒!