On 11/07/2018 at 01:05, xxxxxxxx wrote:
Thanks Maxime,
i got there in the end, my solution was far less elegant than yours though, so thank you for spending the time to look through my code, i can certainly learn lots from your approach.
Apologies also for my scrappy code - there was parts in there that made no sense whatsoever so kudos for managing to read through that.
I'll post my solution below for reference but anyone who references this thread should definitely go with Maximes above suggestion.
def screenCalc(guide, cZ, bd, sf, rd_w, rd_h) :
gPos = guide.GetMg().off # get global position of guide
convertPos = bd.WS(gPos) # convert world to screen space
# Perform safe zone maths
convertPos[0] = (convertPos[0] - sf["cl"])/(sf["cr"]-sf["cl"])
convertPos[1] = (convertPos[1] - sf["ct"])/(sf["cb"]-sf["ct"])
convertPos[2] = (cZ - convertPos[2])/cZ
gV1 = convertPos[0] # get x from vector
gV2 = convertPos[1] # get y from vector
# Remaps vector values to screenspace values, aswell as inverts
g_Xpos = c4d.utils.RangeMap(gV1, 0, 1, 0, rd_w, True)
INVg_Xpos = c4d.utils.RangeMap(gV1, 1, 0, 0, rd_w, True)
g_Ypos = c4d.utils.RangeMap(gV2, 0, 1, 0, rd_h, True)
INVg_Ypos = c4d.utils.RangeMap(gV2, 1, 0, 0, rd_h, True)
# Loads final values into a list and returns it
pos = [int(g_Xpos), int(INVg_Xpos), int(g_Ypos), int(INVg_Ypos)]
return pos
def main() :
# +------- GET DOCUMENT DATA --------#
bd = doc.GetActiveBaseDraw() # Get current view
rd = doc.GetActiveRenderData() # Get Render Setting
rd_w = rd[c4d.RDATA_XRES_VIRTUAL] # Get X resolution
rd_h = rd[c4d.RDATA_YRES_VIRTUAL] # Get Y resolution
sf = bd.GetSafeFrame() # Get SafeFrame dims
# +------- GET CUSTOM DATA --------#
obj = op.GetObject() # Get Null
cam = obj[c4d.ID_USERDATA,2] # Get Camera
top = obj[c4d.ID_USERDATA,3] # GetGuideObjects
bottom = obj[c4d.ID_USERDATA,7] # --------------
left = obj[c4d.ID_USERDATA,8] # --------------
right = obj[c4d.ID_USERDATA,9] # --------------
switch = obj[c4d.ID_USERDATA,4] # Get Switch
cZ = cam[c4d.CAMERAOBJECT_TARGETDISTANCE] # Get z-distance
# If tag is activated
if switch:
# Use function to grab data
try:
topPos = screenCalc(top, cZ, bd, sf, rd_w, rd_h)
bottomPos = screenCalc(bottom, cZ, bd, sf, rd_w, rd_h)
leftPos = screenCalc(left, cZ, bd, sf, rd_w, rd_h)
rightPos = screenCalc(right, cZ, bd, sf, rd_w, rd_h)
# SET RENDER DATA
rd[c4d.RDATA_RENDERREGION_TOP] = topPos[2]
rd[c4d.RDATA_RENDERREGION_BOTTOM] = bottomPos[3]
rd[c4d.RDATA_RENDERREGION_LEFT] = leftPos[0]
rd[c4d.RDATA_RENDERREGION_RIGHT] = rightPos[1]
doc.SetActiveRenderData(rd)
c4d.EventAdd()
except ZeroDivisionError:
pass
else:
pass
Thanks again,
Cerac