Forum: Poser Python Scripting


Subject: How to make a Poser texture image display what a camera sees?

Anthony Appleyard opened this issue on Feb 13, 2018 ยท 3 posts


Anthony Appleyard posted Tue, 13 February 2018 at 5:48 AM

If a Poser scene has:


an0malaus posted Sat, 22 June 2019 at 5:36 AM

I don't know if you had this question answered anywhere else, but it seemed like a nice little programming exercise, so I gave it a go. I chose a primitive, one sided square called 'square_1' as object 'P' and its material name is 'Preview' (P2) with an additional specification of the image_map node name 'Diffuse Map', so you could work with multiple images in that material and only change the right one. I've used 'Dolly Camera' as the gun sight cam, and 'Main Camera' as the render cam, with configurable resolutions for both of them (set as X,Y tuples).

Here's the script: (save as GunSightRender.py)

# GunSightRender.py
# (c) 2019 an0malaus (Geoff Hicks/GeoffIX/gwhicks)
#
# Requested on Renderosity by AnthonyAppleyard 2018
#
# v1.0  20190622    Initial version.
########################################################################################################################

version = '1.0'

import poser
import os

imagePath = 'Somewhere/on/your/computer/gun view.jpg'
imageType = 'jpg'
imageQuality = 80
#gunCamName = 'Dolly2'
gunCamName = 'Dolly Camera'
gunCamRes = (256,256)
renderCamName = 'Main Camera'
renderRes = (512,512)
picturePropName = 'square_1' # 'P'
pictureMatName = 'Preview' # 'P2'
imageNodeName = 'Diffuse Map'
imageNodeType = poser.kNodeTypeCodeIMAGEMAP
imageSrc = 'Image_Source'

scene = poser.Scene()
gunCam = scene.Actor(gunCamName)
assert gunCam and gunCam.IsCamera(), 'Camera "{}" not found!'.format(gunCamName)

renderCam = scene.Actor(renderCamName)
assert renderCam and renderCam.IsCamera(), 'Camera "{}" not found!'.format(renderCamName)

pictureProp = scene.Actor(picturePropName)
assert pictureProp and pictureProp.IsProp(), 'Prop "{}" not found!'.format(picturePropName)

mat = None
imageNode = None
try:
    mat = pictureProp.Material(pictureMatName)
except:
    pass
assert mat, 'Prop "{}" has no material "{}"!'.format(picturePropName,pictureMatName)

found = False
for layer in mat.Layers() or []:
    tree = layer.ShaderTree()
    for node in tree.Nodes() or []:
        if node.Type() == imageNodeType and node.Name() == imageNodeName:
            found = True
            break
    else:
        continue # try next layer
    break # Only get here if found
assert found, 'Prop "{}", material "{}" has no "{}" node "{}"!'.format(picturePropName,pictureMatName,imageNodeType,imageNodeName)

scene.SetCurrentCamera(gunCam)
scene.SetOutputRes(*gunCamRes) # Resolution of render output, tuple unpacked into parameters
scene.Render()
scene.SaveImage(imageType,imagePath,imageQuality)
assert os.path.exists(imagePath), 'Saved render "{}" not found!'.format(imagePath)

input = node.InputByInternalName(imageSrc)
input.SetString(imagePath)
tree.UpdatePreview()
scene.DrawAll()
scene.SetCurrentCamera(renderCam)
scene.SetOutputRes(*renderRes) # Resolution of render output, tuple unpacked into parameters
scene.Render()
print 'Done.'

### END ###

You'll need to put a real path in the imagePath string. I used a known folder on my Mac, which won't work for anyone else. Here's the material setup for the object P (square_1). Note the name set to 'Diffuse Map' which identifies which image_map node will be updated. Screen Shot 2019-06-22 at 8.21.21 pm.png Here's the view from the gun sight cam (Dolly Camera) before rendering

Screen Shot 2019-06-22 at 8.21.51 pm.png

and here's the final render from the Main Camera Screen Shot 2019-06-22 at 8.24.59 pm.png

showing the texture automagically loaded onto the square prop.

Cheers, an0malaus



My ShareCG Stuff

Verbosity: Profusely promulgating Graham's number epics of complete and utter verbiage by the metric monkey barrel.


Anthony Appleyard posted Sat, 22 June 2019 at 5:52 AM

Thanks.