在Blender中使用脚本创建PDF

时间:2017-02-10 12:31:17

标签: python pdf blender reportlab

我是一个全新的新手(Blender的新手,Python的新手,以及一般的编码新手)所以请耐心等待。

我有一个Blender脚本,可以生成特定的几何体,然后渲染图像。在同一个脚本中,我想创建一个包含该图像的PDF文件。

我有两个不同的pdf生成脚本,在Blender之外工作得很好(我使用的是Spyder)但是如果我在Blender中运行相同的代码,我会遇到问题。

这是第一个:

import datetime
from reportlab.lib.enums import TA_JUSTIFY
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import mm
import os.path

formatted_date = datetime.date.today()
date_str = str(formatted_date)
full_name = "Nachname, Vorname"

fpath = "I:/MedTech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/"

fname = full_name + "_" + date_str
fcount = 0
fcounts = fname + "_" + str(fcount) + ".pdf"
while os.path.isfile(fcounts)==True:
    fcount += 1
    fcounts = fname + "_" + str(fcount) + ".pdf"
    print(fcounts)
fname = fcounts


doc = SimpleDocTemplate(fpath + fname,pagesize=letter,
                        rightMargin=72,leftMargin=72,
                        topMargin=72,bottomMargin=18)
Story=[]

KRIlogo = fpath + "Klinikum_rechts_der_Isar_logo.png" 
lg_res_x = 1920
lg_res_y = 1080
lg_w = 50
lg_h = lg_w * lg_res_y/lg_res_x
lg = Image(KRIlogo, lg_w*mm, lg_h*mm)
lg.hAlign = 'RIGHT'
Story.append(lg)

wireIm = fpath + "20170102_red_20170207-092526.png"
bl_res_x = 1920
bl_res_y = 1080
im_w = 60
im_h = im_w * bl_res_y/bl_res_x
im = Image(wireIm, im_w*mm, im_h*mm)
im.hAlign = 'LEFT'
Story.append(im)

styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
ntext = '<font size=12>%s</font>' % full_name
dtext = '<font size=12>%s</font>' % date_str

Story.append(Paragraph(ntext, styles["Normal"]))
Story.append(Spacer(1, 12))
Story.append(Paragraph(dtext, styles["Normal"]))
Story.append(Spacer(1, 12)) 

doc.build(Story)

这是第二个:

import datetime
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.lib.utils import ImageReader
import os.path

formatted_date = datetime.date.today()
date_str = str(formatted_date)
full_name = "Nachname, Vorname"

fpath = "I:/MedTech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/"

fname = full_name + "_" + date_str
fcount = 0
fcounts = fname + "_" + str(fcount) + ".pdf"
while os.path.isfile(fcounts)==True:
    fcount += 1
    fcounts = fname + "_" + str(fcount) + ".pdf"
    print(fcounts)
fname = fcounts

wireIm = fpath + "20170102_red_20170207-092526.png"
bl_res_x = 1920
bl_res_y = 1080
im_w = 60
im_h = im_w * bl_res_y/bl_res_x

WireImage = ImageReader(wireIm)

c = canvas.Canvas(fname)
c.drawImage(WireImage, 10, 10, width=60*mm)
c.showPage()
c.save()

这两个脚本给出了我几乎相同的错误:

Traceback (most recent call last):
  File "I:\MedTech_Projekte\NAM\Studenten\WenokorRebecca_SA\BLENDER CODE\2016121
