【发布时间】:2012-11-08 16:55:53
【问题描述】:
这是我正在尝试创建的附加组件:
import bpy
import os
import sys
import subprocess
import time
from threading import *
class Repeat(Thread):
def __init__(self,delay,function,*args,**kwargs):
Thread.__init__(self)
self.abort = Event()
self.delay = delay
self.args = args
self.kwargs = kwargs
self.function = function
def stop(self):
self.abort.set()
def run(self):
while not self.abort.isSet():
self.function(*self.args,**self.kwargs)
self.abort.wait(self.delay)
class ExportToGIMP(bpy.types.Operator):
bl_idname = "uv.exporttogimp"
bl_label = "Export to GIMP"
def execute(self, context):
f = open("/home/antoni4040/blender.txt", "w").close()
self.filepath = os.path.join(os.path.dirname(bpy.data.filepath), "Layout")
bpy.ops.uv.export_layout(filepath=self.filepath, check_existing=True, export_all=False, modified=False, mode='PNG', size=(1024, 1024), opacity=0.25, tessellated=False)
self.files = os.path.dirname(bpy.data.filepath)
cmd = " (python-fu-bgsync RUN-NONINTERACTIVE)"
subprocess.Popen(['gimp', '-b', cmd])
file = Repeat(3, self.up)
file.start()
return {'FINISHED'};
def doit(self):
r = Repeat(3, self.update)
r.start()
def up(self):
f = open("/home/antoni4040/blender.txt", "r")
string = f.read()
if "ok" in string:
self.materialize()
self.doit()
file.stop()
else:
pass
def update(self):
self.layout_texture.image.reload()
for area in bpy.data.screens['Default'].areas:
if area.type in ['IMAGE_EDITOR', 'VIEW_3D']:
area.tag_redraw()
def materialize(self):
self.layout_texture = bpy.data.textures.new(name = "Layout_Texture", type = "IMAGE")
self.material = bpy.data.materials.new(name="Layout")
self.object = bpy.context.active_object
self.material_texture = self.material.texture_slots.add()
self.material_texture.texture = self.layout_texture
self.material_texture.texture_coords = "UV"
self.filepath2 = "/home/antoni4040/Έγγραφα/Layout1.png"
self.texture_image = bpy.data.images.load(self.filepath2)
self.layout_texture.image = self.texture_image
self.con_obj = self.object.data
self.con_obj.materials.append(self.material)
bpy.data.screens['UV Editing'].areas[1].spaces[0].image = self.texture_image
def exporttogimp_menu(self, context):
self.layout.operator(ExportToGIMP.bl_idname, text="Export To GIMP")
bpy.utils.register_class(ExportToGIMP)
bpy.types.IMAGE_MT_uvs.append(exporttogimp_menu)
与此一起使用:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gimpfu import *
import time
import sys
def blender_gimp_sync():
im_dir = "/home/antoni4040/Έγγραφα/Layout.png"
image = gimp.pdb.gimp_file_load(im_dir, im_dir)
gimp.Display(image)
layer = gimp.pdb.gimp_layer_new(image, 1024, 1024, RGBA_IMAGE, "PaintHere", 100.0, NORMAL_MODE)
image.add_layer(layer)
drawable = pdb.gimp_image_get_active_layer(image)
num = 1
while True:
time.sleep(3.0)
if image is not None:
ims_dir = "/home/antoni4040/Έγγραφα/Layout1.png"
gimp.pdb.file_png_save(image, drawable, ims_dir, ims_dir, 0, 0, 0, 0, 0, 0, 0)
f = open("/home/antoni4040/blender.txt", "w")
f.write("ok")
else:
pass
register(
"python_fu_bgsync",
"Blender-Gimp Sync",
"Syncronize Gimp with Blender for texturing",
"Antonis Karvelas",
"Antonis Karvelas",
"2012",
"Sync",
"",
[],
[],
blender_gimp_sync,
menu="<Image>/Image/Blender-Gimp"
)
main()
但是,我总是收到分段错误错误... 为什么会这样?我做错了什么?
【问题讨论】:
-
什么是段错误?搅拌机?还是搅拌机继续运行和 GIMP segfautls?尝试从 GIMP 调用中删除“-b”开关并查看它是否出现 - 或者它是否是 GIMP segafaulting。另一件事,在 GIMP 脚本上,在将“ok”写入 blender.txt 文件后,您应该关闭 r 至少刷新它,否则,其他进程将永远不会读取“ok” - 它只会在以下情况下刷新到磁盘写入一个新文件。这会阻止您的架构工作,但不应出现段错误。
-
还有一件事:您的搅拌机文件缺少开头的“# encoding: utf-8”声明 - 在解析带有希腊字符的字符串时应该引发语法错误。
-
好吧,当 Gimp 继续工作时,Blender 崩溃了……
-
好吧,现在我明白了:AttributeError: 'Context' object has no attribute 'active_object'... 很奇怪,而且没有任何理由...
-
对不起 - 我没有尝试过。当然,尝试在 ablender scritp 中使用 Python 的线程是不安全的。尝试找到一种方法从搅拌器中将异步回调到您的插件中。此外,您可以在此处发布自己的更正作为答案,以便将其记录在案。
标签: python segmentation-fault blender gimp gimpfu