9 - Present\20170109 Face Align.blend\Text.002", line 58, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\doctemplate.py", line 1200, in build
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\doctemplate.py", line 956, in build
    self.handle_flowable(flowables)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\doctemplate.py", line 821, in handle_flowable
    if frame.add(f, canv, trySplit=self.allowSplitting):
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\frames.py", line 167, in _add
    w, h = flowable.wrap(aW, h)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\flowables.py", line 484, in wrap
    return self.drawWidth, self.drawHeight
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\flowables.py", line 478, in __getattr__
    self._setup_inner()
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\flowables.py", line 442, in _setup_inner
    img = self._img
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\platypus\flowables.py", line 472, in __getattr__
    self._img = ImageReader(self._file)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 807, in __init__
    annotateException('\nfileName=%r identity=%s'%(fileName,self.identity()))
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 1387, in annotateException
    rl_reraise(t,v,b)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 144, in rl_reraise
    raise v
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 801, in __init__
    annotateException('\nImaging Library not available, unable to import bitmaps
 only jpegs\nfileName=%r identity=%s'%(fileName,self.identity()))
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 1387, in annotateException
    rl_reraise(t,v,b)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 144, in rl_reraise
    raise v
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\lib\utils.py", line 799, in __init__
    self._width,self._height,c=readJPEGInfo(self.fp)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\pdfbase\pdfutils.py", line 243, in readJPEGInfo
    x = struct.unpack('B', image.read(1))
struct.error: unpack requires a bytes object of length 1
Imaging Library not available, unable to import bitmaps only jpegs
fileName='I:/MedTech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/Kli
nikum_rechts_der_Isar_logo.png' identity=[ImageReader@0xac09ef0 filename='I:/Med
Tech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/Klinikum_rechts_der
_Isar_logo.png']
fileName='I:/MedTech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/Kli
nikum_rechts_der_Isar_logo.png' identity=[ImageReader@0xac09ef0 filename='I:/Med
Tech_Projekte/NAM/Studenten/WenokorRebecca_SA/Spyder Scripts/Klinikum_rechts_der
_Isar_logo.png']
Error: Python script fail, look in the console for now...

当我使用jpeg而不是png时,我得到以下内容:

Bibliotheken/Dokumente/Spyder Scripts/20170102_red_20170207-092526.jpeg
Traceback (most recent call last):
  File "I:\MedTech_Projekte\NAM\Studenten\WenokorRebecca_SA\BLENDER CODE\2016121
9 - Present\20170109 Face Align.blend\Text.001", line 37, in <module>
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\pdfgen\canvas.py", line 1237, in save
    self._doc.SaveToFile(self._filename, self)
  File "C:\Program Files\Blender Foundation\Blender\2.78\python\lib\site-package
s\reportlab\pdfbase\pdfdoc.py", line 218, in SaveToFile
    f = open(filename, "wb")
PermissionError: [Errno 13] Permission denied: 'Nachname, Vorname_2017-02-10_0.p
df'
Error: Python script fail, look in the console for now...

许多在线论坛都提到在处理图像时需要PIL和/或枕头。我不完全理解如何在我的代码中使用这些库,但如果代码在Spyder中没有它们的情况下工作,我不明白为什么它会在Blender中突然需要它们。

非常感谢任何帮助!如果我的问题不明确,请随时询问更多信息:)

谢谢!

2 个答案:

答案 0 :(得分:0)

Python提供了一个允许你运行python代码的环境,标准安装包含读取文件和打印文本到运行python脚本的控制台的能力,以及其他各种各样的东西。

要使用标准python安装中未包含的功能,我们可以安装和使用第三方模块,您用来创建pdf的reportlab模块就是一个例子。第三方模块。 reportlab模块知道如何创建pdf文件,如果您希望它将图像添加到pdf,那么它将使用另一个知道如何读取图像文件的模块。如果用于读取图像的模块不可用,那么它无法获得将图像添加到pdf所需的图像信息,但它仍然可以创建没有图像的pdf。

安装python时,主程序和各种模块安装在需要时可以找到的特定位置。混合器的安装包含它自己的python副本和它的标准库,它没有设置为使用你可能拥有的任何python的正常安装。正如您所发现的,您可以手动将项目添加到blender的python版本中,但是在spyder中运行的脚本(使用python的标准安装)的混合器内的失败表明您遗漏了某些内容。

第二个错误是由于权限阻止普通用户写入应用程序文件夹,这是因为您只指定了一个文件名,导致尝试在当前目录中创建该文件。您应该能够通过使用目标文件的完整路径而不仅仅是文件名来修复此错误。

您可能希望查看subprocess module从blender外部运行您的pdf创建脚本,并将使用blender创建的图像的位置作为参数传递。这将允许您运行python脚本以在blender中自动执行任务,并在spyder中使用的相同设置中执行pdf生成。

答案 1 :(得分:-1)

在您的终端类型

sudo gnome-terminal

这将为您提供root访问权限,然后尝试运行代码